1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Zemit Framework. |
4
|
|
|
* |
5
|
|
|
* (c) Zemit Team <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Zemit\Bootstrap; |
12
|
|
|
|
13
|
|
|
use Phalcon\Debug; |
14
|
|
|
use Phalcon\Config; |
|
|
|
|
15
|
|
|
use Zemit\Di\Injectable; |
16
|
|
|
use Zemit\Events\EventsAwareTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Prepare |
20
|
|
|
* Prepare raw php stuff early in the bootstrap |
21
|
|
|
* |
22
|
|
|
* @author Julien Turbide <[email protected]> |
23
|
|
|
* @copyright Zemit Team <[email protected]> |
24
|
|
|
* |
25
|
|
|
* @since 1.0 |
26
|
|
|
* @version 1.0 |
27
|
|
|
* |
28
|
|
|
* @package Zemit\Bootstrap |
29
|
|
|
*/ |
30
|
|
|
class Prepare extends Injectable |
31
|
|
|
{ |
32
|
|
|
use EventsAwareTrait; |
33
|
|
|
|
34
|
|
|
private static bool $initialized = false; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Prepare raw php stuff |
38
|
|
|
* - Initialize |
39
|
|
|
* - Random fixes |
40
|
|
|
* - Define constants |
41
|
|
|
* - Force debug |
42
|
|
|
* - Force PHP settings |
43
|
|
|
*/ |
44
|
4 |
|
public function __construct() |
45
|
|
|
{ |
46
|
4 |
|
$this->initialize(); |
47
|
4 |
|
$this->forwarded(); |
48
|
4 |
|
$this->define(); |
49
|
|
|
// $this->debug(); |
50
|
|
|
// $this->php(); |
51
|
4 |
|
self::$initialized = true; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Initialisation |
56
|
|
|
*/ |
57
|
|
|
public function initialize() |
58
|
|
|
{ |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Fix for forwarded https |
63
|
|
|
*/ |
64
|
4 |
|
protected function forwarded() |
65
|
|
|
{ |
66
|
4 |
|
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') { |
67
|
|
|
$_SERVER['HTTPS'] = 'on'; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Prepare application environment variables |
73
|
|
|
* - APPLICATION_ENV |
74
|
|
|
* - APP_ENV |
75
|
|
|
* - ENV |
76
|
|
|
*/ |
77
|
4 |
|
protected function define() |
78
|
|
|
{ |
79
|
4 |
|
defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production')); |
80
|
4 |
|
defined('APP_ENV') || define('APP_ENV', (getenv('APP_ENV') ? getenv('APP_ENV') : APPLICATION_ENV)); |
81
|
4 |
|
defined('ENV') || define('ENV', (getenv('ENV') ? getenv('ENV') : APPLICATION_ENV)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Prepare debugging |
86
|
|
|
* - Prepare error reporting and display errors natively with PHP |
87
|
|
|
* - Listen with phalcon debugger |
88
|
|
|
*/ |
89
|
4 |
|
public function debug(Config $config = null) |
90
|
|
|
{ |
91
|
4 |
|
$config ??= $this->config; |
92
|
|
|
|
93
|
4 |
|
if ($config->app->debug || $config->debug->enable) { |
|
|
|
|
94
|
|
|
// Enabling error reporting and display |
95
|
|
|
error_reporting(E_ALL); |
96
|
|
|
ini_set('display_errors', 1); |
97
|
|
|
} else { |
98
|
|
|
// Disabling error reporting and display |
99
|
4 |
|
error_reporting(-1); |
100
|
4 |
|
ini_set('display_errors', 0); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Prepare some PHP config |
106
|
|
|
* - Should be initialized only once |
107
|
|
|
* |
108
|
|
|
* @param Config|null $config |
109
|
|
|
* @param false $force |
110
|
|
|
*/ |
111
|
4 |
|
public function php(Config $config = null, $force = false) : void |
112
|
|
|
{ |
113
|
4 |
|
if (!$force && self::$initialized) { |
|
|
|
|
114
|
4 |
|
return; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$appConfig = $config->app ?? $this->config->app; |
|
|
|
|
118
|
|
|
|
119
|
|
|
if ($appConfig) { |
120
|
|
|
setlocale(LC_ALL, 'fr_CA.' . $appConfig->encoding, 'French_Canada.1252'); |
121
|
|
|
date_default_timezone_set($appConfig->timezone ?? 'America/Montreal'); |
122
|
|
|
mb_internal_encoding($appConfig->encoding ?? 'UTF-8'); |
123
|
|
|
mb_http_output($appConfig->encoding ?? 'UTF-8'); |
124
|
|
|
ini_set('memory_limit', $appConfig->memoryLimit ?? '256M'); |
125
|
|
|
ini_set('post_max_size', $appConfig->postLimit ?? '20M'); |
126
|
|
|
ini_set('upload_max_filesize', $appConfig->postLimit ?? '20M'); |
127
|
|
|
ini_set('max_execution_time', $appConfig->timeoutLimit ?? '60'); |
128
|
|
|
ini_set('html_errors', $appConfig->htmlErrors ?? 0); |
129
|
|
|
set_time_limit($appConfig->timeoutLimit ?? '60'); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: