Skip to main content

Aura.Di 2.x to 3.x Upgrade Guide

·251 words·2 mins
Hari KT
Author
Hari KT
Freelancer | Founder of Tripti and Tanvish | Maintainer of Aura for PHP, created by Paul M Jones

3.x has a very minimal BC break. But if you are not sure what are they, then you may feel the pain. I am trying to document most of them, incase I missed please edit and send a pull request.

I will try to eventually pushed to the main Aura.Di repo.

BC Breaks
#

Instantiation
#

The way di container is instantiated has been changed from

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<?php
use Aura\Di\Container;
use Aura\Di\Factory;
use Aura\Di\ContainerBuilder;

$di = new Container(new Factory);

// or 

$container_builder = new ContainerBuilder();
$di = $container_builder->newInstance(
    array(),
    array(),
    $auto_resolve = false
);

to

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<?php
use Aura\Di\ContainerBuilder;

$container_builder = new ContainerBuilder();

// use the builder to create and configure a container
// using an array of ContainerConfig classes
$di = $container_builder->newConfiguredInstance([
    'Aura\Cli\_Config\Common',
    'Aura\Router\_Config\Common',
    'Aura\Web\_Config\Common',
]);

setter vs setters
#

$di->setter is now $di->setters. Please note there is an additional s in the end. https://github.com/auraphp/Aura.Di/issues/115.

Automatic locking
#

Automatic locking of container once an object is created by container. So make sure everything is lazy call, else you will run something like Cannot modify container when locked.

Config vs ContainerConfig
#

Version 2 Aura\Di\Config is now Aura\Di\ContainerConfig

Features
#

lazyGetCall
#

Example taken from Radar

1
$di->params['Radar\Adr\Handler\RoutingHandler']['matcher'] = $di->lazyGetCall('radar/adr:router', 'getMatcher');

Here the matcher assigned is taken from the RouterContainer getMatcher method.

Instance Factories
#

Create multiple instances of the class. You can read the docs

Related

Speed Up Configuration in Aura v2

·322 words·2 mins
Aura v2 added auto resolution in-order to help lazy people writing configuration manually. Even though it was introduced to help, it introduced a few issues. So auto resolution will be disabled in the future. Some of the complains/suggestions are how to easily write the di configuration. So introducing you FOA.DiConfig Installation # 1 composer require foa/di-config Usage # 1 2 3 vendor/bin/di-config-dump Usage : vendor/bin/di-config-dump /real/path/to/file.php Usage : vendor/bin/di-config-dump /real/path/to/directory Example 1 # Let’s assume you have

Integrating Zend Form in Zend Expressive and Zend View

Example is based using Aura.Di. But the functionality will be same for any containers. First register the service Zend\View\HelperPluginManager, so that we can access the same object. To register the form helpers, create the object of Zend\Form\View\HelperConfig and pass the Zend\View\HelperPluginManager service. Example code with Aura.Di version 3 configuration. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <?php use Aura\Di\ContainerConfig; use Aura\Di\Container; use Zend\Form\View\HelperConfig; class ViewHelper extends ContainerConfig { public function define(Container $di) { $di->set('Zend\View\HelperPluginManager', $di->lazyNew('Zend\View\HelperPluginManager')); } public function modify(Container $di) { $serviceManager = $di->get('Zend\View\HelperPluginManager'); $helper = new HelperConfig(); $helper->configureServiceManager($servicemanager); } } Creating your own zend-view helper # Create your helper class 1 2 3 4 5 6 7 8 9 10 11 12 13 <?php namespace App\View\Helper; use Zend\View\Helper\AbstractHelper; class HasError extends AbstractHelper { // add as many parameters you want to pass from the view public function __invoke() { // some code } } Registering your helper class. First get the Zend\View\HelperPluginManager service.

Improving Environment Values in Aura v2

·208 words·1 min
Aura v2 framework probably have missed a better way to handle environment variables. But that doesn’t make you stall. Things can be improved ;-). Assume you are already using aura framework and is at root of the project. We are going to make use of vlucas/phpdotenv , alternatives are there if you are interested to experiment. 1 composer require vlucas/phpdotenv Edit the file config/_env.php and add Dotenv::load(/path/to/.env); to the first line. If you have not modified anything it will look as below