Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Oct 14, 2018
0 parents commit e271b17
Show file tree
Hide file tree
Showing 32 changed files with 1,673 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
root = true

[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 4

[*.yml]
indent_style = space
indent_size = 2

[*.md]
indent_style = space
indent_size = 4

[Makefile]
indent_style = tab
indent_size = 4
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build
composer.lock
vendor
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
sudo: false

cache:
directories:
- $COMPOSER_CACHE_DIR
- $HOME/.composer/cache
- $TRAVIS_BUILD_DIR/build

language: php

php:
- 7.1
- 7.2

before_script:
- if [[ $TRAVIS_PHP_VERSION != "7.1" ]]; then phpenv config-rm xdebug.ini; fi

script:
- if [[ $TRAVIS_PHP_VERSION == "7.1" ]]; then make test-coveralls; else make test; fi
24 changes: 24 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM php:7.1-alpine3.8

RUN apk add --no-cache make $PHPIZE_DEPS \
&& pecl install xdebug \
&& docker-php-ext-enable xdebug

RUN echo $'\
xdebug.coverage_enable=1\n\
xdebug.default_enable=1\n\
' >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

ENV COMPOSER_ALLOW_SUPERUSER 1

RUN curl -o /tmp/composer-setup.php https://getcomposer.org/installer && \
curl -o /tmp/composer-setup.sig https://composer.github.io/installer.sig && \
php -r "if (hash('SHA384', file_get_contents('/tmp/composer-setup.php')) !== trim(file_get_contents('/tmp/composer-setup.sig'))) { unlink('/tmp/composer-setup.php'); echo 'Invalid installer' . PHP_EOL; exit(1); }" && \
php /tmp/composer-setup.php && \
mv composer.phar /usr/local/bin/composer

RUN wget -O phpunit https://phar.phpunit.de/phpunit-7.phar && \
chmod +x phpunit && \
mv phpunit /usr/local/bin/phpunit

WORKDIR app
30 changes: 30 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
The olvlvl/symfony-dependency-injection-proxy package is free software.
It is released under the terms of the following BSD License.

Copyright (c) 2018 by Olivier Laviale
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of Olivier Laviale nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
PHPUNIT=$(shell which phpunit)

vendor:
@composer install

test: test-setup
@php -d xdebug.coverage_enable=0 $(PHPUNIT)

test-coverage: test-setup
@mkdir -p build/coverage
@$(PHPUNIT) --coverage-html build/coverage

test-coveralls: test-setup
@mkdir -p build/logs
composer require satooshi/php-coveralls '^2.0'
@$(PHPUNIT) --coverage-clover build/logs/clover.xml
php vendor/bin/php-coveralls -v

test-container:
@docker-compose run --rm app sh
@docker-compose down

test-setup: vendor
@rm -f tests/sandbox/*

.PHONY: all test test-container test-coverage test-coveralls test-setup
208 changes: 208 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
# Proxy generator for Symfony's DIC

[![Release](https://img.shields.io/packagist/v/olvlvl/symfony-dependency-injection-proxy.svg)](https://packagist.org/packages/olvlvl/symfony-dependency-injection-proxy)
[![Build Status](https://img.shields.io/travis/olvlvl/symfony-dependency-injection-proxy.svg)](http://travis-ci.org/olvlvl/symfony-dependency-injection-proxy)
[![Code Quality](https://img.shields.io/scrutinizer/g/olvlvl/symfony-dependency-injection-proxy.svg)](https://scrutinizer-ci.com/g/olvlvl/symfony-dependency-injection-proxy)
[![Code Coverage](https://img.shields.io/coveralls/olvlvl/symfony-dependency-injection-proxy.svg)](https://coveralls.io/r/olvlvl/symfony-dependency-injection-proxy)
[![Packagist](https://img.shields.io/packagist/dt/olvlvl/symfony-dependency-injection-proxy.svg)](https://packagist.org/packages/olvlvl/symfony-dependency-injection-proxy)

This package provides a proxy generator for [Symfony's dependency injection component][1] that generates super tiny,
super simple proxies, especially when [compared to Symphony's default implementation][2].

> If you're not familiar with proxy services, better have a look at [Symfony's documentation][3] before going any
> further.
The generator works with the following assumptions: the service we want to proxy implements an interface and services
using that service expect that interface too. Pretty normal stuff. Consider the following code, where an
`ExceptionHandler` service requires a logger implementing `LogInterface`:

```php
<?php

use Psr\Log\LoggerInterface;

class ExceptionHandler
{
private $logger;

public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}

// …
}
```

Now imagine we're using [Monolog](https://github.com/Seldaek/monolog) as a logger, and we have an expansive stream to
setup. Why waste time building that logger for every request when it's seldom used? That's when we mark our service as
_lazy_.

The following example demonstrates how we can mark our `Psr\Log\LoggerInterface` service as lazy (we could use PHP code
or XML just the same):

```yaml
services:

Psr\Log\LoggerInterface:
class: Monolog\Logger
lazy: true
#
```

The service can also use a factory:

```yaml
services:

Psr\Log\LoggerInterface:
factory: 'LoggerFactory::build'
lazy: true
#
```

> We don't have to define our service with a class, we could use `logger` instead of `Psr\Log\LoggerInterface` just
> the same, except we would have to define `class` for the factory one.
Now let's see how to build our container.

## Building the dependency injection container

The following code demonstrates how to build, compile, and dump a container:

```php
<?php

use olvlvl\SymfonyDependencyInjectionProxy\FactoryRenderer;
use olvlvl\SymfonyDependencyInjectionProxy\InterfaceResolver\BasicInterfaceResolver;
use olvlvl\SymfonyDependencyInjectionProxy\MethodRenderer;
use olvlvl\SymfonyDependencyInjectionProxy\ProxyDumper;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;

$builder = new ContainerBuilder();

// …
// Here we load our config, or build the container using clever PHP calls.
// We might event have some compile passes to add.
// …

$builder->compile();

$dumper = new PhpDumper($builder);
$dumper->setProxyDumper(new ProxyDumper(
new BasicInterfaceResolver(),
new FactoryRenderer(new MethodRenderer)
));

/* @var string $containerFile */

file_put_contents($containerFile, $dumper->dump());
```

There you have it. We can use our container as usual and everything is awesome and cute.





### What if my lazy service implements multiple interfaces?

The basic interface resolver will have a hard time figuring out which interface to implement if a service implements
many. For instance, if a service was an instance of `ArrayObject` the following exception would be raised:

```
Don't know which interface to choose from for ArrayObject: IteratorAggregate, Traversable, ArrayAccess, Serializable, Countable.
```

We can help by decorating the basic interface resolver with a map, and specify which interface to implement for which
class:

```php
<?php

use olvlvl\SymfonyDependencyInjectionProxy\FactoryRenderer;
use olvlvl\SymfonyDependencyInjectionProxy\InterfaceResolver\BasicInterfaceResolver;
use olvlvl\SymfonyDependencyInjectionProxy\InterfaceResolver\MapInterfaceResolver;
use olvlvl\SymfonyDependencyInjectionProxy\MethodRenderer;
use olvlvl\SymfonyDependencyInjectionProxy\ProxyDumper;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;

/* @var PhpDumper $dumper */

$dumper->setProxyDumper(new ProxyDumper(
new MapInterfaceResolver(new BasicInterfaceResolver(), [
ArrayObject::class => ArrayAccess::class,
]),
new FactoryRenderer(new MethodRenderer)
));
```





----------





## Requirements

The package requires PHP 7.1 or later.





## Installation

The recommended way to install this package is through [Composer](http://getcomposer.org/):

$ composer require olvlvl/symfony-dependency-injection-proxy





### Cloning the repository

The package is [available on GitHub](https://github.com/olvlvl/symfony-dependency-injection-proxy),
its repository can be cloned with the following command line:

$ git clone https://github.com/olvlvl/symfony-dependency-injection-proxy.git





## Testing

The test suite is ran with the `make test` command. [PHPUnit](https://phpunit.de/) and
[Composer](http://getcomposer.org/) need to be globally available to run the suite. The command
installs dependencies as required. The `make test-coverage` command runs test suite and also creates
an HTML coverage report in `build/coverage`. If your environment doesn't meet the requirements you can run the tests
with a container, run `make test-container` to create it.

The package is continuously tested by [Travis CI](http://about.travis-ci.org/).

[![Build Status](https://img.shields.io/travis/olvlvl/symfony-dependency-injection-proxy.svg)](http://travis-ci.org/olvlvl/symfony-dependency-injection-proxy)
[![Code Coverage](https://img.shields.io/coveralls/olvlvl/symfony-dependency-injection-proxy.svg)](https://coveralls.io/r/olvlvl/symfony-dependency-injection-proxy)





## License

**olvlvl/symfony-dependency-injection-proxy** is licensed under the New BSD License - See the [LICENSE](LICENSE) file for details.






[1]: https://symfony.com/doc/current/components/dependency_injection.html
[2]: https://github.com/olvlvl/symfony-dependency-injection-proxy/wiki/Comparing-olvlvl's-proxy-generator-with-Symphony's
[3]: https://symfony.com/doc/current/service_container/lazy_services.html
28 changes: 28 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "olvlvl/symfony-dependency-injection-proxy",
"description": "Generate super tiny proxies for Symfony's dependency injection",
"keywords": [ "symfony", "dependency", "injection", "proxy" ],
"type": "library",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Olivier Laviale",
"email": "[email protected]"
}
],
"require": {
"php": ">=7.1",
"ext-json": "*",
"symfony/dependency-injection": "^4.1"
},
"autoload": {
"psr-4": {
"olvlvl\\SymfonyDependencyInjectionProxy\\": "lib"
}
},
"autoload-dev": {
"psr-4": {
"tests\\olvlvl\\SymfonyDependencyInjectionProxy\\": "tests"
}
}
}
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
version: "2.1"
services:
app:
build: .
volumes:
- .:/app:delegated
- ~/.composer:/root/.composer:delegated
Loading

0 comments on commit e271b17

Please sign in to comment.