Skip to content

Commit

Permalink
✨ Support writing proxy classes to files #49
Browse files Browse the repository at this point in the history
  • Loading branch information
Elie NEHME committed Dec 15, 2021
1 parent 0a5e4c8 commit 5d92acd
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## V8.0.1 - TBD

### Added

- [#49](https://github.com/elie29/zend-di-config/issues/49) Support writing proxy classes to files

### Changed

- Nothing.

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- Nothing.

## V8.0.0 - 2021-03-23

- V8.0+ supports only PHP8+ and drops other versions
Expand Down
35 changes: 23 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![Coverage Status](https://coveralls.io/repos/github/elie29/zend-di-config/badge.svg)](https://coveralls.io/github/elie29/zend-di-config)

## Introduction

[zend-phpdi-config](https://packagist.org/packages/elie29/zend-phpdi-config) acts as a bridge to configure a PSR-11 compatible [PHP-DI](http://php-di.org) container using service manager configuration.
It can be used with [Laminas](https://getlaminas.org/) and [Mezzio](https://docs.mezzio.dev/) starting from v6.0.0

Expand Down Expand Up @@ -39,6 +40,9 @@ $container = $factory(
// Enable compilation
Config::DI_CACHE_PATH => __DIR__, // Folder path

// Write proxy to file : cf. https://php-di.org/doc/lazy-injection.html
Config::DI_PROXY_PATH => __DIR__, // Folder path

// Disable autowire (enabled by default)
Config::USE_AUTOWIRE => false

Expand Down Expand Up @@ -69,23 +73,25 @@ The `dependencies` sub associative array can contain the following keys:
[Expressive delegators documentation](https://docs.laminas.dev/laminas-servicemanager/delegators/)
for more details.

>**N.B.:** The whole configuration -- unless `dependencies` -- is merged in a `config` key within the `$container`:
> **N.B.:** The whole configuration -- unless `dependencies` -- is merged in a `config` key within the `$container`:
>
>```php
>$config = $container->get('config');
>```
> ```php
> $config = $container->get('config');
> ```
## CLI command to add a new autowire entry

![Configuration image](./config-add-entry.png)

The cli command `add-autowires-entry` creates the configuration file if it doesn't exist otherwise it adds the entry
to the autowires key.

Example of adding ConsoleHelper to a config.php:
>```console
>./vendor/bin/add-autowires-entry config.php "Laminas\\Stdlib\\ConsoleHelper"
>[DONE] Changes written to config.php
>```

> ```console
> ./vendor/bin/add-autowires-entry config.php "Laminas\\Stdlib\\ConsoleHelper"
> [DONE] Changes written to config.php
> ```
## Using with Expressive
Expand Down Expand Up @@ -113,6 +119,7 @@ return call_user_func(function () {
```
## Example of a ConfigProvider class

```php
<?php

Expand Down Expand Up @@ -144,6 +151,7 @@ class ConfigProvider
```

Where UserManager depends on Mailer as follow:

```php
class UserManager
{
Expand Down Expand Up @@ -172,12 +180,15 @@ class Mailer
## Switching back to another container

To switch back to another container is very easy:
1. Create your factories with `__invoke` function
2. Replace `autowires` key in ConfigProvider by `factories` key, then for each class name attach its correspondent factory.

1. Create your factories with `__invoke` function
2. Replace `autowires` key in ConfigProvider by `factories` key, then for each class name attach its correspondent factory.

## PSR 11 and Interop\Container\ContainerInterface

V4.x supports as well Interop\Container\ContainerInterface

## Migration guides
- [Migration from 3.x to 4.0](docs/migration-4.0.md)
- Migration from 4.x to 5.0: container-interop/container-interop was dropped in favor of [PSR-11](https://packagist.org/packages/psr/container).

- [Migration from 3.x to 4.0](docs/migration-4.0.md)
- Migration from 4.x to 5.0: container-interop/container-interop was dropped in favor of [PSR-11](https://packagist.org/packages/psr/container).
10 changes: 10 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public function __construct(array $config)

public function configureContainer(ContainerBuilder $builder): void
{
$this->shouldWriteProxyToFile($builder);

if (! $this->enableCompilation($builder)) {
$this->setDependencies();
$this->addDefinitions($builder);
Expand Down Expand Up @@ -162,6 +164,14 @@ private function enableCache(ContainerBuilder $builder): void
}
}

private function shouldWriteProxyToFile(ContainerBuilder $builder): void
{
$path = $this->definitions[self::CONFIG][self::DI_PROXY_PATH] ?? null;
if ($path) {
$builder->writeProxiesToFile(true, $path);
}
}

private function get(string $key): array
{
if (! isset($this->dependencies[$key]) || ! is_array($this->dependencies[$key])) {
Expand Down
1 change: 1 addition & 0 deletions src/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface ConfigInterface
public const DI_CACHE_PATH = 'di_cache_path';
public const ENABLE_CACHE_DEFINITION = 'enable_cache_definition';
public const USE_AUTOWIRE = 'use_autowire';
public const DI_PROXY_PATH = 'di_proxy_path';

public function configureContainer(ContainerBuilder $builder): void;
}

0 comments on commit 5d92acd

Please sign in to comment.