Skip to content

Commit 1a055fc

Browse files
committed
Merge pull request #3 from PHP-DI/explicit-modules
Explicitly enable modules
2 parents 9e7aa8d + aa658d5 commit 1a055fc

File tree

4 files changed

+90
-51
lines changed

4 files changed

+90
-51
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ matrix:
1212
env: dependencies=lowest
1313

1414
before_script:
15-
- composer selfupdate
15+
- composer selfupdate -n
1616
- if [[ "$TRAVIS_PHP_VERSION" == '5.6' ]]; then composer require satooshi/php-coveralls:dev-master -n ; fi
1717
- if [[ "$TRAVIS_PHP_VERSION" != '5.6' ]]; then composer install -n ; fi
1818
- if [ "$dependencies" = "lowest" ]; then composer update --prefer-lowest --prefer-stable -n; fi;

README.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,49 @@ Requirements:
2222

2323
## Usage
2424

25-
TODO
25+
The kernel's role is to create the container. It does so by registering all the configuration files of the modules we ask it to load:
26+
27+
```php
28+
$kernel = new Kernel([
29+
'twig',
30+
'doctrine',
31+
'app',
32+
]);
33+
34+
$container = $kernel->createContainer();
35+
```
36+
37+
### Installing a module
38+
39+
To install a 3rd party module:
40+
41+
- install the package using Composer
42+
- add it to the list of modules your kernel will load, for example:
43+
44+
```php
45+
$kernel = new Kernel([
46+
'twig',
47+
]);
48+
```
49+
50+
### Creating a module
51+
52+
1. choose a module name, for example `blogpress`
53+
1. create a resource directory in your package, usually `res/`
54+
1. map it with Puli, for example `puli map /blogpress res`
55+
1. create as many PHP-DI configuration files as needed in `res/config/`
56+
57+
That's it. Here is what your package should look like:
58+
59+
```
60+
res/
61+
config/
62+
config.php
63+
...
64+
src/
65+
...
66+
composer.json
67+
puli.json
68+
```
69+
70+
When users install your package and tell the kernel to load the `blogpress` module, it will load all the files matching the Puli path `/blogpress/config/*.php` (i.e. `vendor/johndoe/blogpress/res/config/*.php` on the filesystem).

src/Kernel.php

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
use DI\Container;
77
use DI\ContainerBuilder;
88
use Doctrine\Common\Cache\Cache;
9-
use Puli\Discovery\Api\Binding\Binding;
109
use Puli\Discovery\Api\Discovery;
11-
use Puli\Discovery\Binding\ResourceBinding;
10+
use Puli\Repository\Api\Resource\FilesystemResource;
1211
use Puli\Repository\Api\ResourceRepository;
13-
use Puli\Repository\Resource\FileResource;
1412

1513
/**
1614
* Application kernel.
@@ -19,23 +17,24 @@
1917
*/
2018
class Kernel
2119
{
22-
/**
23-
* Name of the binding for PHP-DI configuration files in Puli.
24-
*
25-
* @see http://docs.puli.io/en/latest/discovery/introduction.html
26-
*/
27-
const PULI_BINDING_NAME = 'php-di/configuration';
28-
2920
/**
3021
* If null, defaults to the constant PULI_FACTORY_CLASS defined by Puli.
3122
*
3223
* @var string|null
3324
*/
3425
private $puliFactoryClass;
3526

36-
public function setPuliFactoryClass($class)
27+
/**
28+
* @var string[]
29+
*/
30+
private $modules;
31+
32+
/**
33+
* @param array $modules The name of the modules to load.
34+
*/
35+
public function __construct(array $modules = [])
3736
{
38-
$this->puliFactoryClass = $class;
37+
$this->modules = $modules;
3938
}
4039

4140
/**
@@ -55,8 +54,6 @@ public function createContainer()
5554
$factory = new $factoryClass();
5655
/** @var ResourceRepository $repository */
5756
$repository = $factory->createRepository();
58-
/** @var Discovery $discovery */
59-
$discovery = $factory->createDiscovery($repository);
6057

6158
$containerBuilder = new ContainerBuilder();
6259

@@ -65,32 +62,31 @@ public function createContainer()
6562
$containerBuilder->setDefinitionCache($cache);
6663
}
6764

68-
// Discover and load all configuration files registered under `php-di/configuration` in Puli
69-
$bindings = $discovery->findBindings(self::PULI_BINDING_NAME);
70-
$bindings = array_filter($bindings, function (Binding $binding) {
71-
return $binding instanceof ResourceBinding;
72-
});
73-
/** @var ResourceBinding[] $bindings */
74-
foreach ($bindings as $binding) {
75-
foreach ($binding->getResources() as $resource) {
76-
if (!$resource instanceof FileResource) {
77-
throw new \RuntimeException(sprintf('Cannot load "%s": only file resources are supported', $resource->getName()));
78-
}
79-
$containerBuilder->addDefinitions($resource->getFilesystemPath());
80-
}
81-
}
82-
8365
// Puli objects
8466
$containerBuilder->addDefinitions([
8567
ResourceRepository::class => $repository,
86-
Discovery::class => $discovery,
68+
Discovery::class => function () use ($factory, $repository) {
69+
return $factory->createDiscovery($repository);
70+
},
8771
]);
8872

73+
foreach ($this->modules as $module) {
74+
$this->loadModule($containerBuilder, $repository, $module);
75+
}
76+
8977
$this->configureContainerBuilder($containerBuilder);
9078

9179
return $containerBuilder->build();
9280
}
9381

82+
/**
83+
* @param string $class
84+
*/
85+
public function setPuliFactoryClass($class)
86+
{
87+
$this->puliFactoryClass = $class;
88+
}
89+
9490
/**
9591
* Override this method to configure the cache to use for container definitions.
9692
*
@@ -107,4 +103,14 @@ protected function getContainerCache()
107103
protected function configureContainerBuilder(ContainerBuilder $containerBuilder)
108104
{
109105
}
106+
107+
private function loadModule(ContainerBuilder $builder, ResourceRepository $resources, $module)
108+
{
109+
// Load all config files in the config/ directory
110+
foreach ($resources->find('/'.$module.'/config/*.php') as $resource) {
111+
if ($resource instanceof FilesystemResource) {
112+
$builder->addDefinitions($resource->getFilesystemPath());
113+
}
114+
}
115+
}
110116
}

tests/KernelTest.php

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
use DI\Kernel\Kernel;
66
use DI\Kernel\Test\Fixture\PuliFactoryClass;
77
use Puli\Discovery\Api\Discovery;
8-
use Puli\Discovery\Api\Type\BindingType;
9-
use Puli\Discovery\Binding\ResourceBinding;
108
use Puli\Discovery\InMemoryDiscovery;
119
use Puli\Repository\Api\ResourceRepository;
1210
use Puli\Repository\InMemoryRepository;
@@ -58,26 +56,16 @@ public function registers_puli_discovery()
5856
/**
5957
* @test
6058
*/
61-
public function registers_module_configuration_files()
59+
public function loads_module_configs()
6260
{
63-
$this->createPuliResource('/blog/config.php', __DIR__.'/Fixture/config.php');
64-
$this->bindPuliResource('/blog/config.php', Kernel::PULI_BINDING_NAME);
61+
PuliFactoryClass::$repository->add('/blog/config/config.php', new FileResource(__DIR__.'/Fixture/config.php'));
6562

63+
$this->kernel = new Kernel([
64+
'blog',
65+
]);
66+
$this->kernel->setPuliFactoryClass(PuliFactoryClass::class);
6667
$container = $this->kernel->createContainer();
67-
$this->assertEquals('bar', $container->get('foo'));
68-
}
6968

70-
private function createPuliResource($path, $file)
71-
{
72-
PuliFactoryClass::$repository->add($path, new FileResource($file));
73-
}
74-
75-
private function bindPuliResource($path, $bindingName)
76-
{
77-
PuliFactoryClass::$discovery->addBindingType(new BindingType($bindingName));
78-
79-
$binding = new ResourceBinding($path, $bindingName);
80-
$binding->setRepository(PuliFactoryClass::$repository);
81-
PuliFactoryClass::$discovery->addBinding($binding);
69+
$this->assertEquals('bar', $container->get('foo'));
8270
}
8371
}

0 commit comments

Comments
 (0)