PluginProbe
Packeta / trunk
Packeta vtrunk
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / deps / nette / di / src / DI / CompilerExtension.php

CompilerExtension.php in Packeta trunk, at deps/nette/di/src/DI/CompilerExtension.php

141 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * This file is part of the Nette Framework (https://nette.org)
5 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6 */
7 declare (strict_types=1);
8 namespace Packetery\Nette\DI;
9
10 use Packetery\Nette;
11 /**
12 * Configurator compiling extension.
13 */
14 abstract class CompilerExtension
15 {
16 use \Packetery\Nette\SmartObject;
17 /** @var Compiler */
18 protected $compiler;
19 /** @var string */
20 protected $name;
21 /** @var array|object */
22 protected $config = [];
23 /** @var \Packetery\Nette\PhpGenerator\Closure */
24 protected $initialization;
25 /** @return static */
26 public function setCompiler(Compiler $compiler, string $name)
27 {
28 $this->initialization = new \Packetery\Nette\PhpGenerator\Closure();
29 $this->compiler = $compiler;
30 $this->name = $name;
31 return $this;
32 }
33 /**
34 * @param array|object $config
35 * @return static
36 */
37 public function setConfig($config)
38 {
39 if (!\is_array($config) && !\is_object($config)) {
40 throw new \Packetery\Nette\InvalidArgumentException();
41 }
42 $this->config = $config;
43 return $this;
44 }
45 /**
46 * Returns extension configuration.
47 * @return array|object
48 */
49 public function getConfig()
50 {
51 return $this->config;
52 }
53 /**
54 * Returns configuration schema.
55 */
56 public function getConfigSchema() : \Packetery\Nette\Schema\Schema
57 {
58 return \is_object($this->config) ? \Packetery\Nette\Schema\Expect::from($this->config) : \Packetery\Nette\Schema\Expect::array();
59 }
60 /**
61 * Checks whether $config contains only $expected items and returns combined array.
62 * @throws \Packetery\Nette\InvalidStateException
63 * @deprecated use getConfigSchema()
64 */
65 public function validateConfig(array $expected, ?array $config = null, ?string $name = null) : array
66 {
67 if (\func_num_args() === 1) {
68 return $this->config = $this->validateConfig($expected, $this->config);
69 }
70 if ($extra = \array_diff_key((array) $config, $expected)) {
71 $name = $name ? \str_replace('.', " › ", $name) : $this->name;
72 $hint = \Packetery\Nette\Utils\Helpers::getSuggestion(\array_keys($expected), \key($extra));
73 throw new \Packetery\Nette\DI\InvalidConfigurationException(\sprintf("Unknown configuration option '%s › %s'", $name, $hint ? \key($extra) : \implode("', '{$name} › ", \array_keys($extra))) . ($hint ? ", did you mean '{$name} › {$hint}'?" : '.'));
74 }
75 return \Packetery\Nette\Schema\Helpers::merge($config, $expected);
76 }
77 public function getContainerBuilder() : ContainerBuilder
78 {
79 return $this->compiler->getContainerBuilder();
80 }
81 /**
82 * Reads configuration from file.
83 */
84 public function loadFromFile(string $file) : array
85 {
86 $loader = $this->createLoader();
87 $res = $loader->load($file);
88 $this->compiler->addDependencies($loader->getDependencies());
89 return $res;
90 }
91 /**
92 * Loads list of service definitions from configuration.
93 * Prefixes its names and replaces @extension with name in definition.
94 */
95 public function loadDefinitionsFromConfig(array $configList) : void
96 {
97 $res = [];
98 foreach ($configList as $key => $config) {
99 $key = \is_string($key) ? $this->name . '.' . $key : $key;
100 $res[$key] = Helpers::prefixServiceName($config, $this->name);
101 }
102 $this->compiler->loadDefinitionsFromConfig($res);
103 }
104 protected function createLoader() : Config\Loader
105 {
106 return new Config\Loader();
107 }
108 public function getInitialization() : \Packetery\Nette\PhpGenerator\Closure
109 {
110 return $this->initialization;
111 }
112 /**
113 * Prepend extension name to identifier or service name.
114 */
115 public function prefix(string $id) : string
116 {
117 return \substr_replace($id, $this->name . '.', \substr($id, 0, 1) === '@' ? 1 : 0, 0);
118 }
119 /**
120 * Processes configuration data. Intended to be overridden by descendant.
121 * @return void
122 */
123 public function loadConfiguration()
124 {
125 }
126 /**
127 * Adjusts DI container before is compiled to PHP class. Intended to be overridden by descendant.
128 * @return void
129 */
130 public function beforeCompile()
131 {
132 }
133 /**
134 * Adjusts DI container compiled to PHP class. Intended to be overridden by descendant.
135 * @return void
136 */
137 public function afterCompile(\Packetery\Nette\PhpGenerator\ClassType $class)
138 {
139 }
140 }
141