YamlDiscovery.php 1.92 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php

/**
 * @file
 * Contains \Drupal\Component\Discovery\YamlDiscovery.
 */

namespace Drupal\Component\Discovery;

use Symfony\Component\Yaml\Parser;

/**
 * Provides discovery for YAML files within a given set of directories.
 */
class YamlDiscovery implements DiscoverableInterface {

  /**
   * The base filename to look for in each directory.
   *
   * @var string
   */
  protected $name;

  /**
25
   * An array of directories to scan, keyed by the provider.
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
   *
   * @var array
   */
  protected $directories = array();

  /**
   * The symfony YAML parser.
   *
   * @var \Symfony\Component\Yaml\Parser
   */
  protected $parser;

  /**
   * Constructs a YamlDiscovery object.
   *
   * @param string $name
42
43
   *   The base filename to look for in each directory. The format will be
   *   $provider.$name.yml.
44
   * @param array $directories
45
   *   An array of directories to scan, keyed by the provider.
46
47
48
49
50
51
52
53
54
55
56
57
58
   */
  public function __construct($name, array $directories) {
    $this->name = $name;
    $this->directories = $directories;
  }

  /**
   * {@inheritdoc}
   */
  public function findAll() {
    $all = array();
    $parser = $this->parser();

59
60
    foreach ($this->findFiles() as $provider => $file) {
      $all[$provider] = $parser->parse(file_get_contents($file));
61
62
63
64
65
66
    }

    return $all;
  }

  /**
67
   * Returns the YAML parser.
68
69
70
71
72
73
74
75
76
77
78
79
   *
   * @return \Symfony\Component\Yaml\Parser
   *   The symfony YAML parser.
   */
  protected function parser() {
    if (!isset($this->parser)) {
      $this->parser = new Parser();
    }
    return $this->parser;
  }

  /**
80
   * Returns an array of file paths, keyed by provider.
81
82
83
84
85
   *
   * @return array
   */
  protected function findFiles() {
    $files = array();
86
87
    foreach ($this->directories as $provider => $directory) {
      $file = $directory . '/' . $provider . '.' . $this->name . '.yml';
88
      if (file_exists($file)) {
89
        $files[$provider] = $file;
90
91
92
93
94
95
96
      }
    }
    return $files;
  }

}