Completed
Push — update/show-recurring-payments... ( 106a5e...b030b6 )
by
unknown
415:44 queued 407:29
created

AutoloadFileWriter::copyAutoloaderFiles()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 9
nop 3
dl 0
loc 37
rs 8.3946
c 0
b 0
f 0
1
<?php // phpcs:ignore WordPress.Files.FileName
2
/**
3
 * Autoloader file writer.
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
// phpcs:disable WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents
16
// phpcs:disable WordPress.WP.AlternativeFunctions.file_system_read_fopen
17
// phpcs:disable WordPress.WP.AlternativeFunctions.file_system_read_fwrite
18
19
namespace Automattic\Jetpack\Autoloader;
20
21
/**
22
 * Class AutoloadFileWriter.
23
 */
24
class AutoloadFileWriter {
25
26
	/**
27
	 * The file comment to use.
28
	 */
29
	const COMMENT = <<<AUTOLOADER_COMMENT
30
/**
31
 * This file was automatically generated by automattic/jetpack-autoloader.
32
 *
33
 * @package automattic/jetpack-autoloader
34
 */
35
36
AUTOLOADER_COMMENT;
37
38
	/**
39
	 * Copies autoloader files and replaces any placeholders in them.
40
	 *
41
	 * @param IOInterface|null $io An IO for writing to.
42
	 * @param string           $outDir The directory to place the autoloader files in.
43
	 * @param string           $suffix The suffix to use in the autoloader's namespace.
44
	 */
45
	public static function copyAutoloaderFiles( $io, $outDir, $suffix ) {
46
		$renameList = array(
47
			'autoload.php' => '../autoload_packages.php',
48
		);
49
		$ignoreList = array(
50
			'AutoloadGenerator.php',
51
			'AutoloadProcessor.php',
52
			'CustomAutoloaderPlugin.php',
53
			'ManifestGenerator.php',
54
			'AutoloadFileWriter.php',
55
		);
56
57
		// Copy all of the autoloader files.
58
		$files = scandir( __DIR__ );
59
		foreach ( $files as $file ) {
60
			// Only PHP files will be copied.
61
			if ( substr( $file, -4 ) !== '.php' ) {
62
				continue;
63
			}
64
65
			if ( in_array( $file, $ignoreList, true ) ) {
66
				continue;
67
			}
68
69
			$newFile = isset( $renameList[ $file ] ) ? $renameList[ $file ] : $file;
70
			$content = self::prepareAutoloaderFile( $file, $suffix );
71
72
			$written = file_put_contents( $outDir . '/' . $newFile, $content );
73
			if ( $io ) {
74
				if ( $written ) {
75
					$io->writeError( "  <info>Generated: $newFile</info>" );
76
				} else {
77
					$io->writeError( "  <error>Error: $newFile</error>" );
78
				}
79
			}
80
		}
81
	}
82
83
	/**
84
	 * Prepares an autoloader file to be written to the destination.
85
	 *
86
	 * @param String $filename a file to prepare.
87
	 * @param String $suffix   Unique suffix used in the namespace.
88
	 *
89
	 * @return string
90
	 */
91
	private static function prepareAutoloaderFile( $filename, $suffix ) {
92
		$header  = self::COMMENT;
93
		$header .= PHP_EOL;
94
		$header .= 'namespace Automattic\Jetpack\Autoloader\jp' . $suffix . ';';
95
		$header .= PHP_EOL . PHP_EOL;
96
97
		$sourceLoader  = fopen( __DIR__ . '/' . $filename, 'r' );
98
		$file_contents = stream_get_contents( $sourceLoader );
99
		return str_replace(
100
			'/* HEADER */',
101
			$header,
102
			$file_contents
103
		);
104
	}
105
}
106