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