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