TaggedServicesPass::saveTaggedServices()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.6666
cc 2
eloc 6
nc 2
nop 3
1
<?php
2
namespace kujaff\VersionsBundle\DependencyInjection\Compiler;
3
4
use Symfony\Component\DependencyInjection\ContainerBuilder;
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
7
/**
8
 * Save service tagged "bundle.install" and "bundle.update"
9
 */
10
class TaggedServicesPass implements CompilerPassInterface
11
{
12
13
	/**
14
	 * Save tagged service into a cache file
15
	 *
16
	 * @param ContainerBuilder $container
17
	 * @param string $tag
18
	 * @param string $fileName
19
	 */
20
	private function saveTaggedServices(ContainerBuilder $container, $tag, $fileName)
21
	{
22
		$services = array();
23
		foreach ($container->findTaggedServiceIds($tag) as $id => $attributes) {
24
			$services[] = $id;
25
		}
26
		$php = '<?php return ' . var_export($services, true) . ';';
27
		file_put_contents($container->getParameter('kernel.cache_dir') . DIRECTORY_SEPARATOR . $fileName, $php);
28
	}
29
30
	/**
31
	 * Process the build
32
	 *
33
	 * @param ContainerBuilder $container
34
	 */
35
	public function process(ContainerBuilder $container)
36
	{
37
		$this->saveTaggedServices($container, 'bundle.install', 'services.bundle.install.php');
38
		$this->saveTaggedServices($container, 'bundle.update', 'services.bundle.update.php');
39
		$this->saveTaggedServices($container, 'bundle.uninstall', 'services.bundle.uninstall.php');
40
	}
41
42
}
43