Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

YamlFileLoader.php

Zuletzt modifiziert: 09.10.2024, 12:58 - Dateigröße: 12.38 KiB


001  <?php
002   
003  /*
004   * This file is part of the Symfony package.
005   *
006   * (c) Fabien Potencier <fabien@symfony.com>
007   *
008   * For the full copyright and license information, please view the LICENSE
009   * file that was distributed with this source code.
010   */
011   
012  namespace Symfony\Component\DependencyInjection\Loader;
013   
014  use Symfony\Component\DependencyInjection\DefinitionDecorator;
015  use Symfony\Component\DependencyInjection\Alias;
016  use Symfony\Component\DependencyInjection\ContainerInterface;
017  use Symfony\Component\DependencyInjection\Definition;
018  use Symfony\Component\DependencyInjection\Reference;
019  use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
020  use Symfony\Component\Config\Resource\FileResource;
021  use Symfony\Component\Yaml\Parser as YamlParser;
022   
023  /**
024   * YamlFileLoader loads YAML files service definitions.
025   *
026   * The YAML format does not support anonymous services (cf. the XML loader).
027   *
028   * @author Fabien Potencier <fabien@symfony.com>
029   */
030  class YamlFileLoader extends FileLoader
031  {
032      private $yamlParser;
033   
034      /**
035       * Loads a Yaml file.
036       *
037       * @param mixed  $file The resource
038       * @param string $type The resource type
039       */
040      public function load($file, $type = null)
041      {
042          $path = $this->locator->locate($file);
043   
044          $content = $this->loadFile($path);
045   
046          $this->container->addResource(new FileResource($path));
047   
048          // empty file
049          if (null === $content) {
050              return;
051          }
052   
053          // imports
054          $this->parseImports($content, $path);
055   
056          // parameters
057          if (isset($content['parameters'])) {
058              if (!is_array($content['parameters'])) {
059                  throw new InvalidArgumentException(sprintf('The "parameters" key should contain an array in %s. Check your YAML syntax.', $file));
060              }
061   
062              foreach ($content['parameters'] as $key => $value) {
063                  $this->container->setParameter($key, $this->resolveServices($value));
064              }
065          }
066   
067          // extensions
068          $this->loadFromExtensions($content);
069   
070          // services
071          $this->parseDefinitions($content, $file);
072      }
073   
074      /**
075       * Returns true if this class supports the given resource.
076       *
077       * @param mixed  $resource A resource
078       * @param string $type     The resource type
079       *
080       * @return bool    true if this class supports the given resource, false otherwise
081       */
082      public function supports($resource, $type = null)
083      {
084          return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION);
085      }
086   
087      /**
088       * Parses all imports
089       *
090       * @param array  $content
091       * @param string $file
092       */
093      private function parseImports($content, $file)
094      {
095          if (!isset($content['imports'])) {
096              return;
097          }
098   
099          if (!is_array($content['imports'])) {
100              throw new InvalidArgumentException(sprintf('The "imports" key should contain an array in %s. Check your YAML syntax.', $file));
101          }
102   
103          foreach ($content['imports'] as $import) {
104              if (!is_array($import)) {
105                  throw new InvalidArgumentException(sprintf('The values in the "imports" key should be arrays in %s. Check your YAML syntax.', $file));
106              }
107   
108              $this->setCurrentDir(dirname($file));
109              $this->import($import['resource'], null, isset($import['ignore_errors']) ? (bool) $import['ignore_errors'] : false, $file);
110          }
111      }
112   
113      /**
114       * Parses definitions
115       *
116       * @param array  $content
117       * @param string $file
118       */
119      private function parseDefinitions($content, $file)
120      {
121          if (!isset($content['services'])) {
122              return;
123          }
124   
125          if (!is_array($content['services'])) {
126              throw new InvalidArgumentException(sprintf('The "services" key should contain an array in %s. Check your YAML syntax.', $file));
127          }
128   
129          foreach ($content['services'] as $id => $service) {
130              $this->parseDefinition($id, $service, $file);
131          }
132      }
133   
134      /**
135       * Parses a definition.
136       *
137       * @param string $id
138       * @param array  $service
139       * @param string $file
140       *
141       * @throws InvalidArgumentException When tags are invalid
142       */
143      private function parseDefinition($id, $service, $file)
144      {
145          if (is_string($service) && 0 === strpos($service, '@')) {
146              $this->container->setAlias($id, substr($service, 1));
147   
148              return;
149          }
150   
151          if (!is_array($service)) {
152              throw new InvalidArgumentException(sprintf('A service definition must be an array or a string starting with "@" but %s found for service "%s" in %s. Check your YAML syntax.', gettype($service), $id, $file));
153          }
154   
155          if (isset($service['alias'])) {
156              $public = !array_key_exists('public', $service) || (bool) $service['public'];
157              $this->container->setAlias($id, new Alias($service['alias'], $public));
158   
159              return;
160          }
161   
162          if (isset($service['parent'])) {
163              $definition = new DefinitionDecorator($service['parent']);
164          } else {
165              $definition = new Definition();
166          }
167   
168          if (isset($service['class'])) {
169              $definition->setClass($service['class']);
170          }
171   
172          if (isset($service['scope'])) {
173              $definition->setScope($service['scope']);
174          }
175   
176          if (isset($service['synthetic'])) {
177              $definition->setSynthetic($service['synthetic']);
178          }
179   
180          if (isset($service['synchronized'])) {
181              $definition->setSynchronized($service['synchronized']);
182          }
183   
184          if (isset($service['lazy'])) {
185              $definition->setLazy($service['lazy']);
186          }
187   
188          if (isset($service['public'])) {
189              $definition->setPublic($service['public']);
190          }
191   
192          if (isset($service['abstract'])) {
193              $definition->setAbstract($service['abstract']);
194          }
195   
196          if (isset($service['factory_class'])) {
197              $definition->setFactoryClass($service['factory_class']);
198          }
199   
200          if (isset($service['factory_method'])) {
201              $definition->setFactoryMethod($service['factory_method']);
202          }
203   
204          if (isset($service['factory_service'])) {
205              $definition->setFactoryService($service['factory_service']);
206          }
207   
208          if (isset($service['file'])) {
209              $definition->setFile($service['file']);
210          }
211   
212          if (isset($service['arguments'])) {
213              $definition->setArguments($this->resolveServices($service['arguments']));
214          }
215   
216          if (isset($service['properties'])) {
217              $definition->setProperties($this->resolveServices($service['properties']));
218          }
219   
220          if (isset($service['configurator'])) {
221              if (is_string($service['configurator'])) {
222                  $definition->setConfigurator($service['configurator']);
223              } else {
224                  $definition->setConfigurator(array($this->resolveServices($service['configurator'][0]), $service['configurator'][1]));
225              }
226          }
227   
228          if (isset($service['calls'])) {
229              if (!is_array($service['calls'])) {
230                  throw new InvalidArgumentException(sprintf('Parameter "calls" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
231              }
232   
233              foreach ($service['calls'] as $call) {
234                  $args = isset($call[1]) ? $this->resolveServices($call[1]) : array();
235                  $definition->addMethodCall($call[0], $args);
236              }
237          }
238   
239          if (isset($service['tags'])) {
240              if (!is_array($service['tags'])) {
241                  throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
242              }
243   
244              foreach ($service['tags'] as $tag) {
245                  if (!is_array($tag)) {
246                      throw new InvalidArgumentException(sprintf('A "tags" entry must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
247                  }
248   
249                  if (!isset($tag['name'])) {
250                      throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
251                  }
252   
253                  $name = $tag['name'];
254                  unset($tag['name']);
255   
256                  foreach ($tag as $value) {
257                      if (!is_scalar($value)) {
258                          throw new InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s" in %s. Check your YAML syntax.', $id, $name, $file));
259                      }
260                  }
261   
262                  $definition->addTag($name, $tag);
263              }
264          }
265   
266          $this->container->setDefinition($id, $definition);
267      }
268   
269      /**
270       * Loads a YAML file.
271       *
272       * @param string $file
273       *
274       * @return array The file content
275       *
276       * @throws InvalidArgumentException when the given file is not a local file or when it does not exist
277       */
278      protected function loadFile($file)
279      {
280          if (!stream_is_local($file)) {
281              throw new InvalidArgumentException(sprintf('This is not a local file "%s".', $file));
282          }
283   
284          if (!file_exists($file)) {
285              throw new InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file));
286          }
287   
288          if (null === $this->yamlParser) {
289              $this->yamlParser = new YamlParser();
290          }
291   
292          return $this->validate($this->yamlParser->parse(file_get_contents($file)), $file);
293      }
294   
295      /**
296       * Validates a YAML file.
297       *
298       * @param mixed  $content
299       * @param string $file
300       *
301       * @return array
302       *
303       * @throws InvalidArgumentException When service file is not valid
304       */
305      private function validate($content, $file)
306      {
307          if (null === $content) {
308              return $content;
309          }
310   
311          if (!is_array($content)) {
312              throw new InvalidArgumentException(sprintf('The service file "%s" is not valid. It should contain an array. Check your YAML syntax.', $file));
313          }
314   
315          foreach (array_keys($content) as $namespace) {
316              if (in_array($namespace, array('imports', 'parameters', 'services'))) {
317                  continue;
318              }
319   
320              if (!$this->container->hasExtension($namespace)) {
321                  $extensionNamespaces = array_filter(array_map(function ($ext) { return $ext->getAlias(); }, $this->container->getExtensions()));
322                  throw new InvalidArgumentException(sprintf(
323                      'There is no extension able to load the configuration for "%s" (in %s). Looked for namespace "%s", found %s',
324                      $namespace,
325                      $file,
326                      $namespace,
327                      $extensionNamespaces ? sprintf('"%s"', implode('", "', $extensionNamespaces)) : 'none'
328                  ));
329              }
330          }
331   
332          return $content;
333      }
334   
335      /**
336       * Resolves services.
337       *
338       * @param string|array $value
339       *
340       * @return array|string|Reference
341       */
342      private function resolveServices($value)
343      {
344          if (is_array($value)) {
345              $value = array_map(array($this, 'resolveServices'), $value);
346          } elseif (is_string($value) &&  0 === strpos($value, '@')) {
347              if (0 === strpos($value, '@@')) {
348                  $value = substr($value, 1);
349                  $invalidBehavior = null;
350              } elseif (0 === strpos($value, '@?')) {
351                  $value = substr($value, 2);
352                  $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
353              } else {
354                  $value = substr($value, 1);
355                  $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
356              }
357   
358              if ('=' === substr($value, -1)) {
359                  $value = substr($value, 0, -1);
360                  $strict = false;
361              } else {
362                  $strict = true;
363              }
364   
365              if (null !== $invalidBehavior) {
366                  $value = new Reference($value, $invalidBehavior, $strict);
367              }
368          }
369   
370          return $value;
371      }
372   
373      /**
374       * Loads from Extensions
375       *
376       * @param array $content
377       */
378      private function loadFromExtensions($content)
379      {
380          foreach ($content as $namespace => $values) {
381              if (in_array($namespace, array('imports', 'parameters', 'services'))) {
382                  continue;
383              }
384   
385              if (!is_array($values)) {
386                  $values = array();
387              }
388   
389              $this->container->loadFromExtension($namespace, $values);
390          }
391      }
392  }
393