|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CakeCMS Core |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of the of the simple cms based on CakePHP 3. |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
* |
|
9
|
|
|
* @package Core |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
* @copyright MIT License http://www.opensource.org/licenses/mit-license.php |
|
12
|
|
|
* @link https://github.com/CakeCMS/Core". |
|
13
|
|
|
* @author Sergey Kalistratov <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Core; |
|
17
|
|
|
|
|
18
|
|
|
use Core\Path\Path; |
|
19
|
|
|
use JBZoo\Utils\FS; |
|
20
|
|
|
use Pimple\Container; |
|
|
|
|
|
|
21
|
|
|
use Cake\Utility\Hash; |
|
22
|
|
|
use Cake\Core\Configure; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class Cms |
|
26
|
|
|
* |
|
27
|
|
|
* @package Core |
|
28
|
|
|
*/ |
|
29
|
|
|
class Cms extends Container |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get cms instance. |
|
34
|
|
|
* |
|
35
|
|
|
* @return Cms |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function getInstance() |
|
38
|
|
|
{ |
|
39
|
|
|
static $instance; |
|
40
|
|
|
if (null === $instance) { |
|
41
|
|
|
$instance = new self(); |
|
42
|
|
|
$instance->_initialize(); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $instance; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Merge configure values by key. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $key |
|
52
|
|
|
* @param array|string $config |
|
53
|
|
|
* @return array|mixed |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function mergeConfig($key, $config) |
|
56
|
|
|
{ |
|
57
|
|
|
$values = Hash::merge((array) Configure::read($key), $config); |
|
58
|
|
|
Configure::write($key, $values); |
|
59
|
|
|
|
|
60
|
|
|
return $values; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* On initialize application. |
|
65
|
|
|
* |
|
66
|
|
|
* @return void |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function _initialize() |
|
69
|
|
|
{ |
|
70
|
|
|
$this['path'] = function () { |
|
71
|
|
|
return $this->_initPaths(); |
|
72
|
|
|
}; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Init base paths. |
|
77
|
|
|
* |
|
78
|
|
|
* @return Path |
|
79
|
|
|
* @throws \JBZoo\Path\Exception |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function _initPaths() |
|
82
|
|
|
{ |
|
83
|
|
|
$path = Path::getInstance(); |
|
84
|
|
|
$path->setRoot(ROOT); |
|
85
|
|
|
|
|
86
|
|
|
// Setup all webroot paths |
|
87
|
|
|
$path->set('webroot', Configure::read('App.wwwRoot')); |
|
88
|
|
|
foreach ((array) Plugin::loaded() as $name) { |
|
89
|
|
|
$plgPath = Plugin::path($name) . '/' . Configure::read('App.webroot') . '/'; |
|
90
|
|
|
$path->set('webroot', FS::clean($plgPath), Path::MOD_APPEND); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// Setup applicatiojn paths |
|
94
|
|
|
$paths = Configure::read('App.paths'); |
|
95
|
|
|
foreach ($paths as $alias => $_paths) { |
|
96
|
|
|
$path->set($alias, $_paths); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $path; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: