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