Completed
Push — 7.x-1.x ( 2f9e3c...1070be )
by Frédéric G.
01:36
created

Exportable::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\qa;
4
5
abstract class Exportable {
6
7
  /**
8
   * The directory containing the file containing this class.
9
   *
10
   * @var string
11
   */
12
  public $dir;
13
14
  /**
15
   * Machine name
16
   *
17
   * @var string
18
   */
19
  public $name;
20
21
  /**
22
   * The namespace for the class.
23
   *
24
   * @var string
25
   */
26
  public $namespace;
27
28
  /**
29
   * Description: translatable
30
   *
31
   * @var string
32
   */
33
  public $description;
34
35
  /**
36
   * Human readable name, used for titles. Translatable.
37
   *
38
   * @var string
39
   */
40
  public $title;
41
42
  /**
43
   * @param $base
44
   * @param $ancestor
45
   *
46
   * @return array
47
   */
48
  public static function getClasses($base, $ancestor) {
49
    $realBase = realpath($base);
50
    $rdi = new \RecursiveDirectoryIterator($base, \FilesystemIterator::SKIP_DOTS);
51
    $rii = new \RecursiveIteratorIterator($rdi);
52
    $ri = new \RegexIterator($rii, '/.+\.php$/', \RegexIterator::GET_MATCH);
53
    foreach ($ri as $k => $v) {
54
      include_once $k;
55
    }
56
    $packageNames = array_filter(get_declared_classes(), function ($name) use ($realBase, $ancestor) {
57
      $ret = is_subclass_of($name, $ancestor);
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $ancestor can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
58
      if ($ret) {
59
        $rc = new \ReflectionClass($name);
60
        if (!$rc->isInstantiable()) {
61
          $ret = FALSE;
62
        }
63
        else {
64
          $dir = dirname($rc->getFileName());
65
          if (strpos($dir, $realBase) !== 0) {
66
            $ret = FALSE;
67
          }
68
        }
69
      }
70
      return $ret;
71
    });
72
    $packageNames = array_combine($packageNames, $packageNames);
73
    $ret = array_map(function ($name) {
74
      $instance = new $name();
75
      return $instance;
76
    }, $packageNames);
77
    return $ret;
78
  }
79
80
  /**
81
   * Singleton protected constructor
82
   */
83
  public function __construct() {
84
    $this->name = get_called_class();
85
    $rc = new \ReflectionClass($this->name);
86
    $this->dir = dirname($rc->getFileName());
87
    $this->namespace = $rc->getNamespaceName();
88
    $this->init();
89
  }
90
91
  /**
92
   * Initializer: must be implemented in concrete classes.
93
   *
94
   * Assignment to $this->package_name cannot be factored because it uses a
95
   * per-class magic constant.
96
   *
97
   * @return void
98
   */
99
  abstract public function init();
100
}
101