Skip to content

Commit d41aeff

Browse files
committed
First commit
0 parents  commit d41aeff

14 files changed

+370
-0
lines changed

.coveralls.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
coverage_clover: clover.xml
2+
json_path: coveralls-upload.json

.gitattributes

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# .gitattributes
2+
tests/ export-ignore
3+
.coveralls.yml export-ignore
4+
.gitattributes export-ignore
5+
.gitignore export-ignore
6+
.travis.yml export-ignore
7+
phpunit.xml.dist export-ignore
8+
9+
# Auto detect text files and perform LF normalization
10+
* text=auto

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor/
2+
/composer.lock
3+
/.puli/

.travis.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
language: php
2+
3+
php:
4+
- 5.4
5+
- 5.5
6+
- 5.6
7+
- 7.0
8+
- hhvm
9+
10+
matrix:
11+
include:
12+
- php: 5.4
13+
env: dependencies=lowest
14+
15+
before_script:
16+
- if [[ "$TRAVIS_PHP_VERSION" == '5.6' ]]; then composer require satooshi/php-coveralls:dev-master -n ; fi
17+
- if [[ "$TRAVIS_PHP_VERSION" != '5.6' ]]; then composer install -n ; fi
18+
- if [ "$dependencies" = "lowest" ]; then composer update --prefer-lowest --prefer-stable -n; fi;
19+
20+
script:
21+
- if [[ "$TRAVIS_PHP_VERSION" == '5.6' ]]; then vendor/bin/phpunit --coverage-clover clover.xml ; fi
22+
- if [[ "$TRAVIS_PHP_VERSION" != '5.6' ]]; then vendor/bin/phpunit ; fi
23+
24+
after_script:
25+
- if [[ "$TRAVIS_PHP_VERSION" == '5.6' ]]; then php vendor/bin/coveralls -v ; fi

CONTRIBUTING.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Contributing
2+
3+
First of all, **thank you** for contributing!
4+
5+
Here are a few rules to follow in order to ease code reviews and merging:
6+
7+
- follow [PSR-1](http://www.php-fig.org/psr/1/) and [PSR-2](http://www.php-fig.org/psr/2/)
8+
- run the test suite
9+
- write (or update) unit tests when applicable
10+
- write documentation for new features
11+
- use [commit messages that make sense](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html)
12+
13+
One may ask you to [squash your commits](http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html) too. This is used to "clean" your pull request before merging it (we don't want commits such as `fix tests`, `fix 2`, `fix 3`, etc.).
14+
15+
When creating your pull request on GitHub, please write a description which gives the context and/or explains why you are creating it.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Matthieu Napoli
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# PHP-DI application kernel
2+
3+
Kernel for applications built with [PHP-DI](http://php-di.org) and [Puli](http://puli.io) with built-in support for PHP-DI modules.
4+
5+
[![Build Status](https://img.shields.io/travis/PHP-DI/Kernel.svg?style=flat-square)](https://travis-ci.org/PHP-DI/Kernel)
6+
[![Coverage Status](https://img.shields.io/coveralls/PHP-DI/Kernel/master.svg?style=flat-square)](https://coveralls.io/r/PHP-DI/Kernel?branch=master)
7+
8+
## Introduction
9+
10+
TODO
11+
12+
## Installation
13+
14+
```
15+
composer require php-di/kernel
16+
```
17+
18+
Requirements:
19+
20+
- PHP 5.5 or greater
21+
- [Puli CLI tool](http://docs.puli.io/en/latest/installation.html#installing-the-puli-cli)
22+
23+
## Usage
24+
25+
TODO

composer.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "php-di/kernel",
3+
"description": "Kernel for PHP-DI applications",
4+
"keywords": ["php-di", "application", "kernel"],
5+
"homepage": "http://php-di.org",
6+
"license": "MIT",
7+
"type": "library",
8+
"autoload": {
9+
"psr-4": {
10+
"DI\\Kernel\\": "src/"
11+
}
12+
},
13+
"autoload-dev": {
14+
"psr-4": {
15+
"DI\\Kernel\\Test\\": "tests/"
16+
}
17+
},
18+
"require": {
19+
"php": "^5.5|^7.0",
20+
"php-di/php-di": "^5.2",
21+
"doctrine/cache": "^1.4",
22+
"puli/repository": "^1.0.0-beta8@beta",
23+
"puli/discovery": "^1.0.0-beta8@beta"
24+
},
25+
"require-dev": {
26+
"phpunit/phpunit": "^5.0"
27+
}
28+
}

phpunit.xml.dist

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<phpunit colors="true"
3+
convertErrorsToExceptions="true"
4+
convertNoticesToExceptions="true"
5+
convertWarningsToExceptions="true"
6+
bootstrap="./vendor/autoload.php">
7+
8+
<testsuites>
9+
<testsuite name="Test suite">
10+
<directory>./tests/</directory>
11+
</testsuite>
12+
</testsuites>
13+
14+
<filter>
15+
<whitelist processUncoveredFilesFromWhitelist="true">
16+
<directory suffix=".php">src</directory>
17+
</whitelist>
18+
</filter>
19+
20+
</phpunit>

puli.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"version": "1.0",
3+
"binding-types": {
4+
"php-di/configuration": {
5+
"description": "PHP-DI configuration file"
6+
}
7+
}
8+
}

src/Kernel.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace DI\Kernel;
4+
5+
use DI\Cache\ArrayCache;
6+
use DI\Container;
7+
use DI\ContainerBuilder;
8+
use Doctrine\Common\Cache\Cache;
9+
use Puli\Discovery\Api\Binding\Binding;
10+
use Puli\Discovery\Api\Discovery;
11+
use Puli\Discovery\Binding\ResourceBinding;
12+
use Puli\Repository\Api\ResourceRepository;
13+
use Puli\Repository\Resource\FileResource;
14+
15+
/**
16+
* Application kernel.
17+
*
18+
* @author Matthieu Napoli <matthieu@mnapoli.fr>
19+
*/
20+
class Kernel
21+
{
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+
29+
/**
30+
* Configure and create a container using all configuration files registered under
31+
* the `php-di/configuration` binding type in Puli.
32+
*
33+
* @return Container
34+
*/
35+
public function createContainer()
36+
{
37+
if (!defined('PULI_FACTORY_CLASS')) {
38+
throw new \RuntimeException('Puli is not installed');
39+
}
40+
41+
// Create Puli objects
42+
$factoryClass = PULI_FACTORY_CLASS;
43+
$factory = new $factoryClass();
44+
/** @var ResourceRepository $repository */
45+
$repository = $factory->createRepository();
46+
/** @var Discovery $discovery */
47+
$discovery = $factory->createDiscovery($repository);
48+
49+
$containerBuilder = new ContainerBuilder();
50+
51+
$cache = $this->getContainerCache();
52+
if ($cache) {
53+
$containerBuilder->setDefinitionCache($cache);
54+
}
55+
56+
// Discover and load all configuration files registered under `php-di/configuration` in Puli
57+
$bindings = $discovery->findBindings(self::PULI_BINDING_NAME);
58+
$bindings = array_filter($bindings, function (Binding $binding) {
59+
return $binding instanceof ResourceBinding;
60+
});
61+
/** @var ResourceBinding[] $bindings */
62+
foreach ($bindings as $binding) {
63+
foreach ($binding->getResources() as $resource) {
64+
if (!$resource instanceof FileResource) {
65+
throw new \RuntimeException(sprintf('Cannot load "%s": only file resources are supported', $resource->getName()));
66+
}
67+
$containerBuilder->addDefinitions($resource->getFilesystemPath());
68+
}
69+
}
70+
71+
// Puli objects
72+
$containerBuilder->addDefinitions([
73+
ResourceRepository::class => $repository,
74+
Discovery::class => $discovery,
75+
]);
76+
77+
$this->configureContainerBuilder($containerBuilder);
78+
79+
return $containerBuilder->build();
80+
}
81+
82+
/**
83+
* Override this method to configure the cache to use for container definitions.
84+
*
85+
* @return Cache|null
86+
*/
87+
protected function getContainerCache()
88+
{
89+
return new ArrayCache();
90+
}
91+
92+
/**
93+
* Override this method to customize the container builder before it is used.
94+
*/
95+
protected function configureContainerBuilder(ContainerBuilder $containerBuilder)
96+
{
97+
}
98+
}

tests/Fixture/PuliFactoryClass.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace DI\Kernel\Test\Fixture;
4+
5+
use Puli\Discovery\InMemoryDiscovery;
6+
use Puli\Repository\InMemoryRepository;
7+
8+
class PuliFactoryClass
9+
{
10+
/**
11+
* @var InMemoryRepository
12+
*/
13+
public static $repository;
14+
15+
/**
16+
* @var InMemoryDiscovery
17+
*/
18+
public static $discovery;
19+
20+
public function createRepository()
21+
{
22+
return self::$repository;
23+
}
24+
25+
public function createDiscovery()
26+
{
27+
return self::$discovery;
28+
}
29+
}

tests/Fixture/config.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
'foo' => 'bar',
5+
];

tests/KernelTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace DI\Kernel\Test;
4+
5+
use DI\Kernel\Kernel;
6+
use DI\Kernel\Test\Fixture\PuliFactoryClass;
7+
use Puli\Discovery\Api\Discovery;
8+
use Puli\Discovery\Api\Type\BindingType;
9+
use Puli\Discovery\Binding\ResourceBinding;
10+
use Puli\Discovery\InMemoryDiscovery;
11+
use Puli\Repository\Api\ResourceRepository;
12+
use Puli\Repository\InMemoryRepository;
13+
use Puli\Repository\Resource\FileResource;
14+
15+
/**
16+
* @covers \DI\Application\Kernel
17+
*/
18+
class KernelTest extends \PHPUnit_Framework_TestCase
19+
{
20+
public function setUp()
21+
{
22+
if (!defined('PULI_FACTORY_CLASS')) {
23+
define('PULI_FACTORY_CLASS', PuliFactoryClass::class);
24+
}
25+
26+
PuliFactoryClass::$repository = new InMemoryRepository();
27+
PuliFactoryClass::$discovery = new InMemoryDiscovery();
28+
}
29+
30+
/**
31+
* @test
32+
*/
33+
public function creates_a_container()
34+
{
35+
$this->assertInstanceOf('DI\Container', (new Kernel)->createContainer());
36+
}
37+
38+
/**
39+
* @test
40+
*/
41+
public function registers_puli_repository()
42+
{
43+
$container = (new Kernel)->createContainer();
44+
$this->assertInstanceOf(ResourceRepository::class, $container->get(ResourceRepository::class));
45+
}
46+
47+
/**
48+
* @test
49+
*/
50+
public function registers_puli_discovery()
51+
{
52+
$container = (new Kernel)->createContainer();
53+
$this->assertInstanceOf(Discovery::class, $container->get(Discovery::class));
54+
}
55+
56+
/**
57+
* @test
58+
*/
59+
public function registers_module_configuration_files()
60+
{
61+
$this->createPuliResource('/blog/config.php', __DIR__ . '/Fixture/config.php');
62+
$this->bindPuliResource('/blog/config.php', Kernel::PULI_BINDING_NAME);
63+
64+
$container = (new Kernel)->createContainer();
65+
$this->assertEquals('bar', $container->get('foo'));
66+
}
67+
68+
private function createPuliResource($path, $file)
69+
{
70+
PuliFactoryClass::$repository->add($path, new FileResource($file));
71+
}
72+
73+
private function bindPuliResource($path, $bindingName)
74+
{
75+
PuliFactoryClass::$discovery->addBindingType(new BindingType($bindingName));
76+
77+
$binding = new ResourceBinding($path, $bindingName);
78+
$binding->setRepository(PuliFactoryClass::$repository);
79+
PuliFactoryClass::$discovery->addBinding($binding);
80+
}
81+
}

0 commit comments

Comments
 (0)