1
|
|
|
<?php // phpcs:ignore WordPress.Files.FileName |
2
|
|
|
/** |
3
|
|
|
* Manifest Generator. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-autoloader |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
// phpcs:disable WordPress.Files.FileName.InvalidClassFileName |
9
|
|
|
// phpcs:disable WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid |
10
|
|
|
// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
11
|
|
|
// phpcs:disable WordPress.NamingConventions.ValidVariableName.InterpolatedVariableNotSnakeCase |
12
|
|
|
// phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase |
13
|
|
|
// phpcs:disable WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase |
14
|
|
|
// phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_var_export |
15
|
|
|
|
16
|
|
|
namespace Automattic\Jetpack\Autoloader; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class ManifestGenerator. |
20
|
|
|
*/ |
21
|
|
|
class ManifestGenerator { |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Builds a manifest file for the given autoloader type. |
25
|
|
|
* |
26
|
|
|
* @param string $autoloaderType The type of autoloader to build a manifest for. |
27
|
|
|
* @param string $fileName The filename of the manifest. |
28
|
|
|
* @param array $content The manifest content to generate using. |
29
|
|
|
* |
30
|
|
|
* @return string|null $manifestFile |
31
|
|
|
* @throws \InvalidArgumentException When an invalid autoloader type is given. |
32
|
|
|
*/ |
33
|
|
|
public static function buildManifest( $autoloaderType, $fileName, $content ) { |
34
|
|
|
if ( empty( $content ) ) { |
35
|
|
|
return null; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
switch ( $autoloaderType ) { |
39
|
|
|
case 'classmap': |
40
|
|
|
case 'files': |
41
|
|
|
return self::buildStandardManifest( $fileName, $content ); |
42
|
|
|
case 'psr-4': |
43
|
|
|
return self::buildPsr4Manifest( $fileName, $content ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
throw new \InvalidArgumentException( 'An invalid manifest type of ' . $autoloaderType . ' was passed!' ); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Builds the contents for the standard manifest file. |
51
|
|
|
* |
52
|
|
|
* @param string $fileName The filename we are building. |
53
|
|
|
* @param array $manifestData The formatted data for the manifest. |
54
|
|
|
* |
55
|
|
|
* @return string|null $manifestFile |
56
|
|
|
*/ |
57
|
|
|
private static function buildStandardManifest( $fileName, $manifestData ) { |
58
|
|
|
$fileContent = PHP_EOL; |
59
|
|
|
foreach ( $manifestData as $key => $data ) { |
60
|
|
|
$key = var_export( $key, true ); |
61
|
|
|
$versionCode = var_export( $data['version'], true ); |
62
|
|
|
$fileContent .= <<<MANIFEST_CODE |
63
|
|
|
$key => array( |
64
|
|
|
'version' => $versionCode, |
65
|
|
|
'path' => {$data['path']} |
66
|
|
|
), |
67
|
|
|
MANIFEST_CODE; |
68
|
|
|
$fileContent .= PHP_EOL; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return self::buildFile( $fileName, $fileContent ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Builds the contents for the PSR-4 manifest file. |
76
|
|
|
* |
77
|
|
|
* @param string $fileName The filename we are building. |
78
|
|
|
* @param array $namespaces The formatted PSR-4 data for the manifest. |
79
|
|
|
* |
80
|
|
|
* @return string|null $manifestFile |
81
|
|
|
*/ |
82
|
|
|
private static function buildPsr4Manifest( $fileName, $namespaces ) { |
83
|
|
|
$fileContent = PHP_EOL; |
84
|
|
|
foreach ( $namespaces as $namespace => $data ) { |
85
|
|
|
$namespaceCode = var_export( $namespace, true ); |
86
|
|
|
$versionCode = var_export( $data['version'], true ); |
87
|
|
|
$pathCode = 'array( ' . implode( ', ', $data['path'] ) . ' )'; |
88
|
|
|
$fileContent .= <<<MANIFEST_CODE |
89
|
|
|
$namespaceCode => array( |
90
|
|
|
'version' => $versionCode, |
91
|
|
|
'path' => $pathCode |
92
|
|
|
), |
93
|
|
|
MANIFEST_CODE; |
94
|
|
|
$fileContent .= PHP_EOL; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return self::buildFile( $fileName, $fileContent ); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Generate the PHP that will be used in the file. |
102
|
|
|
* |
103
|
|
|
* @param string $fileName The filename we are building. |
104
|
|
|
* @param string $content The content to be written into the file. |
105
|
|
|
* |
106
|
|
|
* @return string $fileContent |
107
|
|
|
*/ |
108
|
|
|
private static function buildFile( $fileName, $content ) { |
109
|
|
|
return <<<INCLUDE_FILE |
110
|
|
|
<?php |
111
|
|
|
|
112
|
|
|
// This file `$fileName` was auto generated by automattic/jetpack-autoloader. |
113
|
|
|
|
114
|
|
|
\$vendorDir = dirname(__DIR__); |
115
|
|
|
\$baseDir = dirname(\$vendorDir); |
116
|
|
|
|
117
|
|
|
return array($content); |
118
|
|
|
|
119
|
|
|
INCLUDE_FILE; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|