Created
July 26, 2012 16:46
-
-
Save wdalmut/3183148 to your computer and use it in GitHub Desktop.
ZF2 DiC - Zend Cache Object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
composer.phar | |
vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"minimum-stability": "dev", | |
"repositories": [ | |
{ | |
"type": "composer", | |
"url": "http://packages.zendframework.com/" | |
} | |
], | |
"require": { | |
"zendframework/zend-di": "2.0.*", | |
"zendframework/zend-cache": "2.0.*", | |
"zendframework/zend-servicemanager": "2.0.*", | |
"zendframework/zend-eventmanager": "2.0.*", | |
"zendframework/zend-config": "2.0.*" | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"hash": "572a2b8fb2a55f4499722e4c1382081a", | |
"packages": [ | |
{ | |
"package": "zendframework/zend-cache", | |
"version": "2.0.0-rc1" | |
}, | |
{ | |
"package": "zendframework/zend-code", | |
"version": "2.0.0-rc1" | |
}, | |
{ | |
"package": "zendframework/zend-config", | |
"version": "2.0.0-rc1" | |
}, | |
{ | |
"package": "zendframework/zend-di", | |
"version": "2.0.0-rc1" | |
}, | |
{ | |
"package": "zendframework/zend-eventmanager", | |
"version": "2.0.0-rc1" | |
}, | |
{ | |
"package": "zendframework/zend-servicemanager", | |
"version": "2.0.0-rc1" | |
}, | |
{ | |
"package": "zendframework/zend-stdlib", | |
"version": "2.0.0-rc1" | |
} | |
], | |
"packages-dev": null, | |
"aliases": [ | |
], | |
"minimum-stability": "dev", | |
"stability-flags": [ | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
definition.class.Zend\Cache\Pattern\PatternOptions.setObject.object.type = "A" | |
definition.class.Zend\Cache\Pattern\PatternOptions.setObject.object.required = true | |
definition.class.Zend\Cache\Pattern\PatternOptions.setStorage.storage.type = "Zend\Cache\Storage\Adapter\AbstractAdapter" | |
definition.class.Zend\Cache\Pattern\PatternOptions.setStorage.storage.required = true | |
definition.class.Zend\Cache\Pattern\ObjectCache.setOptions.options.type = "Zend\Cache\Pattern\PatternOptions" | |
definition.class.Zend\Cache\Pattern\ObjectCache.setOptions.options.required = true | |
instance.alias.cache = Zend\Cache\Pattern\ObjectCache | |
instance.Zend\Cache\Pattern\PatternOptions.injections.setStorage.storage = 'Zend\Cache\Storage\Adapter\Memory' | |
instance.Zend\Cache\Pattern\PatternOptions.injections.setObject.object = 'A' | |
instance.cache.injections.setOptions.shared = 'Zend\Cache\Pattern\PatternOptions' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__ . '/vendor/autoload.php'; | |
use Zend\Cache\PatternFactory; | |
class A { | |
/** | |
* @var B The B var | |
*/ | |
private $_b; | |
public function __construct(\B $b) { | |
$this->_b = $b; | |
} | |
public function intensiveTask($param1, $param2) { | |
return $param1 . " " . $this->_b->intensiveB() . " " . $param2; | |
} | |
} | |
class B { | |
private $_c; | |
public function __construct(\C $c) { | |
$this->_c = $c; | |
} | |
public function intensiveB() { | |
return $this->_c->compute(); | |
} | |
} | |
class C { | |
private $_c; | |
public function __construct() { | |
$this->_c = "Created C"; | |
} | |
public function compute() { | |
return $this->_c; | |
} | |
} | |
$c = new \C(); | |
$b = new \B($c); | |
$a = new \A($b); | |
$objectProxy = PatternFactory::factory('object', | |
array('object' => $a, 'storage' => 'memory')); | |
$computed = $objectProxy->intensiveTask("example", "complete"); | |
echo $computed . PHP_EOL; | |
// USING DI [SIMPLE 1] | |
$di = new \Zend\Di\Di(); | |
$c = $di->get("A"); | |
echo $c->intensiveTask("di", "ok") . PHP_EOL; | |
// Create this configuration | |
$c = new \C(); | |
$b = new \B($c); | |
$a = new \A($b); | |
$options = new \Zend\Cache\Pattern\PatternOptions(); | |
$options->setObject($a); | |
$storage = new \Zend\Cache\Storage\Adapter\Memory(); | |
$options->setStorage($storage); | |
$proxy = new \Zend\Cache\Pattern\ObjectCache(); | |
$proxy->setOptions($options); | |
echo $proxy->intensiveTask("A", "B") . PHP_EOL; | |
// Using Di | |
$di = new \Zend\Di\Di(); | |
$conf = new \Zend\Di\Config( | |
array( | |
'definition' => array( | |
'class' => array( | |
'Zend\Cache\Pattern\PatternOptions' => array( | |
'setObject' => array( | |
'object' => array( | |
'type' => 'A', | |
'required' => true | |
) | |
), | |
'setStorage' => array( | |
'storage' => array( | |
'type' => 'Zend\Cache\Storage\Adapter\AbstractAdapter', | |
'required' => true | |
) | |
) | |
), | |
'Zend\Cache\Pattern\ObjectCache' => array( | |
'setOptions' => array( | |
'options' => array( | |
'type' => 'Zend\Cache\Pattern\PatternOptions', | |
'required' => true | |
) | |
) | |
) | |
) | |
), | |
'instance' => array( | |
'alias' => array( | |
'cache' => 'Zend\Cache\Pattern\ObjectCache' | |
), | |
'Zend\Cache\Pattern\PatternOptions' => array( | |
'injections' => array( | |
'setStorage' => array("storage" => 'Zend\Cache\Storage\Adapter\Memory'), | |
'setObject' => array("object" => 'A') | |
) | |
), | |
'cache' => array( | |
'injections' => array( | |
'setOptions' => array('shared' => 'Zend\Cache\Pattern\PatternOptions') | |
) | |
) | |
) | |
) | |
); | |
$di->configure($conf); | |
$inj = $di->get("cache"); | |
echo $inj->intensiveTask("Di", "OKKKK") . PHP_EOL; | |
// DiC using ini files | |
$di = new \Zend\Di\Di(); | |
$ini = new \Zend\Config\Reader\Ini(); | |
$di->configure(new \Zend\Di\Config($ini->fromFile(__DIR__ . '/example.ini'))); | |
$proxy = $di->get("cache"); | |
echo $proxy->intensiveTask("Ini", "ok") . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks,
I agree with you that in Symfony it has more semantic mean at first sight. I try it out with symfony components to have a complete panorama.