YamlDiscovery.php 1.66 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php

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

namespace Drupal\Component\Discovery;

10
use Drupal\Component\Serialization\Yaml;
11
12
13
14
15
16
17
18
19
20
21
22
23
24

/**
 * 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
   *
   * @var array
   */
  protected $directories = array();

  /**
   * Constructs a YamlDiscovery object.
   *
   * @param string $name
35
36
   *   The base filename to look for in each directory. The format will be
   *   $provider.$name.yml.
37
   * @param array $directories
38
   *   An array of directories to scan, keyed by the provider.
39
40
41
42
43
44
45
46
47
48
49
   */
  public function __construct($name, array $directories) {
    $this->name = $name;
    $this->directories = $directories;
  }

  /**
   * {@inheritdoc}
   */
  public function findAll() {
    $all = array();
50
    foreach ($this->findFiles() as $provider => $file) {
51
52
53
      // If a file is empty or its contents are commented out, return an empty
      // array instead of NULL for type consistency.
      $all[$provider] = Yaml::decode(file_get_contents($file)) ?: [];
54
55
56
57
58
59
    }

    return $all;
  }

  /**
60
   * Returns an array of file paths, keyed by provider.
61
62
63
64
65
   *
   * @return array
   */
  protected function findFiles() {
    $files = array();
66
67
    foreach ($this->directories as $provider => $directory) {
      $file = $directory . '/' . $provider . '.' . $this->name . '.yml';
68
      if (file_exists($file)) {
69
        $files[$provider] = $file;
70
71
72
73
74
75
76
      }
    }
    return $files;
  }

}