forked from liuggio/godfather
-
Notifications
You must be signed in to change notification settings - Fork 6
/
godfather.php
executable file
·58 lines (52 loc) · 1.64 KB
/
godfather.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
$loader = require __DIR__.'/../vendor/autoload.php';
$loader->add('Entity', __DIR__);
$loader->add('Manager', __DIR__);
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
use PUGX\Godfather\Container\DependencyInjection\CompilerPass;
use PUGX\GodfatherBundle\DependencyInjection\GodfatherExtension;
/**
* Random.
*
* @return \Entity\Mug|\Entity\Tshirt|stdClass
*/
function getRandomEntityProduct()
{
switch (rand(0,2)) {
case 0:
return new Entity\Mug();
case 1:
return new Entity\Tshirt();
case 2:
return new \stdClass();
}
}
// loading config
$config = array(
'default' => array(
'contexts' => array(
'manager' => array(
'fallback' => 'manager.default'
)
)
)
);
$container = new ContainerBuilder();
$loader = new GodfatherExtension();
$loader->load(array($config), $container);
// loading strategies
$loader = new YamlFileLoader($container, new FileLocator(__DIR__));
$loader->load('services.yml');
// loading tagged services
$container->addCompilerPass(new CompilerPass());
$container->compile();
// working with strategy
$product = getRandomEntityProduct();
// getting the correct manager
$strategy = $container->get('godfather')->getManager($product);
echo "Random entity class\t".get_class($product).PHP_EOL;
echo " the manager class\t".get_class($strategy).PHP_EOL;
echo " manager::getName()\t".$strategy->getName().PHP_EOL.PHP_EOL;
echo "Please run it again the result may change :) ".PHP_EOL.PHP_EOL;