Completed
Push — master ( aaeda7...30c0e6 )
by Olivier
02:00
created

ApplicationExtension::from()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ICanBoogie\Binding\SymfonyDependencyInjection\Extension;
13
14
use ICanBoogie\Application;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Definition;
17
use Symfony\Component\DependencyInjection\Extension\Extension;
18
use Symfony\Component\DependencyInjection\Reference;
19
20
class ApplicationExtension extends Extension
21
{
22
	const GETTER_PREFIX = 'get_';
23
	const LAZY_GETTER_PREFIX = 'lazy_get_';
24
25
	/**
26
	 * Create a new instance.
27
	 *
28
	 * @param Application $app
29
	 *
30
	 * @return static
31
	 */
32
	static public function from(Application $app)
33
	{
34
		return new static($app);
35
	}
36
37
	/**
38
	 * @var Application
39
	 */
40
	private $app;
41
42
	/**
43
	 * @param Application $app
44
	 */
45
	public function __construct(Application $app)
46
	{
47
		$this->app = $app;
48
	}
49
50
	/**
51
	 * @inheritdoc
52
	 */
53
	public function load(array $configs, ContainerBuilder $container)
54
	{
55
		$container->setDefinition(
56
			'app',
57
			(new Definition(Application::class))
58
				->setSynthetic(true)
59
		);
60
61
		foreach ($this->app->prototype as $method => $callable)
62
		{
63
			if (strpos($method, self::LAZY_GETTER_PREFIX) === 0)
64
			{
65
				$id = substr($method, strlen(self::LAZY_GETTER_PREFIX));
66
			}
67
			elseif (strpos($method, self::GETTER_PREFIX) === 0)
68
			{
69
				$id = substr($method, strlen(self::GETTER_PREFIX));
70
			}
71
			else
72
			{
73
				continue;
74
			}
75
76
			$definition = (new Definition('ICanBoogie\Dummy' . uniqid()))
77
				->setFactory([ new Reference('app'), '__get' ])
78
				->setArguments([ $id ]);
79
80
			$container->setDefinition($id, $definition);
81
		}
82
	}
83
}
84