1 | <?php |
||
2 | |||
3 | use Phile\Core\Config; |
||
4 | use Phile\Core\Container; |
||
5 | use Phile\Core\Event; |
||
0 ignored issues
–
show
|
|||
6 | use Phile\Core\Router; |
||
7 | use Phile\Phile; |
||
8 | use Phile\Plugin\PluginRepository; |
||
9 | |||
10 | /** |
||
11 | * Setup global Container with dependencies |
||
12 | * |
||
13 | * There's no need to make changes here in an ordinary Phile installation. |
||
14 | * It allows advanced configuration options if necessary. |
||
15 | * |
||
16 | * @author PhileCMS |
||
17 | * @link https://github.com/PhileCMS/Phile |
||
18 | * @license http://opensource.org/licenses/MIT |
||
19 | */ |
||
20 | |||
21 | $config = [ |
||
22 | 'types' => [ |
||
23 | 'Phile_App' => Phile::class, |
||
24 | 'Phile_Config' => Config::class, |
||
25 | 'Phile_EventBus' => Event::class, |
||
26 | 'Phile_Plugins' => PluginRepository::class, |
||
27 | 'Phile_Router' => Router::class, |
||
28 | |||
29 | 'Phile_Cache' => \Phile\ServiceLocator\CacheInterface::class, |
||
30 | 'Phile_Template' => \Phile\ServiceLocator\TemplateInterface::class, |
||
31 | 'Phile_Parser' => \Phile\ServiceLocator\ParserInterface::class, |
||
32 | 'Phile_Data_Persistence' => \Phile\ServiceLocator\PersistenceInterface::class, |
||
33 | 'Phile_Parser_Meta' => \Phile\ServiceLocator\MetaInterface::class, |
||
34 | 'Phile_ErrorHandler' => \Phile\ServiceLocator\ErrorHandlerInterface::class, |
||
35 | ] |
||
36 | ]; |
||
37 | |||
38 | $container = new Container($config); |
||
39 | Container::setInstance($container); |
||
40 | |||
41 | $container->set('Phile_EventBus', function (): Event { |
||
42 | return new Event; |
||
43 | }); |
||
44 | |||
45 | $container->set('Phile_Config', function (): Config { |
||
46 | return new Config; |
||
47 | }); |
||
48 | |||
49 | $container->set('Phile_App', function (Container $container): Phile { |
||
50 | return new Phile($container->get('Phile_EventBus'), $container->get('Phile_Config')); |
||
51 | }); |
||
52 | |||
53 | $container->set('Phile_Plugins', function (Container $container): PluginRepository { |
||
54 | return new PluginRepository($container->get('Phile_EventBus')); |
||
55 | }); |
||
56 |
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: