Skip to content

Commit

Permalink
Merge pull request #17 from aimfeld/laminas
Browse files Browse the repository at this point in the history
Laminas
  • Loading branch information
aimfeld authored Jan 10, 2020
2 parents 8f0aeb0 + 74fdca1 commit 5714254
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 87 deletions.
18 changes: 9 additions & 9 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

namespace ZendDiCompiler;

use Zend\Mvc\MvcEvent;
use Zend\Config\Config;
use Zend\EventManager\EventInterface as Event;
use Zend\ModuleManager\ModuleManager;
use Zend\ServiceManager\ServiceManager;
use Laminas\Mvc\MvcEvent;
use Laminas\Config\Config;
use Laminas\EventManager\EventInterface as Event;
use Laminas\ModuleManager\ModuleManager;
use Laminas\ServiceManager\ServiceManager;

/**
* @package ZendDiCompiler
Expand All @@ -38,7 +38,7 @@ class Module
public function getAutoloaderConfig()
{
return [
'Zend\Loader\StandardAutoloader' => [
'Laminas\Loader\StandardAutoloader' => [
'namespaces' => [
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
]
Expand All @@ -63,7 +63,7 @@ public function getServiceConfig()
$this->zendDiCompiler = new ZendDiCompiler;

return [
// Provide ZendDiCompiler as a Zend\ServiceManager service.
// Provide ZendDiCompiler as a Laminas\ServiceManager service.
'services' => [
'ZendDiCompiler' => $this->zendDiCompiler,
]
Expand Down Expand Up @@ -93,15 +93,15 @@ public function modulesLoaded(Event $e)
$this->config = new Config($serviceManager->get('config'));
$this->zendDiCompiler->setConfig($this->config);

// If Zend\Mvc is not used, the onBootstrap event won't be called and no mvc-related
// If Laminas\Mvc is not used, the onBootstrap event won't be called and no mvc-related
// shared instances will be added.
if (!$this->config->get('zendDiCompiler')->useZendMvc) {
$this->zendDiCompiler->init();
}
}

/**
* If Zend\Mvc is used, this function will be called and mvc-related shared instances will be provided
* If Laminas\Mvc is used, this function will be called and mvc-related shared instances will be provided
*
* @param MvcEvent $mvcEvent
*/
Expand Down
52 changes: 26 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@

# Introduction

Are you tired of writing tons of factory code (closures) for the Zend\ServiceManager in your Zend Framework 2 application?
Are you tired of writing tons of factory code (closures) for the Laminas\ServiceManager in your Zend Framework 2 application?
Are outdated factory methods causing bugs? This can all be avoided by using ZendDiCompiler!

**ZendDiCompiler** is a Zend Framework 2 module that uses auto-generated factory code for dependency-injection.
It saves you a lot of work, since there's **no need anymore for writing
Zend\ServiceManager factory closures** and keeping them up-to-date manually.
Laminas\ServiceManager factory closures** and keeping them up-to-date manually.

ZendDiCompiler scans your code using **Zend\Di** and creates factory methods automatically. If the factory methods are outdated, ZendDiCompiler
ZendDiCompiler scans your code using **Laminas\Di** and creates factory methods automatically. If the factory methods are outdated, ZendDiCompiler
updates them in the background. Therefore, you **develop faster**, **avoid bugs** due to outdated factory methods, and
experience **great performance** in production!

Expand All @@ -37,16 +37,16 @@ experience **great performance** in production!
- **Code scanning** for creating DI definitions and **automatic factory code generation**.
- Can deal with **shared instances** and **type preferences**.
- Allows for **custom code introspection strategies** (by default, only constructors are scanned).
- Can be used as a **complement to Zend\ServiceManager**.
- Can be used as a **complement to Laminas\ServiceManager**.
- Detection of outdated generated factory code and **automatic rescanning** (great for development).
- Can create new instances or reuse instances created before.
- Can be used as a **factory for runtime objects** combining DI and passing of runtime parameters.
- **Greater perfomance** and less memory consumption, as compared to using Zend\Di\Di with cached definitions.
- **Greater perfomance** and less memory consumption, as compared to using Laminas\Di\Di with cached definitions.

# Caveats

- [Setter injection and interface injection](http://framework.zend.com/manual/current/en/tutorials/quickstart.di.html) are not supported yet. Instances must be injected via constructor injection (which I recommend over the two other methods anyway).
- Using ZendDiCompiler makes sense if you develop a large application or a framework. For smaller applications, ZendDiCompiler may be overkill and you should handle instantiation using Zend\ServiceManager callback methods.
- Using ZendDiCompiler makes sense if you develop a large application or a framework. For smaller applications, ZendDiCompiler may be overkill and you should handle instantiation using Laminas\ServiceManager callback methods.

# Installation

Expand Down Expand Up @@ -98,7 +98,7 @@ As soon as you inject the ZendDiCompiler itself into your controllers and other
The ZF2 MVC architecture is based on controller classes with action methods. Given this architecture, controller dependencies become
numerous very quickly. In order to avoid bloated controller constructors, it makes sense to inject ZendDiCompiler as a
single dependency into ZF2 controller classes and use it to pull the other dependencies from inside the controllers.
This means using it as a _service locator_, just like `Zend\ServiceManager` is typically used.
This means using it as a _service locator_, just like `Laminas\ServiceManager` is typically used.

ZendDiCompiler is also used as a _service locator_ inside of the provided `ZendDiCompiler\DiFactory` which is very useful for
[creating runtime objects with dependencies](#using-the-difactory-to-create-runtime-objects-with-dependencies). This
Expand All @@ -110,7 +110,7 @@ DiFactory or your extended version of it with [custom creation methods](#passing

## Configuration

ZendDiCompiler uses standard [Zend\Di configuration](http://framework.zend.com/manual/2.1/en/modules/zend.di.configuration.html)
ZendDiCompiler uses standard [Laminas\Di configuration](http://framework.zend.com/manual/2.1/en/modules/zend.di.configuration.html)
(which is not well documented yet). To make things easier, see [example.config.php](https://github.com/aimfeld/ZendDiCompiler/blob/master/config/example.config.php) for
examples of how to specify:

Expand Down Expand Up @@ -139,13 +139,13 @@ The following _default shared instances_ can be constructor-injected without exp

- `ZendDiCompiler\ZendDiCompiler`
- `ZendDiCompiler\DiFactory`
- `Zend\Mvc\MvcEvent`
- `Zend\Config\Config`
- `Zend\View\Renderer\PhpRenderer`
- `Zend\Mvc\ApplicationInterface`
- `Zend\ServiceManager\ServiceLocatorInterface`
- `Zend\EventManager\EventManagerInterface`
- `Zend\Mvc\Router\RouteStackInterface`
- `Laminas\Mvc\MvcEvent`
- `Laminas\Config\Config`
- `Laminas\View\Renderer\PhpRenderer`
- `Laminas\Mvc\ApplicationInterface`
- `Laminas\ServiceManager\ServiceLocatorInterface`
- `Laminas\EventManager\EventManagerInterface`
- `Laminas\Mvc\Router\RouteStackInterface`


## Type preferences
Expand Down Expand Up @@ -214,9 +214,9 @@ In our example, we have the following classes:
ExampleController

```php
use Zend\Mvc\Controller\AbstractActionController;
use Laminas\Mvc\Controller\AbstractActionController;
use ZendDiCompiler\ZendDiCompiler;
use Zend\Config\Config;
use Laminas\Config\Config;

class ExampleController extends AbstractActionController
{
Expand Down Expand Up @@ -346,7 +346,7 @@ class Module
## Using the DiFactory to create runtime objects with dependencies

It is useful to distinguish two types of objects: _services_ and _runtime objects_. For _services_, all parameters should
be specified in the configuration (e.g. a config array wrapped in a `Zend\Config\Config` object). If class constructors
be specified in the configuration (e.g. a config array wrapped in a `Laminas\Config\Config` object). If class constructors
e.g. in third party code require some custom parameters, they can be specified in the
[instance configuration](https://github.com/aimfeld/ZendDiCompiler/blob/master/config/example.config.php)).

Expand Down Expand Up @@ -424,7 +424,7 @@ class ExampleDiFactory extends DiFactory
*/
public function createRuntimeB($runtimeParam1, $runtimeParam2)
{
$config = $this->zendDiCompiler->get('Zend\Config\Config');
$config = $this->zendDiCompiler->get('Laminas\Config\Config');
$serviceA = $this->zendDiCompiler->get('ZendDiCompiler\Example\ServiceA');
return new RuntimeB($config, $serviceA, $runtimeParam1, $runtimeParam2);
}
Expand Down Expand Up @@ -463,7 +463,7 @@ Just for illustration, this is the generated service locator created by ZendDiCo
```php
namespace ZendDiCompiler;

use Zend\Di\ServiceLocator;
use Laminas\Di\ServiceLocator;

/**
* Generated by ZendDiCompiler\Generator (2013-03-07 21:11:39)
Expand Down Expand Up @@ -532,7 +532,7 @@ class GeneratedServiceLocator extends ServiceLocator
return $this->services['ZendDiCompiler\Example\ExampleController'];
}

$object = new \ZendDiCompiler\Example\ExampleController($this->get('ZendDiCompiler\ZendDiCompiler'), $this->getZendDiCompilerExampleServiceA(), $this->getZendDiCompilerExampleServiceC(), $this->get('Zend\Config\Config'));
$object = new \ZendDiCompiler\Example\ExampleController($this->get('ZendDiCompiler\ZendDiCompiler'), $this->getZendDiCompilerExampleServiceA(), $this->getZendDiCompilerExampleServiceC(), $this->get('Laminas\Config\Config'));
if (!$newInstance) {
$this->services['ZendDiCompiler\Example\ExampleController'] = $object;
}
Expand Down Expand Up @@ -608,7 +608,7 @@ class GeneratedServiceLocator extends ServiceLocator
return $this->services['ZendDiCompiler\Example\RuntimeA'];
}

$object = new \ZendDiCompiler\Example\RuntimeA($this->get('Zend\Config\Config'), $this->getZendDiCompilerExampleServiceA(), $params);
$object = new \ZendDiCompiler\Example\RuntimeA($this->get('Laminas\Config\Config'), $this->getZendDiCompilerExampleServiceA(), $params);
if (!$newInstance) {
$this->services['ZendDiCompiler\Example\RuntimeA'] = $object;
}
Expand Down Expand Up @@ -734,10 +734,10 @@ class GeneratedServiceLocator extends ServiceLocator

## Code scan log

ZendDiCompiler logs problems found during code scanning in `data/ZendDiCompiler/code-scan.log`. If you can't retrieve an object from ZendDiCompiler, you will probably find the reason in this log. The most common problem is that you have untyped scalar parameters instead of a [parameter array](#passing-all-runtime-parameters-in-a-single-array) in your constructors without providing values in your [Zend\Di configuration](http://framework.zend.com/manual/current/en/modules/zend.di.configuration.html). Here's an example of the code scan log showing some problems:
ZendDiCompiler logs problems found during code scanning in `data/ZendDiCompiler/code-scan.log`. If you can't retrieve an object from ZendDiCompiler, you will probably find the reason in this log. The most common problem is that you have untyped scalar parameters instead of a [parameter array](#passing-all-runtime-parameters-in-a-single-array) in your constructors without providing values in your [Laminas\Di configuration](http://framework.zend.com/manual/current/en/modules/zend.di.configuration.html). Here's an example of the code scan log showing some problems:
```
INFO (6): Start generating service locator by code scanning.
DEBUG (7): Survey\Cache\Zf1CacheAdapter: Class Zend\Cache\Storage\StorageInterface could not be located in provided definitions.
DEBUG (7): Survey\Cache\Zf1CacheAdapter: Class Laminas\Cache\Storage\StorageInterface could not be located in provided definitions.
DEBUG (7): Survey\DataAggregator\Aggregate: Missing instance/object for parameter data for Survey\DataAggregator\Aggregate::__construct
DEBUG (7): Survey\Db\Table\Rowset: Missing instance/object for parameter config for Survey\Db\Table\Rowset::__construct
DEBUG (7): Survey\DbValidate\ValidationResult: Missing instance/object for parameter errorCount for Survey\DbValidate\ValidationResult::__construct
Expand All @@ -761,7 +761,7 @@ In case of simple [value objects](http://martinfowler.com/bliki/ValueObject.html
As a bonus, ZendDiCompiler will write a `component-dependency-info.txt` file containing information about
which of the scanned components depend on which classes.

Scanned classes are grouped into components (e.g. the Zend\Mvc\MvcEvent class belongs to the Zend\Mvc component).
Scanned classes are grouped into components (e.g. the Laminas\Mvc\MvcEvent class belongs to the Laminas\Mvc component).
For every component, all constructor-injected classes are listed. This helps you analyze which components
depend on which classes of other components. Consider organizing your components into layers.
Each layer should depend on classes of the same or lower layers only.
Expand All @@ -773,7 +773,7 @@ Here's an example of what you might see:
MyLibrary\Mail classes inject:
- MyLibrary\Mail\Transport
- MyLibrary\TextEngine\TextEngine
- Zend\Config\Config
- Laminas\Config\Config
MyLibrary\Validator classes inject:
- MyLibrary\Db\Tables
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"keywords": ["dependency injection", "dependency injection container", "DIC", "zend framework", "service manager", "zf2"],
"homepage": "https://github.com/aimfeld/ZendDiCompiler",
"type": "library",
"license": "New BSD License",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Adrian Imfeld",
Expand All @@ -14,9 +14,9 @@
],
"require": {
"php": ">=7.0",
"zendframework/zend-config": ">=2.6",
"zendframework/zend-di": ">=2.6",
"zendframework/zend-servicemanager": "^3.0"
"laminas/laminas-config": ">=2.6",
"laminas/laminas-di": ">=2.6",
"laminas/laminas-servicemanager": "^3.0"
},
"replace": {
"aimfeld/di-wrapper": "*"
Expand Down
2 changes: 1 addition & 1 deletion config/example.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Override the DI configuration in your project config.
*
* For info on the structure of the 'instance' and the 'preference' array,
* see Zend\Di documentation.
* see Laminas\Di documentation.
*/
return [
// ZendDiCompiler configuration
Expand Down
4 changes: 2 additions & 2 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
* Override the DI configuration in your project config.
*
* For an example config, see example.config.php. For info on the structure of
* the 'instance' and the 'preference' array, see Zend\Di documentation.
* the 'instance' and the 'preference' array, see Laminas\Di documentation.
*/
return [
// ZendDiCompiler configuration
'zendDiCompiler' => [
// Set to false, if your application does not use Zend\Mvc and the onBootstrap event is therefore not called.
// Set to false, if your application does not use Laminas\Mvc and the onBootstrap event is therefore not called.
'useZendMvc' => true,

// Directories that will be code-scanned
Expand Down
4 changes: 2 additions & 2 deletions src/ZendDiCompiler/Example/ExampleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

namespace ZendDiCompiler\Example;

use Zend\Mvc\Controller\AbstractActionController;
use Laminas\Mvc\Controller\AbstractActionController;
use ZendDiCompiler\ZendDiCompiler;
use Zend\Config\Config;
use Laminas\Config\Config;

/**
* @package ZendDiCompiler
Expand Down
2 changes: 1 addition & 1 deletion src/ZendDiCompiler/Example/ExampleDiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ExampleDiFactory extends DiFactory
*/
public function createRuntimeB($runtimeParam1, $runtimeParam2)
{
$config = $this->zendDiCompiler->get('Zend\Config\Config');
$config = $this->zendDiCompiler->get('Laminas\Config\Config');
$serviceA = $this->zendDiCompiler->get('ZendDiCompiler\Example\ServiceA');
return new RuntimeB($config, $serviceA, $runtimeParam1, $runtimeParam2);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ZendDiCompiler/Example/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace ZendDiCompiler\Example;

use Zend\Mvc\MvcEvent;
use Laminas\Mvc\MvcEvent;
use ZendDiCompiler\ZendDiCompiler;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/ZendDiCompiler/Example/RuntimeA.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace ZendDiCompiler\Example;

use Zend\Config\Config;
use Laminas\Config\Config;

/**
* @package ZendDiCompiler
Expand Down
2 changes: 1 addition & 1 deletion src/ZendDiCompiler/Example/RuntimeB.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace ZendDiCompiler\Example;

use Zend\Config\Config;
use Laminas\Config\Config;

/**
* @package ZendDiCompiler
Expand Down
2 changes: 1 addition & 1 deletion src/ZendDiCompiler/Example/ServiceC.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace ZendDiCompiler\Example;

use Zend\Mvc\MvcEvent;
use Laminas\Mvc\MvcEvent;

/**
* @package ZendDiCompiler
Expand Down
30 changes: 15 additions & 15 deletions src/ZendDiCompiler/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@

namespace ZendDiCompiler;

use Zend\Code\Generator\DocBlockGenerator;
use Zend\Code\Generator\ValueGenerator;
use Zend\Di\Di;
use Zend\Di\InstanceManager;
use Zend\Di\ServiceLocator;
use Zend\Di\ServiceLocator\GeneratorInstance;
use Zend\Code\Generator\MethodGenerator;
use Zend\Code\Generator\ParameterGenerator;
use Zend\Code\Generator\ClassGenerator;
use Zend\Code\Generator\FileGenerator;
use Zend\Code\Generator\PropertyGenerator;
use Zend\Config\Config;
use Zend\Log\Logger;
use Laminas\Code\Generator\DocBlockGenerator;
use Laminas\Code\Generator\ValueGenerator;
use Laminas\Di\Di;
use Laminas\Di\InstanceManager;
use Laminas\Di\ServiceLocator;
use Laminas\Di\ServiceLocator\GeneratorInstance;
use Laminas\Code\Generator\MethodGenerator;
use Laminas\Code\Generator\ParameterGenerator;
use Laminas\Code\Generator\ClassGenerator;
use Laminas\Code\Generator\FileGenerator;
use Laminas\Code\Generator\PropertyGenerator;
use Laminas\Config\Config;
use Laminas\Log\Logger;
use ZendDiCompiler\Exception\RuntimeException;
use DateTime;
use ReflectionClass;

/**
* @package ZendDiCompiler
*/
class Generator extends \Zend\Di\ServiceLocator\Generator
class Generator extends \Laminas\Di\ServiceLocator\Generator
{
/**
* Support for passing a $params array for instance creation
Expand Down Expand Up @@ -71,7 +71,7 @@ public function __construct(Di $injector, Config $config, Logger $logger)
/**
* Construct, configure, and return a PHP class file code generation object
*
* Creates a Zend\Code\Generator\FileGenerator object that has
* Creates a Laminas\Code\Generator\FileGenerator object that has
* created the specified class and service locator methods.
*
* @param null|string $filename
Expand Down
Loading

0 comments on commit 5714254

Please sign in to comment.