/** * Data Provider for method __construct($definitionsFile, $diInstance) * * @return array */ public function constructDataProvider() { $this->_diInstance = $this->getMock('Zend\\Di\\Di', array('get', 'setDefinitionList', 'instanceManager')); $this->_magentoConfig = $this->getMock('Mage_Core_Model_Config', array('loadBase'), array(), '', false); $this->_instanceManager = $this->getMock('Zend\\Di\\InstanceManager', array('addSharedInstance'), array(), '', false); $this->_diInstance->expects($this->exactly(3))->method('instanceManager')->will($this->returnValue($this->_instanceManager)); $this->_diInstance->expects($this->exactly(6))->method('get')->with('Mage_Core_Model_Config')->will($this->returnCallback(array($this, 'getCallback'))); $this->_diInstance->expects($this->exactly(4))->method('setDefinitionList')->will($this->returnCallback(array($this, 'verifySetDefinitionListCallback'))); $this->_instanceManager->expects($this->exactly(3))->method('addSharedInstance')->will($this->returnCallback(array($this, 'verifyAddSharedInstanceCallback'))); return array('without definition file and with specific Di instance' => array(null, $this->_diInstance), 'with definition file and with specific Di instance' => array(__DIR__ . '/_files/test_definition_file', $this->_diInstance), 'with missing definition file and with specific Di instance' => array('test_definition_file', $this->_diInstance)); }
/** * @param Zend\Di\DefinitionList|null $definitions * @param Magento_Di_InstanceManager|null $instanceManager * @param Zend\Di\Config|null $config * @param string|null $definitionsFile * @return Magento_Di_Zend */ public function __construct(Zend\Di\DefinitionList $definitions = null, Magento_Di_InstanceManager $instanceManager = null, Zend\Di\Config $config = null, $definitionsFile = null) { if (is_null($definitions)) { if (is_string($definitionsFile) && is_file($definitionsFile) && is_readable($definitionsFile)) { $definitionProvider = new Magento_Di_Definition_ArrayDefinition_Zend(unserialize(file_get_contents($definitionsFile))); } else { $definitionProvider = new Magento_Di_Definition_RuntimeDefinition_Zend(); } $definitions = new Magento_Di_DefinitionList_Zend($definitionProvider); } $instanceManager = $instanceManager ?: new Magento_Di_InstanceManager_Zend(); parent::__construct($definitions, $instanceManager, $config); $this->setInstanceManager($instanceManager); }
<?php namespace MovieApp { class Lister { public $dbFinder; public function __construct(DbFinder $dbFinder) { $this->dbFinder = $dbFinder; } } class DbFinder { public $username, $password = null; public function setCredentials($username, $password) { $this->username = $username; $this->password = $password; } } } namespace { // bootstrap include 'zf2bootstrap' . (stream_resolve_include_path('zf2bootstrap.php') ? '.php' : '.dist.php'); $di = new Zend\Di\Di(); $lister = $di->get('MovieApp\\Lister', array('username' => 'my-username', 'password' => 'my-password')); // expression to test $works = $lister->dbFinder instanceof MovieApp\DbFinder && $lister->dbFinder->username == 'my-username' && $lister->dbFinder->password == 'my-password'; // display result echo $works ? 'It works!' : 'It DOES NOT work!'; }
class Mapper { public $drivers; public function addDriver(Driver $driver, $name) { $this->drivers[] = array($driver, $name); } } interface Driver { } } namespace DifferentNs { class DriverOne implements \System\Driver { } class DriverTwo implements \System\Driver { } } namespace { // bootstrap include 'zf2bootstrap' . (stream_resolve_include_path('zf2bootstrap.php') ? '.php' : '.dist.php'); $di = new Zend\Di\Di(); $di->configure(new Zend\Di\Configuration(array('definition' => array('class' => array('System\\Mapper' => array('addDriver' => array('driver' => array('type' => 'System\\Driver', 'required' => true), 'name' => array('required' => true))))), 'instance' => array('System\\Mapper' => array('injections' => array('addDriver' => array(array('driver' => 'DifferentNs\\DriverOne', 'name' => 'foo'), array('driver' => 'DifferentNs\\DriverTwo', 'name' => 'bar')))))))); $mapper = $di->get('System\\Mapper'); // expression to test $works = $mapper->drivers[0][0] instanceof DifferentNs\DriverOne && $mapper->drivers[1][0] instanceof DifferentNs\DriverTwo; // display result echo ($works ? 'It works!' : 'It DOES NOT work!') . PHP_EOL; }
namespace MovieApp { class Lister { public $finderA, $finderB; public function setFinderA(Finder $finder) { $this->finderA = $finder; } public function setFinderB(Finder $finder) { $this->finderB = $finder; } } class Finder { } } namespace { // bootstrap include 'zf2bootstrap' . (stream_resolve_include_path('zf2bootstrap.php') ? '.php' : '.dist.php'); $di = new Zend\Di\Di(); $di->instanceManager()->setParameters('MovieApp\\Lister', array('MovieApp\\Lister::setFinderA:finder' => new MovieApp\Finder(), 'MovieApp\\Lister::setFinderB:finder' => function () { return new MovieApp\Finder(); })); $lister = $di->get('MovieApp\\Lister'); // expression to test $works = $lister->finderA instanceof MovieApp\Finder && $lister->finderB instanceof MovieApp\Finder && $lister->finderA !== $lister->finderB; // display result echo ($works ? 'It works!' : 'It DOES NOT work!') . PHP_EOL; }
namespace MovieApp { class Lister { public $dbFinder; public function __construct(DbFinder $dbFinder) { $this->dbFinder = $dbFinder; } } class DbFinder { public $username, $password = null; public function setCredentials($username, $password) { $this->username = $username; $this->password = $password; } } } namespace { // bootstrap include 'zf2bootstrap' . (stream_resolve_include_path('zf2bootstrap.php') ? '.php' : '.dist.php'); $di = new Zend\Di\Di(); $di->instanceManager()->setParameters('MovieApp\\DbFinder', array('username' => 'my-username', 'password' => 'my-password')); $lister = $di->get('MovieApp\\Lister'); // expression to test $works = $lister->dbFinder instanceof MovieApp\DbFinder && $lister->dbFinder->username == 'my-username' && $lister->dbFinder->password == 'my-password'; // display result echo ($works ? 'It works!' : 'It DOES NOT work!') . PHP_EOL; }
<?php namespace MovieApp { class Lister { public $finder; public function __construct(Finder $finder) { $this->finder = $finder; } } class Finder { } } namespace { // bootstrap include 'zf2bootstrap' . (stream_resolve_include_path('zf2bootstrap.php') ? '.php' : '.dist.php'); $di = new Zend\Di\Di(); $lister = $di->get('MovieApp\\Lister'); // expression to test $works = $lister->finder instanceof MovieApp\Finder; // display result echo ($works ? 'It works!' : 'It DOES NOT work!') . PHP_EOL; }
<?php $di = new Zend\Di\Di(); //trigger autoloader $a = $di->get('A'); unset($a); $t1 = microtime(true); for ($i = 0; $i < 10000; $i++) { $a = $di->get('A'); } $t2 = microtime(true); $results = ['time' => $t2 - $t1, 'files' => count(get_included_files()), 'memory' => memory_get_peak_usage() / 1024 / 1024]; echo json_encode($results);
<?php $di = new Zend\Di\Di(); for ($i = 0; $i < $argv[1]; $i++) { //This is required otherwise ->newinstance() only creates a new instance of the top level of the code //If there's a better way to configure this let me know $di = new Zend\Di\Di(); $a = $di->newinstance('J'); } $results = ['time' => 0, 'files' => count(get_included_files()), 'memory' => memory_get_peak_usage() / 1024 / 1024]; echo json_encode($results);
namespace MovieApp { class Lister { public $finder; public function setFinder(Finder $finder) { $this->finder = $finder; } } class Finder { public $id; public function __construct($id) { $this->id = $id; } } } namespace { // bootstrap include 'zf2bootstrap' . (stream_resolve_include_path('zf2bootstrap.php') ? '.php' : '.dist.php'); $di = new Zend\Di\Di(); $di->instanceManager()->setParameters('MovieApp\\Lister', array('finder' => function () { return new MovieApp\Finder('foo'); })); $lister = $di->get('MovieApp\\Lister'); // expression to test $works = $lister->finder instanceof MovieApp\Finder && $lister->finder->id === 'foo'; // display result echo ($works ? 'It works!' : 'It DOES NOT work!') . PHP_EOL; }
/***************************************** * CONFIGURE LOGGER *****************************************/ $oInfoLogger = new Zend\Log\Logger(); $oExceptionLogger = new Zend\Log\Logger(); $oLogger = new Classes\Helpers\Logger($oInfoLogger, $oExceptionLogger, $aSectionConfig); $oLogger->ConfigureLogger('jobs'); /***************************************** * CONFIGURE ADAPTER *****************************************/ $aDbConfig = array('driver' => $aSectionConfig['database.adapter'], 'host' => $aSectionConfig['database.params.host'], 'username' => $aSectionConfig['database.params.username'], 'password' => $aSectionConfig['database.params.password'], 'dbname' => $aSectionConfig['database.params.dbname']); $oAdapter = new Zend\Db\Adapter\Adapter($aDbConfig); /***************************************** * CONFIGURE DIC *****************************************/ $oDi = new Zend\Di\Di(); $oDi->instanceManager()->setParameters('Classes\\Helpers\\File', array('aConfig' => $aSectionConfig)); $oDi->instanceManager()->setParameters('Classes\\Helpers\\Logger', array('oInfoLogger' => $oInfoLogger, 'oExceptionLogger' => $oExceptionLogger, 'aConfig' => $aSectionConfig)); $oDi->instanceManager()->setParameters('Classes\\Mappers\\JobItemsToMwXml', array('aConfig' => $aSectionConfig)); $oDi->instanceManager()->setParameters('Classes\\Db\\JobQueue', array('oAdapter' => $oAdapter)); $oDi->instanceManager()->setParameters('Classes\\Db\\Box', array('oAdapter' => $oAdapter)); $oDi->instanceManager()->setParameters('Classes\\Db\\Folio', array('oAdapter' => $oAdapter)); $oDi->instanceManager()->setParameters('Classes\\Db\\Item', array('oAdapter' => $oAdapter)); $oDi->instanceManager()->setParameters('Classes\\Db\\ErrorLog', array('oAdapter' => $oAdapter)); $oDi->instanceManager()->setParameters('Classes\\Db\\MediaWiki', array('oAdapter' => $oAdapter)); $oMediaWikiDb = $oDi->get('Classes\\Db\\MediaWiki'); $oDi->instanceManager()->setParameters('Classes\\Mappers\\JobItemsToMwXml', array($oMediaWikiDb, 'aConfig' => $aSectionConfig)); /***************************************** * ADD TASK ABSTRACT *****************************************/ require_once 'Classes/TaskAbstract.php';
<?php namespace Foo\Bar { class Baz { public $bam; public function __construct(Bam $bam) { $this->bam = $bam; } } class Bam { public $username, $password = null; public function __construct($username, $password) { $this->username = $username; $this->password = $password; } } } namespace { include 'zf2bootstrap.php'; $di = new Zend\Di\Di(); $di->instanceManager()->setParameters('Foo\\Bar\\Bam', array('username' => 'my-username', 'password' => 'my-password')); $baz = $di->get('Foo\\Bar\\Baz'); // expression to test $works = $baz->bam instanceof Foo\Bar\Bam && $baz->bam->username == 'my-username' && $baz->bam->password == 'my-password'; // display result echo $works ? 'It works!' : 'It DOES NOT work!'; }
<?php require_once 'library/Zend/Loader/StandardAutoloader.php'; $loader = new Zend\Loader\StandardAutoloader(array('autoregister_zf' => true)); $loader->registerNamespace('SON', 'library/SON'); $loader->register(); $definitionList = new Zend\Di\DefinitionList(array($runtime = new Zend\Di\Definition\RuntimeDefinition())); $di = new Zend\Di\Di($definitionList); //$di->instanceManager()->setParameters('SON\Db\Connection', array( // 'server' => 'localhost', // 'dbname' => 'banco', // 'user' => 'username', // 'password' => 123 //)); $di->instanceManager()->addAlias('conexao1', 'SON\\Db\\Connection', array('server' => 'localhost', 'dbname' => 'banco', 'user' => 'username', 'password' => 123)); $di->instanceManager()->addAlias('conexao2', 'SON\\Db\\Connection', array('server' => 'localhost', 'dbname' => 'banco2', 'user' => 'username', 'password' => 1234)); $conexao1 = $di->get('conexao1'); $conexao2 = $di->get('conexao2'); $di->instanceManager()->addTypePreference('SON\\Db\\Connection', 'conexao1'); $categoria = $di->get('SON\\Categoria', array('db' => 'conexao2')); #$di = new Zend\Di\Di; //$di->configure(new Zend\Di\Config(array( // 'definition' => array( // 'class' => array( // 'SON\Produto' => array( // 'setCategoria' => array('required'=>true) // ) // ) // ) //))); $di->configure(new Zend\Di\Config(array('definition' => array('class' => array('SON\\Produto' => array('addCategoria' => array('categoria' => array('type' => 'SON\\CategoriaInterface', 'required' => true))))), 'instance' => array('SON\\Produto' => array('injections' => array('SON\\Categoria', 'SON\\Category', 'SON\\Categoria'))))));
<?php namespace Foo\Bar { use Zend\Di\Definition\Annotation as Di; class Baz { public $bam; /** * @Di\Inject() */ public function injectBam(Bam $bam) { $this->bam = $bam; } } class Bam { } } namespace { include 'zf2bootstrap.php'; $di = new Zend\Di\Di(); $di->definitions()->getDefinitionForClass('Foo\\Bar\\Baz')->getIntrospectionStrategy()->setUseAnnotations(true); $baz = $di->get('Foo\\Bar\\Baz'); // expression to test $works = $baz->bam instanceof Foo\Bar\Bam; // display result echo $works ? 'It works!' : 'It DOES NOT work!'; }
<?php namespace Foo\Bar { class Baz { public $bam; public function setBam(Bam $bam) { $this->bam = $bam; } } class Bam { } } namespace { include 'zf2bootstrap.php'; $di = new Zend\Di\Di(); $di->configure(new Zend\Di\Configuration(array('definition' => array('class' => array('Foo\\Bar\\Baz' => array('setBam' => array('required' => true))))))); $baz = $di->get('Foo\\Bar\\Baz'); // expression to test $works = $baz->bam instanceof Foo\Bar\Bam; // display result echo $works ? 'It works!' : 'It DOES NOT work!'; }
/** * Get model class with injection (as new instance) * * @param string $path Path * @param array $params Params * * @return mixed */ public function newInstance($path, $params = []) { $className = Mage::getConfig()->getModelClassName($path); return $this->_di->newInstance($className, $params); }
public function getPaginator(array $paginatorOptions = array()) { $defaultPaginatorOptions = array('itemCountPerPage' => 10, 'pageRange' => 5, 'pageNumber' => 1); $dataClass = $this->getDataClass(); $count = $dataClass->getCount(); if (!$count) { return $this->paginator = null; } $dbPaginatorOptions = $dataClass->getPaginatorOptions(); $paginatorOptions = array_merge($defaultPaginatorOptions, $dbPaginatorOptions, $paginatorOptions); $count = (int) $count; $diConfig = array('instance' => array('Zend\\Paginator\\Adapter\\DbSelect' => array('parameters' => array('rowCount' => $count, 'select' => $dataClass->getSelect(), 'adapterOrSqlObject' => $dataClass->getSql())), 'Eva\\Paginator\\Paginator' => array('parameters' => array('rowCount' => $count, 'adapter' => 'Zend\\Paginator\\Adapter\\DbSelect')))); foreach ($paginatorOptions as $key => $value) { if (false === in_array($key, array('itemCountPerPage', 'pageNumber', 'pageRange'))) { continue; } $diConfig['instance']['Eva\\Paginator\\Paginator']['parameters'][$key] = $paginatorOptions[$key]; } $di = new \Zend\Di\Di(); $di->configure(new \Zend\Di\Config($diConfig)); $paginator = $di->get('Eva\\Paginator\\Paginator'); return $this->paginator = $paginator; }
class Page { public $blocks; public function addBlock(Block $block) { $this->blocks[] = $block; } } interface Block { } } namespace DifferentNs { class BlockOne implements \Application\Block { } class BlockTwo implements \Application\Block { } } namespace { // bootstrap include 'zf2bootstrap' . (stream_resolve_include_path('zf2bootstrap.php') ? '.php' : '.dist.php'); $di = new Zend\Di\Di(); $di->configure(new Zend\Di\Configuration(array('definition' => array('class' => array('Application\\Page' => array('addBlock' => array('block' => array('type' => 'Application\\Block', 'required' => true))))), 'instance' => array('Application\\Page' => array('injections' => array('DifferentNs\\BlockOne', 'DifferentNs\\BlockTwo')))))); $page = $di->get('Application\\Page'); // expression to test $works = $page->blocks[0] instanceof DifferentNs\BlockOne && $page->blocks[1] instanceof DifferentNs\BlockTwo; // display result echo ($works ? 'It works!' : 'It DOES NOT work!') . PHP_EOL; }
<?php namespace MovieApp { use Zend\Di\Definition\Annotation as Di; class Lister { public $finder; /** * @Di\Inject() */ public function setFinder(Finder $finder) { $this->finder = $finder; } } class Finder { } } namespace { // bootstrap include 'zf2bootstrap' . (stream_resolve_include_path('zf2bootstrap.php') ? '.php' : '.dist.php'); $di = new Zend\Di\Di(); $di->definitions()->getDefinitionForClass('MovieApp\\Lister')->getIntrospectionStrategy()->setUseAnnotations(true); $lister = $di->get('MovieApp\\Lister'); // expression to test $works = $lister->finder instanceof MovieApp\Finder; // display result echo ($works ? 'It works!' : 'It DOES NOT work!') . PHP_EOL; }
<?php namespace MovieApp { class Lister { public $finder; public function setFinder(FinderInterface $finder) { $this->finder = $finder; } } class DbFinder implements FinderInterface { } interface FinderInterface { } } namespace { // bootstrap include 'zf2bootstrap' . (stream_resolve_include_path('zf2bootstrap.php') ? '.php' : '.dist.php'); $di = new Zend\Di\Di(); $di->configure(new Zend\Di\Configuration(array('instance' => array('preferences' => array('MovieApp\\FinderInterface' => 'MovieApp\\DbFinder'))))); $lister = $di->get('MovieApp\\Lister'); // expression to test $works = $lister->finder instanceof MovieApp\DbFinder; // display result echo ($works ? 'It works!' : 'It DOES NOT work!') . PHP_EOL; }
namespace MovieApp { class Lister implements FinderAwareInterface { public $finder; public function setFinder(Finder $finder) { $this->finder = $finder; } } class Finder { public function findAllByName($name) { } } interface FinderAwareInterface { public function setFinder(Finder $finder); } } namespace { // bootstrap include 'zf2bootstrap' . (stream_resolve_include_path('zf2bootstrap.php') ? '.php' : '.dist.php'); $di = new Zend\Di\Di(); $lister = new MovieApp\Lister(); $di->injectDependencies($lister); // expression to test $works = $lister->finder instanceof MovieApp\Finder; // display result echo ($works ? 'It works!' : 'It DOES NOT work!') . PHP_EOL; }
<?php namespace Foo\Bar { class Baz { public $bam; public function __construct(Bam $bam) { $this->bam = $bam; } } class Bam { public $username, $password = null; public function setCredentials($username, $password) { $this->username = $username; $this->password = $password; } } } namespace { include 'zf2bootstrap.php'; $di = new Zend\Di\Di(); $baz = $di->get('Foo\\Bar\\Baz', array('username' => 'my-username', 'password' => 'my-password')); // expression to test $works = $baz->bam instanceof Foo\Bar\Bam && $baz->bam->username == 'my-username' && $baz->bam->password == 'my-password'; // display result echo $works ? 'It works!' : 'It DOES NOT work!'; }
<?php namespace MovieApp { class Lister { public $finder; public function setFinder(Finder $finder) { $this->finder = $finder; } } class Finder { } } namespace { // bootstrap include 'zf2bootstrap' . (stream_resolve_include_path('zf2bootstrap.php') ? '.php' : '.dist.php'); $di = new Zend\Di\Di(); $di->configure(new Zend\Di\Configuration(array('definition' => array('class' => array('MovieApp\\Lister' => array('setFinder' => array('required' => true))))))); $lister = $di->get('MovieApp\\Lister'); // expression to test $works = $lister->finder instanceof MovieApp\Finder; // display result echo $works ? 'It works!' : 'It DOES NOT work!'; }
{ public $finder; public function __construct(Finder $finder) { $this->finder = $finder; } } class Finder { public $source; public function __construct(Source $source) { $this->source = $source; } } class Source { } } namespace { // bootstrap include 'zf2bootstrap' . (stream_resolve_include_path('zf2bootstrap.php') ? '.php' : '.dist.php'); $di = new Zend\Di\Di(); $di->configure(new Zend\Di\Configuration(array('instance' => array('MovieApp\\Finder' => array('shared' => false))))); $lister1 = $di->get('MovieApp\\Lister1'); $lister2 = $di->get('MovieApp\\Lister2'); // expression to test $works = $lister1->finder instanceof MovieApp\Finder && $lister1->finder !== $lister2->finder && $lister1->finder->source === $lister2->finder->source; // display result echo ($works ? 'It works!' : 'It DOES NOT work!') . PHP_EOL; }
<?php include 'zf2bootstrap.php'; // must register autoloader $autoloader->registerNamespace('Foo\\Bar', __DIR__ . '/12'); $compiler = new Zend\Di\Definition\CompilerDefinition(); $compiler->addDirectory(__DIR__ . '/12/'); $compiler->compile(); $definitions = new Zend\Di\DefinitionList($compiler); $di = new Zend\Di\Di($definitions); $baz = $di->get('Foo\\Bar\\Baz'); // expression to test $works = $baz->bam instanceof Foo\Bar\Bam; // display result echo $works ? 'It works!' : 'It DOES NOT work!';
<?php $adapter = (include file_exists('bootstrap.php') ? 'bootstrap.php' : 'bootstrap.dist.php'); $di = new Zend\Di\Di(); $di->configure(new Zend\Di\Configuration(array('instance' => array('Zend\\Db\\Adapter\\Adapter' => array('parameters' => array('driver' => $dbconfig)))))); //var_dump($di->instanceManager()); $db = $di->get('Zend\\Db\\Adapter\\Adapter'); echo $db->platform->getName() . PHP_EOL;
<?php $di = new Zend\Di\Di(); //trigger autoloader for all required files $a = $di->newinstance('A'); unset($a); $t1 = microtime(true); for ($i = 0; $i < 10000; $i++) { $a = $di->newinstance('A'); } $t2 = microtime(true); $results = ['time' => $t2 - $t1, 'files' => count(get_included_files()), 'memory' => memory_get_peak_usage() / 1024 / 1024]; echo json_encode($results);
<?php $di = new Zend\Di\Di(); //trigger autoloader for all required files $a1 = $di->get('B'); $a2 = $di->get('B'); $t1 = microtime(true); for ($i = 0; $i < 10000; $i++) { $a = $di->newinstance('B'); } $t2 = microtime(true); $results = ['time' => $t2 - $t1, 'files' => count(get_included_files()), 'memory' => memory_get_peak_usage() / 1024 / 1024]; echo json_encode($results);