-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoload.php
More file actions
95 lines (78 loc) · 2.96 KB
/
Copy pathautoload.php
File metadata and controls
95 lines (78 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
* Automatically register dependent standards for PHPCS.
*/
use Composer\InstalledVersions;
use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Util\Standards;
$autoloaders = [
__DIR__ . '/vendor/autoload.php', // standalone development of the standard
__DIR__ . '/../../autoload.php', // installed in a project: vendor/epiphyt/coding-standard/../../autoload.php = vendor/autoload.php
];
foreach ($autoloaders as $autoloader) {
if (file_exists($autoloader)) {
require_once $autoloader;
break;
}
}
/**
* Get the absolute path of an installed Composer package.
*
* Resolves the path independently of the current working directory, no matter
* whether the standard is developed standalone (dependencies live in its own
* vendor directory), installed as a dependency of a project (dependencies are
* siblings inside the project's vendor directory) or installed globally.
*
* @param string $package Package name, e.g. "slevomat/coding-standard"
* @return string Absolute path to the package, or an empty string if not found
*/
$getPackagePath = static function ($package) {
$candidates = [];
if (class_exists(InstalledVersions::class) && InstalledVersions::isInstalled($package)) {
$candidates[] = (string) InstalledVersions::getInstallPath($package);
}
$candidates[] = __DIR__ . '/vendor/' . $package; // standalone development of the standard
$candidates[] = dirname(__DIR__, 2) . '/' . $package; // installed in a project
foreach ($candidates as $candidate) {
if ($candidate !== '' && is_dir($candidate)) {
return (string) realpath($candidate);
}
}
return '';
};
/**
* Dependent standards, mapped to one of the standards they provide.
*
* The standard name is used to check whether the project already provides it,
* in which case the project's version takes precedence.
*/
$dependencies = [
'phpcompatibility/php-compatibility' => 'PHPCompatibility',
'phpcompatibility/phpcompatibility-paragonie' => 'PHPCompatibilityParagonieRandomCompat',
'phpcompatibility/phpcompatibility-wp' => 'PHPCompatibilityWP',
'phpcsstandards/phpcsextra' => 'Universal',
'phpcsstandards/phpcsutils' => 'PHPCSUtils',
'slevomat/coding-standard' => 'SlevomatCodingStandard',
'wp-coding-standards/wpcs' => 'WordPress',
];
$installed = Config::getConfigData('installed_paths');
if (!is_string($installed)) {
$installed = '';
}
$paths = array_filter(explode(',', $installed));
$paths[] = __DIR__;
$installedStandards = Standards::getInstalledStandardDetails(true);
foreach ($dependencies as $package => $standard) {
// Only add it if it isn't already provided by the project.
if (isset($installedStandards[$standard])) {
continue;
}
$path = $getPackagePath($package);
if ($path !== '') {
$paths[] = $path;
}
}
$paths = array_unique(array_filter($paths));
if (!empty($paths)) {
Config::setConfigData('installed_paths', implode(',', $paths), true);
}