Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 36 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| AutoloadFileWriter | |
0.00% |
0 / 36 |
|
0.00% |
0 / 2 |
72 | |
0.00% |
0 / 1 |
| copyAutoloaderFiles | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
42 | |||
| prepareAutoloaderFile | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Autoloader file writer. |
| 4 | * |
| 5 | * @package automattic/jetpack-autoloader |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Autoloader; |
| 9 | |
| 10 | use Composer\IO\IOInterface; |
| 11 | |
| 12 | /** |
| 13 | * Class AutoloadFileWriter. |
| 14 | */ |
| 15 | class AutoloadFileWriter { |
| 16 | |
| 17 | /** |
| 18 | * The file comment to use. |
| 19 | */ |
| 20 | const COMMENT = <<<'AUTOLOADER_COMMENT' |
| 21 | /** |
| 22 | * This file was automatically generated by automattic/jetpack-autoloader. |
| 23 | * |
| 24 | * @package automattic/jetpack-autoloader |
| 25 | */ |
| 26 | |
| 27 | AUTOLOADER_COMMENT; |
| 28 | |
| 29 | /** |
| 30 | * Copies autoloader files and replaces any placeholders in them. |
| 31 | * |
| 32 | * @param IOInterface|null $io An IO for writing to. |
| 33 | * @param string $outDir The directory to place the autoloader files in. |
| 34 | * @param string $suffix The suffix to use in the autoloader's namespace. |
| 35 | */ |
| 36 | public static function copyAutoloaderFiles( $io, $outDir, $suffix ) { |
| 37 | $renameList = array( |
| 38 | 'autoload.php' => '../autoload_packages.php', |
| 39 | ); |
| 40 | $ignoreList = array( |
| 41 | 'AutoloadGenerator.php', |
| 42 | 'AutoloadProcessor.php', |
| 43 | 'CustomAutoloaderPlugin.php', |
| 44 | 'ManifestGenerator.php', |
| 45 | 'AutoloadFileWriter.php', |
| 46 | ); |
| 47 | |
| 48 | // Copy all of the autoloader files. |
| 49 | $files = scandir( __DIR__ ); |
| 50 | foreach ( $files as $file ) { |
| 51 | // Only PHP files will be copied. |
| 52 | if ( substr( $file, -4 ) !== '.php' ) { |
| 53 | continue; |
| 54 | } |
| 55 | |
| 56 | if ( in_array( $file, $ignoreList, true ) ) { |
| 57 | continue; |
| 58 | } |
| 59 | |
| 60 | $newFile = $renameList[ $file ] ?? $file; |
| 61 | $content = self::prepareAutoloaderFile( $file, $suffix ); |
| 62 | |
| 63 | $written = file_put_contents( $outDir . '/' . $newFile, $content ); |
| 64 | if ( $io ) { |
| 65 | if ( $written ) { |
| 66 | $io->writeError( " <info>Generated: $newFile</info>" ); |
| 67 | } else { |
| 68 | $io->writeError( " <error>Error: $newFile</error>" ); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Prepares an autoloader file to be written to the destination. |
| 76 | * |
| 77 | * @param String $filename a file to prepare. |
| 78 | * @param String $suffix Unique suffix used in the namespace. |
| 79 | * |
| 80 | * @return string |
| 81 | */ |
| 82 | private static function prepareAutoloaderFile( $filename, $suffix ) { |
| 83 | $header = self::COMMENT; |
| 84 | $header .= PHP_EOL; |
| 85 | if ( $suffix === 'Current' ) { |
| 86 | // Unit testing. |
| 87 | $header .= 'namespace Automattic\Jetpack\Autoloader\jpCurrent;'; |
| 88 | } else { |
| 89 | $header .= 'namespace Automattic\Jetpack\Autoloader\jp' . $suffix . '\al' . preg_replace( '/[^0-9a-zA-Z]/', '_', AutoloadGenerator::VERSION ) . ';'; |
| 90 | } |
| 91 | $header .= PHP_EOL . PHP_EOL; |
| 92 | |
| 93 | $sourceLoader = fopen( __DIR__ . '/' . $filename, 'r' ); |
| 94 | $file_contents = stream_get_contents( $sourceLoader ); |
| 95 | return str_replace( |
| 96 | '/* HEADER */', |
| 97 | $header, |
| 98 | $file_contents |
| 99 | ); |
| 100 | } |
| 101 | } |