Ярлыки

.htaccess (4) тестирование (8) шаблоны проектирования (3) css (5) Debian (6) docker (2) Doctrine2 (6) Git (6) html (4) java (6) javascript (13) jquery (11) LFS (3) linux (23) mac os (4) mod_rewrite (2) MSSQL (4) MySQL (18) ORM Doctrine (17) patterns (3) PDO (3) perl (7) PHP (64) PHPUnit (8) Python (15) SEO (2) Silex (1) SimpleXML (1) SQL (14) ssh (4) Ubuntu (24) Yii1 (1) Zend Framework (19) ZendFramework2 (8)

среда, 27 августа 2014 г.

Zend Framework 2. Di использование зависимостей без контроля типов (type-hint).

Версия ZF2 2.3.2
Примеры в руководстве не работают.
class B
{
    protected $a = null;
    
    public function setA($a)
    {
        $this->a = $a;
    }
}

...

use Zend\Di\Di;
use Zend\Di\Definition;
use Zend\Di\Definition\Builder;
use Zend\Di\DefinitionList;

...

$builder = new Definition\BuilderDefinition;
$builder->addClass(($class = new Builder\PhpClass));

$class->setName('Application\Service\B');
$class->addInjectionMethod(($im = new Builder\InjectionMethod));

$im->setName('setA');
$im->addParameter('a', 'Application\Service\A');

$dList = new DefinitionList(array(
                 $builder,
                 new Definition\RuntimeDefinition
         ));
$di = new Di;
$di->setDefinitionList($dList);

$options = array(  
    'definition' => array(  
        'class' => array(  
            'Application\Service\B' => array(  
                'setA' => array(  
                    'required' => true  
                )  
            )  
        )  
    )  
);  
$config = new \Zend\Di\Config($options);  
$di->configure($config);

$c = $di->get('Application\Service\C', array(
    'username' => 'MyUsernameValue',
    'password' => 'MyHardToGuessPassword%$#',
));

object(Application\Service\C)[251]
  protected 'b' => 
    object(Application\Service\B)[244]
      protected 'a' => 
        object(Application\Service\A)[248]
          protected 'username' => string 'MyUsernameValue' (length=15)
          protected 'password' => string 'MyHardToGuessPassword%$#' (length=24)

Zend Framework 2. Zend\Di setter injection.

class A
{
    protected $username;
    
    protected $password;
    
    public function __construct($username, $password)
    {
        $this->username = $username;
        $this->password = $password;
    }
}

class B
{
    protected $a = null;
    
    public function setA(A $a)
    {
        $this->a = $a;
    }
}

class C
{
    protected $b = null;
    
    public function __construct(B $b)
    {
        $this->b = $b;
    }
}

$di = new \Zend\Di\Di;
$options = array(
    'definition' => array(
        'class' => array(
            'Application\Service\B' => array(
                'setA' => array(
                    'required' => true
                )
            )
        )
    )
);
$config = new \Zend\Di\Config($options);
$di->configure($config);
$di->instanceManager()
            ->setParameters('Application\Service\A', array(
    'username' => 'leon',
    'password' => 'sf32KLjldos'
));

$c = $di->get('Application\Service\C');

var_dump($c);
object(Application\Service\C)[246]
  protected 'b' => 
    object(Application\Service\B)[242]
      protected 'a' => 
        object(Application\Service\A)[244]
          protected 'username' => string 'leon' (length=4)
          protected 'password' => string 'sf32KLjldos' (length=11)