1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
6: */
7:
8: namespace Nette\DI;
9:
10: use Nette;
11:
12:
13: /**
14: * Configurator compiling extension.
15: *
16: * @author David Grudl
17: * @property-read array $config
18: * @property-read ContainerBuilder $containerBuilder
19: */
20: abstract class CompilerExtension extends Nette\Object
21: {
22: /** @var Compiler */
23: protected $compiler;
24:
25: /** @var string */
26: protected $name;
27:
28:
29: public function setCompiler(Compiler $compiler, $name)
30: {
31: $this->compiler = $compiler;
32: $this->name = $name;
33: return $this;
34: }
35:
36:
37: /**
38: * Returns extension configuration.
39: * @param array default unexpanded values.
40: * @return array
41: */
42: public function getConfig(array $defaults = NULL)
43: {
44: $config = $this->compiler->getConfig();
45: $config = isset($config[$this->name]) ? $config[$this->name] : array();
46: unset($config['services'], $config['factories']);
47: return Config\Helpers::merge($config, $this->compiler->getContainerBuilder()->expand($defaults));
48: }
49:
50:
51: /**
52: * @return ContainerBuilder
53: */
54: public function getContainerBuilder()
55: {
56: return $this->compiler->getContainerBuilder();
57: }
58:
59:
60: /**
61: * Reads configuration from file.
62: * @param string file name
63: * @return array
64: */
65: public function loadFromFile($file)
66: {
67: $loader = new Config\Loader;
68: $res = $loader->load($file);
69: $container = $this->compiler->getContainerBuilder();
70: foreach ($loader->getDependencies() as $file) {
71: $container->addDependency($file);
72: }
73: return $res;
74: }
75:
76:
77: /**
78: * Prepend extension name to identifier or service name.
79: * @param string
80: * @return string
81: */
82: public function prefix($id)
83: {
84: return substr_replace($id, $this->name . '.', substr($id, 0, 1) === '@' ? 1 : 0, 0);
85: }
86:
87:
88: /**
89: * Processes configuration data. Intended to be overridden by descendant.
90: * @return void
91: */
92: public function loadConfiguration()
93: {
94: }
95:
96:
97: /**
98: * Adjusts DI container before is compiled to PHP class. Intended to be overridden by descendant.
99: * @return void
100: */
101: public function beforeCompile()
102: {
103: }
104:
105:
106: /**
107: * Adjusts DI container compiled to PHP class. Intended to be overridden by descendant.
108: * @return void
109: */
110: public function afterCompile(Nette\PhpGenerator\ClassType $class)
111: {
112: }
113:
114: }
115: