Skip to content

Instantly share code, notes, and snippets.

@wdalmut
Created July 29, 2012 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wdalmut/3196566 to your computer and use it in GitHub Desktop.
Save wdalmut/3196566 to your computer and use it in GitHub Desktop.
Pimple DiC - Zend Cache Object
vendor
composer.phar
{
"minimum-stability": "dev",
"repositories": [
{
"type": "composer",
"url": "http://packages.zendframework.com/"
}
],
"require": {
"zendframework/zend-cache": "2.0.*",
"zendframework/zend-servicemanager": "2.0.*",
"zendframework/zend-eventmanager": "2.0.*",
"pimple/pimple": "1.0.0"
}
}
{
"hash": "8680af13d9b10bc96fa48f580e93ed71",
"packages": [
{
"package": "pimple/pimple",
"version": "1.0.0"
},
{
"package": "zendframework/zend-cache",
"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": [
]
}
<?php
require_once 'vendor/autoload.php';
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;
}
}
$container = new Pimple();
$container["options"] = function($c) {
$options = new \Zend\Cache\Pattern\PatternOptions();
$options->setStorage($c["storage"]);
$options->setObject($c["model"]);
return $options;
};
$container["storage"] = function($c) {
$storage = new \Zend\Cache\Storage\Adapter\Memory();
return $storage;
};
$container["model"] = function($c) {
$model = new \A(new \B(new \C()));
return $model;
};
$container["proxy"] = function($c) {
$cache = new \Zend\Cache\Pattern\ObjectCache();
$cache->setOptions($c["options"]);
return $cache;
};
echo $container["proxy"]->intensiveTask("pimple", "ok") . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment