Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Extension;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\FileCache\FileCacheFactory;
|
Chris@0
|
6 use Drupal\Core\DrupalKernel;
|
Chris@0
|
7 use Drupal\Core\Extension\Discovery\RecursiveExtensionFilterIterator;
|
Chris@0
|
8 use Drupal\Core\Site\Settings;
|
Chris@0
|
9 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * Discovers available extensions in the filesystem.
|
Chris@0
|
13 *
|
Chris@0
|
14 * To also discover test modules, add
|
Chris@0
|
15 * @code
|
Chris@0
|
16 * $settings['extension_discovery_scan_tests'] = TRUE;
|
Chris@0
|
17 * @endcode
|
Chris@0
|
18 * to your settings.php.
|
Chris@0
|
19 */
|
Chris@0
|
20 class ExtensionDiscovery {
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Origin directory weight: Core.
|
Chris@0
|
24 */
|
Chris@0
|
25 const ORIGIN_CORE = 0;
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * Origin directory weight: Installation profile.
|
Chris@0
|
29 */
|
Chris@0
|
30 const ORIGIN_PROFILE = 1;
|
Chris@0
|
31
|
Chris@0
|
32 /**
|
Chris@0
|
33 * Origin directory weight: sites/all.
|
Chris@0
|
34 */
|
Chris@0
|
35 const ORIGIN_SITES_ALL = 2;
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * Origin directory weight: Site-wide directory.
|
Chris@0
|
39 */
|
Chris@0
|
40 const ORIGIN_ROOT = 3;
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * Origin directory weight: Parent site directory of a test site environment.
|
Chris@0
|
44 */
|
Chris@0
|
45 const ORIGIN_PARENT_SITE = 4;
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * Origin directory weight: Site-specific directory.
|
Chris@0
|
49 */
|
Chris@0
|
50 const ORIGIN_SITE = 5;
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * Regular expression to match PHP function names.
|
Chris@0
|
54 *
|
Chris@0
|
55 * @see http://php.net/manual/functions.user-defined.php
|
Chris@0
|
56 */
|
Chris@0
|
57 const PHP_FUNCTION_PATTERN = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/';
|
Chris@0
|
58
|
Chris@0
|
59 /**
|
Chris@0
|
60 * Previously discovered files keyed by origin directory and extension type.
|
Chris@0
|
61 *
|
Chris@0
|
62 * @var array
|
Chris@0
|
63 */
|
Chris@0
|
64 protected static $files = [];
|
Chris@0
|
65
|
Chris@0
|
66 /**
|
Chris@0
|
67 * List of installation profile directories to additionally scan.
|
Chris@0
|
68 *
|
Chris@0
|
69 * @var array
|
Chris@0
|
70 */
|
Chris@0
|
71 protected $profileDirectories;
|
Chris@0
|
72
|
Chris@0
|
73 /**
|
Chris@0
|
74 * The app root for the current operation.
|
Chris@0
|
75 *
|
Chris@0
|
76 * @var string
|
Chris@0
|
77 */
|
Chris@0
|
78 protected $root;
|
Chris@0
|
79
|
Chris@0
|
80 /**
|
Chris@0
|
81 * The file cache object.
|
Chris@0
|
82 *
|
Chris@0
|
83 * @var \Drupal\Component\FileCache\FileCacheInterface
|
Chris@0
|
84 */
|
Chris@0
|
85 protected $fileCache;
|
Chris@0
|
86
|
Chris@0
|
87 /**
|
Chris@0
|
88 * The site path.
|
Chris@0
|
89 *
|
Chris@0
|
90 * @var string
|
Chris@0
|
91 */
|
Chris@0
|
92 protected $sitePath;
|
Chris@0
|
93
|
Chris@0
|
94 /**
|
Chris@0
|
95 * Constructs a new ExtensionDiscovery object.
|
Chris@0
|
96 *
|
Chris@0
|
97 * @param string $root
|
Chris@0
|
98 * The app root.
|
Chris@0
|
99 * @param bool $use_file_cache
|
Chris@0
|
100 * Whether file cache should be used.
|
Chris@0
|
101 * @param string[] $profile_directories
|
Chris@0
|
102 * The available profile directories
|
Chris@0
|
103 * @param string $site_path
|
Chris@0
|
104 * The path to the site.
|
Chris@0
|
105 */
|
Chris@0
|
106 public function __construct($root, $use_file_cache = TRUE, $profile_directories = NULL, $site_path = NULL) {
|
Chris@0
|
107 $this->root = $root;
|
Chris@0
|
108 $this->fileCache = $use_file_cache ? FileCacheFactory::get('extension_discovery') : NULL;
|
Chris@0
|
109 $this->profileDirectories = $profile_directories;
|
Chris@0
|
110 $this->sitePath = $site_path;
|
Chris@0
|
111 }
|
Chris@0
|
112
|
Chris@0
|
113 /**
|
Chris@0
|
114 * Discovers available extensions of a given type.
|
Chris@0
|
115 *
|
Chris@0
|
116 * Finds all extensions (modules, themes, etc) that exist on the site. It
|
Chris@0
|
117 * searches in several locations. For instance, to discover all available
|
Chris@0
|
118 * modules:
|
Chris@0
|
119 * @code
|
Chris@0
|
120 * $listing = new ExtensionDiscovery(\Drupal::root());
|
Chris@0
|
121 * $modules = $listing->scan('module');
|
Chris@0
|
122 * @endcode
|
Chris@0
|
123 *
|
Chris@0
|
124 * The following directories will be searched (in the order stated):
|
Chris@0
|
125 * - the core directory; i.e., /core
|
Chris@0
|
126 * - the installation profile directory; e.g., /core/profiles/standard
|
Chris@0
|
127 * - the legacy site-wide directory; i.e., /sites/all
|
Chris@0
|
128 * - the site-wide directory; i.e., /
|
Chris@0
|
129 * - the site-specific directory; e.g., /sites/example.com
|
Chris@0
|
130 *
|
Chris@0
|
131 * To also find test modules, add
|
Chris@0
|
132 * @code
|
Chris@0
|
133 * $settings['extension_discovery_scan_tests'] = TRUE;
|
Chris@0
|
134 * @endcode
|
Chris@0
|
135 * to your settings.php.
|
Chris@0
|
136 *
|
Chris@0
|
137 * The information is returned in an associative array, keyed by the extension
|
Chris@0
|
138 * name (without .info.yml extension). Extensions found later in the search
|
Chris@0
|
139 * will take precedence over extensions found earlier - unless they are not
|
Chris@0
|
140 * compatible with the current version of Drupal core.
|
Chris@0
|
141 *
|
Chris@0
|
142 * @param string $type
|
Chris@0
|
143 * The extension type to search for. One of 'profile', 'module', 'theme', or
|
Chris@0
|
144 * 'theme_engine'.
|
Chris@0
|
145 * @param bool $include_tests
|
Chris@0
|
146 * (optional) Whether to explicitly include or exclude test extensions. By
|
Chris@0
|
147 * default, test extensions are only discovered when in a test environment.
|
Chris@0
|
148 *
|
Chris@0
|
149 * @return \Drupal\Core\Extension\Extension[]
|
Chris@0
|
150 * An associative array of Extension objects, keyed by extension name.
|
Chris@0
|
151 */
|
Chris@0
|
152 public function scan($type, $include_tests = NULL) {
|
Chris@0
|
153 // Determine the installation profile directories to scan for extensions,
|
Chris@0
|
154 // unless explicit profile directories have been set. Exclude profiles as we
|
Chris@0
|
155 // cannot have profiles within profiles.
|
Chris@0
|
156 if (!isset($this->profileDirectories) && $type != 'profile') {
|
Chris@0
|
157 $this->setProfileDirectoriesFromSettings();
|
Chris@0
|
158 }
|
Chris@0
|
159
|
Chris@0
|
160 // Search the core directory.
|
Chris@0
|
161 $searchdirs[static::ORIGIN_CORE] = 'core';
|
Chris@0
|
162
|
Chris@0
|
163 // Search the legacy sites/all directory.
|
Chris@0
|
164 $searchdirs[static::ORIGIN_SITES_ALL] = 'sites/all';
|
Chris@0
|
165
|
Chris@0
|
166 // Search for contributed and custom extensions in top-level directories.
|
Chris@0
|
167 // The scan uses a whitelist to limit recursion to the expected extension
|
Chris@0
|
168 // type specific directory names only.
|
Chris@0
|
169 $searchdirs[static::ORIGIN_ROOT] = '';
|
Chris@0
|
170
|
Chris@0
|
171 // Simpletest uses the regular built-in multi-site functionality of Drupal
|
Chris@0
|
172 // for running web tests. As a consequence, extensions of the parent site
|
Chris@0
|
173 // located in a different site-specific directory are not discovered in a
|
Chris@0
|
174 // test site environment, because the site directories are not the same.
|
Chris@0
|
175 // Therefore, add the site directory of the parent site to the search paths,
|
Chris@0
|
176 // so that contained extensions are still discovered.
|
Chris@0
|
177 // @see \Drupal\simpletest\WebTestBase::setUp()
|
Chris@0
|
178 if ($parent_site = Settings::get('test_parent_site')) {
|
Chris@0
|
179 $searchdirs[static::ORIGIN_PARENT_SITE] = $parent_site;
|
Chris@0
|
180 }
|
Chris@0
|
181
|
Chris@0
|
182 // Find the site-specific directory to search. Since we are using this
|
Chris@0
|
183 // method to discover extensions including profiles, we might be doing this
|
Chris@0
|
184 // at install time. Therefore Kernel service is not always available, but is
|
Chris@0
|
185 // preferred.
|
Chris@0
|
186 if (\Drupal::hasService('kernel')) {
|
Chris@0
|
187 $searchdirs[static::ORIGIN_SITE] = \Drupal::service('site.path');
|
Chris@0
|
188 }
|
Chris@0
|
189 else {
|
Chris@0
|
190 $searchdirs[static::ORIGIN_SITE] = $this->sitePath ?: DrupalKernel::findSitePath(Request::createFromGlobals());
|
Chris@0
|
191 }
|
Chris@0
|
192
|
Chris@0
|
193 // Unless an explicit value has been passed, manually check whether we are
|
Chris@0
|
194 // in a test environment, in which case test extensions must be included.
|
Chris@0
|
195 // Test extensions can also be included for debugging purposes by setting a
|
Chris@0
|
196 // variable in settings.php.
|
Chris@0
|
197 if (!isset($include_tests)) {
|
Chris@0
|
198 $include_tests = Settings::get('extension_discovery_scan_tests') || drupal_valid_test_ua();
|
Chris@0
|
199 }
|
Chris@0
|
200
|
Chris@0
|
201 $files = [];
|
Chris@0
|
202 foreach ($searchdirs as $dir) {
|
Chris@0
|
203 // Discover all extensions in the directory, unless we did already.
|
Chris@0
|
204 if (!isset(static::$files[$this->root][$dir][$include_tests])) {
|
Chris@0
|
205 static::$files[$this->root][$dir][$include_tests] = $this->scanDirectory($dir, $include_tests);
|
Chris@0
|
206 }
|
Chris@0
|
207 // Only return extensions of the requested type.
|
Chris@0
|
208 if (isset(static::$files[$this->root][$dir][$include_tests][$type])) {
|
Chris@0
|
209 $files += static::$files[$this->root][$dir][$include_tests][$type];
|
Chris@0
|
210 }
|
Chris@0
|
211 }
|
Chris@0
|
212
|
Chris@0
|
213 // If applicable, filter out extensions that do not belong to the current
|
Chris@0
|
214 // installation profiles.
|
Chris@0
|
215 $files = $this->filterByProfileDirectories($files);
|
Chris@0
|
216 // Sort the discovered extensions by their originating directories.
|
Chris@0
|
217 $origin_weights = array_flip($searchdirs);
|
Chris@0
|
218 $files = $this->sort($files, $origin_weights);
|
Chris@0
|
219
|
Chris@0
|
220 // Process and return the list of extensions keyed by extension name.
|
Chris@0
|
221 return $this->process($files);
|
Chris@0
|
222 }
|
Chris@0
|
223
|
Chris@0
|
224 /**
|
Chris@0
|
225 * Sets installation profile directories based on current site settings.
|
Chris@0
|
226 *
|
Chris@0
|
227 * @return $this
|
Chris@0
|
228 */
|
Chris@0
|
229 public function setProfileDirectoriesFromSettings() {
|
Chris@0
|
230 $this->profileDirectories = [];
|
Chris@0
|
231 $profile = drupal_get_profile();
|
Chris@0
|
232 // For SimpleTest to be able to test modules packaged together with a
|
Chris@0
|
233 // distribution we need to include the profile of the parent site (in
|
Chris@0
|
234 // which test runs are triggered).
|
Chris@0
|
235 if (drupal_valid_test_ua() && !drupal_installation_attempted()) {
|
Chris@0
|
236 $testing_profile = \Drupal::config('simpletest.settings')->get('parent_profile');
|
Chris@0
|
237 if ($testing_profile && $testing_profile != $profile) {
|
Chris@0
|
238 $this->profileDirectories[] = drupal_get_path('profile', $testing_profile);
|
Chris@0
|
239 }
|
Chris@0
|
240 }
|
Chris@0
|
241 // In case both profile directories contain the same extension, the actual
|
Chris@0
|
242 // profile always has precedence.
|
Chris@0
|
243 if ($profile) {
|
Chris@0
|
244 $this->profileDirectories[] = drupal_get_path('profile', $profile);
|
Chris@0
|
245 }
|
Chris@0
|
246 return $this;
|
Chris@0
|
247 }
|
Chris@0
|
248
|
Chris@0
|
249 /**
|
Chris@0
|
250 * Gets the installation profile directories to be scanned.
|
Chris@0
|
251 *
|
Chris@0
|
252 * @return array
|
Chris@0
|
253 * A list of installation profile directory paths relative to the system
|
Chris@0
|
254 * root directory.
|
Chris@0
|
255 */
|
Chris@0
|
256 public function getProfileDirectories() {
|
Chris@0
|
257 return $this->profileDirectories;
|
Chris@0
|
258 }
|
Chris@0
|
259
|
Chris@0
|
260 /**
|
Chris@0
|
261 * Sets explicit profile directories to scan.
|
Chris@0
|
262 *
|
Chris@0
|
263 * @param array $paths
|
Chris@0
|
264 * A list of installation profile directory paths relative to the system
|
Chris@0
|
265 * root directory (without trailing slash) to search for extensions.
|
Chris@0
|
266 *
|
Chris@0
|
267 * @return $this
|
Chris@0
|
268 */
|
Chris@0
|
269 public function setProfileDirectories(array $paths = NULL) {
|
Chris@0
|
270 $this->profileDirectories = $paths;
|
Chris@0
|
271 return $this;
|
Chris@0
|
272 }
|
Chris@0
|
273
|
Chris@0
|
274 /**
|
Chris@0
|
275 * Filters out extensions not belonging to the scanned installation profiles.
|
Chris@0
|
276 *
|
Chris@0
|
277 * @param \Drupal\Core\Extension\Extension[] $all_files
|
Chris@0
|
278 * The list of all extensions.
|
Chris@0
|
279 *
|
Chris@0
|
280 * @return \Drupal\Core\Extension\Extension[]
|
Chris@0
|
281 * The filtered list of extensions.
|
Chris@0
|
282 */
|
Chris@0
|
283 protected function filterByProfileDirectories(array $all_files) {
|
Chris@0
|
284 if (empty($this->profileDirectories)) {
|
Chris@0
|
285 return $all_files;
|
Chris@0
|
286 }
|
Chris@0
|
287
|
Chris@0
|
288 $all_files = array_filter($all_files, function ($file) {
|
Chris@0
|
289 if (strpos($file->subpath, 'profiles') !== 0) {
|
Chris@0
|
290 // This extension doesn't belong to a profile, ignore it.
|
Chris@0
|
291 return TRUE;
|
Chris@0
|
292 }
|
Chris@0
|
293
|
Chris@0
|
294 foreach ($this->profileDirectories as $weight => $profile_path) {
|
Chris@0
|
295 if (strpos($file->getPath(), $profile_path) === 0) {
|
Chris@0
|
296 // Parent profile found.
|
Chris@0
|
297 return TRUE;
|
Chris@0
|
298 }
|
Chris@0
|
299 }
|
Chris@0
|
300
|
Chris@0
|
301 return FALSE;
|
Chris@0
|
302 });
|
Chris@0
|
303
|
Chris@0
|
304 return $all_files;
|
Chris@0
|
305 }
|
Chris@0
|
306
|
Chris@0
|
307 /**
|
Chris@0
|
308 * Sorts the discovered extensions.
|
Chris@0
|
309 *
|
Chris@0
|
310 * @param \Drupal\Core\Extension\Extension[] $all_files
|
Chris@0
|
311 * The list of all extensions.
|
Chris@0
|
312 * @param array $weights
|
Chris@0
|
313 * An array of weights, keyed by originating directory.
|
Chris@0
|
314 *
|
Chris@0
|
315 * @return \Drupal\Core\Extension\Extension[]
|
Chris@0
|
316 * The sorted list of extensions.
|
Chris@0
|
317 */
|
Chris@0
|
318 protected function sort(array $all_files, array $weights) {
|
Chris@0
|
319 $origins = [];
|
Chris@0
|
320 $profiles = [];
|
Chris@0
|
321 foreach ($all_files as $key => $file) {
|
Chris@0
|
322 // If the extension does not belong to a profile, just apply the weight
|
Chris@0
|
323 // of the originating directory.
|
Chris@0
|
324 if (strpos($file->subpath, 'profiles') !== 0) {
|
Chris@0
|
325 $origins[$key] = $weights[$file->origin];
|
Chris@0
|
326 $profiles[$key] = NULL;
|
Chris@0
|
327 }
|
Chris@0
|
328 // If the extension belongs to a profile but no profile directories are
|
Chris@0
|
329 // defined, then we are scanning for installation profiles themselves.
|
Chris@0
|
330 // In this case, profiles are sorted by origin only.
|
Chris@0
|
331 elseif (empty($this->profileDirectories)) {
|
Chris@0
|
332 $origins[$key] = static::ORIGIN_PROFILE;
|
Chris@0
|
333 $profiles[$key] = NULL;
|
Chris@0
|
334 }
|
Chris@0
|
335 else {
|
Chris@0
|
336 // Apply the weight of the originating profile directory.
|
Chris@0
|
337 foreach ($this->profileDirectories as $weight => $profile_path) {
|
Chris@0
|
338 if (strpos($file->getPath(), $profile_path) === 0) {
|
Chris@0
|
339 $origins[$key] = static::ORIGIN_PROFILE;
|
Chris@0
|
340 $profiles[$key] = $weight;
|
Chris@0
|
341 continue 2;
|
Chris@0
|
342 }
|
Chris@0
|
343 }
|
Chris@0
|
344 }
|
Chris@0
|
345 }
|
Chris@0
|
346 // Now sort the extensions by origin and installation profile(s).
|
Chris@0
|
347 // The result of this multisort can be depicted like the following matrix,
|
Chris@0
|
348 // whereas the first integer is the weight of the originating directory and
|
Chris@0
|
349 // the second is the weight of the originating installation profile:
|
Chris@0
|
350 // 0 core/modules/node/node.module
|
Chris@0
|
351 // 1 0 profiles/parent_profile/modules/parent_module/parent_module.module
|
Chris@0
|
352 // 1 1 core/profiles/testing/modules/compatible_test/compatible_test.module
|
Chris@0
|
353 // 2 sites/all/modules/common/common.module
|
Chris@0
|
354 // 3 modules/devel/devel.module
|
Chris@0
|
355 // 4 sites/default/modules/custom/custom.module
|
Chris@0
|
356 array_multisort($origins, SORT_ASC, $profiles, SORT_ASC, $all_files);
|
Chris@0
|
357
|
Chris@0
|
358 return $all_files;
|
Chris@0
|
359 }
|
Chris@0
|
360
|
Chris@0
|
361 /**
|
Chris@0
|
362 * Processes the filtered and sorted list of extensions.
|
Chris@0
|
363 *
|
Chris@0
|
364 * Extensions discovered in later search paths override earlier, unless they
|
Chris@0
|
365 * are not compatible with the current version of Drupal core.
|
Chris@0
|
366 *
|
Chris@0
|
367 * @param \Drupal\Core\Extension\Extension[] $all_files
|
Chris@0
|
368 * The sorted list of all extensions that were found.
|
Chris@0
|
369 *
|
Chris@0
|
370 * @return \Drupal\Core\Extension\Extension[]
|
Chris@0
|
371 * The filtered list of extensions, keyed by extension name.
|
Chris@0
|
372 */
|
Chris@0
|
373 protected function process(array $all_files) {
|
Chris@0
|
374 $files = [];
|
Chris@0
|
375 // Duplicate files found in later search directories take precedence over
|
Chris@0
|
376 // earlier ones; they replace the extension in the existing $files array.
|
Chris@0
|
377 foreach ($all_files as $file) {
|
Chris@0
|
378 $files[$file->getName()] = $file;
|
Chris@0
|
379 }
|
Chris@0
|
380 return $files;
|
Chris@0
|
381 }
|
Chris@0
|
382
|
Chris@0
|
383 /**
|
Chris@14
|
384 * Recursively scans a base directory for the extensions it contains.
|
Chris@0
|
385 *
|
Chris@0
|
386 * @param string $dir
|
Chris@0
|
387 * A relative base directory path to scan, without trailing slash.
|
Chris@0
|
388 * @param bool $include_tests
|
Chris@0
|
389 * Whether to include test extensions. If FALSE, all 'tests' directories are
|
Chris@0
|
390 * excluded in the search.
|
Chris@0
|
391 *
|
Chris@0
|
392 * @return array
|
Chris@0
|
393 * An associative array whose keys are extension type names and whose values
|
Chris@0
|
394 * are associative arrays of \Drupal\Core\Extension\Extension objects, keyed
|
Chris@0
|
395 * by absolute path name.
|
Chris@0
|
396 *
|
Chris@0
|
397 * @see \Drupal\Core\Extension\Discovery\RecursiveExtensionFilterIterator
|
Chris@0
|
398 */
|
Chris@0
|
399 protected function scanDirectory($dir, $include_tests) {
|
Chris@0
|
400 $files = [];
|
Chris@0
|
401
|
Chris@0
|
402 // In order to scan top-level directories, absolute directory paths have to
|
Chris@0
|
403 // be used (which also improves performance, since any configured PHP
|
Chris@0
|
404 // include_paths will not be consulted). Retain the relative originating
|
Chris@0
|
405 // directory being scanned, so relative paths can be reconstructed below
|
Chris@0
|
406 // (all paths are expected to be relative to $this->root).
|
Chris@0
|
407 $dir_prefix = ($dir == '' ? '' : "$dir/");
|
Chris@0
|
408 $absolute_dir = ($dir == '' ? $this->root : $this->root . "/$dir");
|
Chris@0
|
409
|
Chris@0
|
410 if (!is_dir($absolute_dir)) {
|
Chris@0
|
411 return $files;
|
Chris@0
|
412 }
|
Chris@0
|
413 // Use Unix paths regardless of platform, skip dot directories, follow
|
Chris@0
|
414 // symlinks (to allow extensions to be linked from elsewhere), and return
|
Chris@0
|
415 // the RecursiveDirectoryIterator instance to have access to getSubPath(),
|
Chris@0
|
416 // since SplFileInfo does not support relative paths.
|
Chris@0
|
417 $flags = \FilesystemIterator::UNIX_PATHS;
|
Chris@0
|
418 $flags |= \FilesystemIterator::SKIP_DOTS;
|
Chris@0
|
419 $flags |= \FilesystemIterator::FOLLOW_SYMLINKS;
|
Chris@0
|
420 $flags |= \FilesystemIterator::CURRENT_AS_SELF;
|
Chris@0
|
421 $directory_iterator = new \RecursiveDirectoryIterator($absolute_dir, $flags);
|
Chris@0
|
422
|
Chris@0
|
423 // Allow directories specified in settings.php to be ignored. You can use
|
Chris@0
|
424 // this to not check for files in common special-purpose directories. For
|
Chris@0
|
425 // example, node_modules and bower_components. Ignoring irrelevant
|
Chris@0
|
426 // directories is a performance boost.
|
Chris@0
|
427 $ignore_directories = Settings::get('file_scan_ignore_directories', []);
|
Chris@0
|
428
|
Chris@0
|
429 // Filter the recursive scan to discover extensions only.
|
Chris@0
|
430 // Important: Without a RecursiveFilterIterator, RecursiveDirectoryIterator
|
Chris@0
|
431 // would recurse into the entire filesystem directory tree without any kind
|
Chris@0
|
432 // of limitations.
|
Chris@0
|
433 $filter = new RecursiveExtensionFilterIterator($directory_iterator, $ignore_directories);
|
Chris@0
|
434 $filter->acceptTests($include_tests);
|
Chris@0
|
435
|
Chris@0
|
436 // The actual recursive filesystem scan is only invoked by instantiating the
|
Chris@0
|
437 // RecursiveIteratorIterator.
|
Chris@0
|
438 $iterator = new \RecursiveIteratorIterator($filter,
|
Chris@0
|
439 \RecursiveIteratorIterator::LEAVES_ONLY,
|
Chris@0
|
440 // Suppress filesystem errors in case a directory cannot be accessed.
|
Chris@0
|
441 \RecursiveIteratorIterator::CATCH_GET_CHILD
|
Chris@0
|
442 );
|
Chris@0
|
443
|
Chris@0
|
444 foreach ($iterator as $key => $fileinfo) {
|
Chris@0
|
445 // All extension names in Drupal have to be valid PHP function names due
|
Chris@0
|
446 // to the module hook architecture.
|
Chris@0
|
447 if (!preg_match(static::PHP_FUNCTION_PATTERN, $fileinfo->getBasename('.info.yml'))) {
|
Chris@0
|
448 continue;
|
Chris@0
|
449 }
|
Chris@0
|
450
|
Chris@0
|
451 if ($this->fileCache && $cached_extension = $this->fileCache->get($fileinfo->getPathName())) {
|
Chris@0
|
452 $files[$cached_extension->getType()][$key] = $cached_extension;
|
Chris@0
|
453 continue;
|
Chris@0
|
454 }
|
Chris@0
|
455
|
Chris@0
|
456 // Determine extension type from info file.
|
Chris@0
|
457 $type = FALSE;
|
Chris@0
|
458 $file = $fileinfo->openFile('r');
|
Chris@0
|
459 while (!$type && !$file->eof()) {
|
Chris@0
|
460 preg_match('@^type:\s*(\'|")?(\w+)\1?\s*$@', $file->fgets(), $matches);
|
Chris@0
|
461 if (isset($matches[2])) {
|
Chris@0
|
462 $type = $matches[2];
|
Chris@0
|
463 }
|
Chris@0
|
464 }
|
Chris@0
|
465 if (empty($type)) {
|
Chris@0
|
466 continue;
|
Chris@0
|
467 }
|
Chris@0
|
468 $name = $fileinfo->getBasename('.info.yml');
|
Chris@0
|
469 $pathname = $dir_prefix . $fileinfo->getSubPathname();
|
Chris@0
|
470
|
Chris@0
|
471 // Determine whether the extension has a main extension file.
|
Chris@0
|
472 // For theme engines, the file extension is .engine.
|
Chris@0
|
473 if ($type == 'theme_engine') {
|
Chris@0
|
474 $filename = $name . '.engine';
|
Chris@0
|
475 }
|
Chris@0
|
476 // For profiles/modules/themes, it is the extension type.
|
Chris@0
|
477 else {
|
Chris@0
|
478 $filename = $name . '.' . $type;
|
Chris@0
|
479 }
|
Chris@0
|
480 if (!file_exists($this->root . '/' . dirname($pathname) . '/' . $filename)) {
|
Chris@0
|
481 $filename = NULL;
|
Chris@0
|
482 }
|
Chris@0
|
483
|
Chris@0
|
484 $extension = new Extension($this->root, $type, $pathname, $filename);
|
Chris@0
|
485
|
Chris@0
|
486 // Track the originating directory for sorting purposes.
|
Chris@0
|
487 $extension->subpath = $fileinfo->getSubPath();
|
Chris@0
|
488 $extension->origin = $dir;
|
Chris@0
|
489
|
Chris@0
|
490 $files[$type][$key] = $extension;
|
Chris@0
|
491
|
Chris@0
|
492 if ($this->fileCache) {
|
Chris@0
|
493 $this->fileCache->set($fileinfo->getPathName(), $extension);
|
Chris@0
|
494 }
|
Chris@0
|
495 }
|
Chris@0
|
496 return $files;
|
Chris@0
|
497 }
|
Chris@0
|
498
|
Chris@0
|
499 }
|