PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/packages/woocommerce-blocks/vendor/automattic/jetpack-autoloader/src/AutoloadGenerator.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 393 lines | 218 code | 47 blank | 128 comment | 23 complexity | 74951ab776ac6763310d822669f25eb5 MD5 | raw file
  1. <?php // phpcs:ignore WordPress.Files.FileName
  2. /**
  3. * Autoloader Generator.
  4. *
  5. * @package automattic/jetpack-autoloader
  6. */
  7. // phpcs:disable PHPCompatibility.Keywords.NewKeywords.t_useFound
  8. // phpcs:disable PHPCompatibility.LanguageConstructs.NewLanguageConstructs.t_ns_separatorFound
  9. // phpcs:disable PHPCompatibility.FunctionDeclarations.NewClosure.Found
  10. // phpcs:disable PHPCompatibility.Keywords.NewKeywords.t_namespaceFound
  11. // phpcs:disable PHPCompatibility.Keywords.NewKeywords.t_dirFound
  12. // phpcs:disable WordPress.Files.FileName.InvalidClassFileName
  13. // phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_var_export
  14. // phpcs:disable WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents
  15. // phpcs:disable WordPress.WP.AlternativeFunctions.file_system_read_fopen
  16. // phpcs:disable WordPress.WP.AlternativeFunctions.file_system_read_fwrite
  17. // phpcs:disable WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
  18. // phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
  19. // phpcs:disable WordPress.NamingConventions.ValidVariableName.InterpolatedVariableNotSnakeCase
  20. // phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
  21. // phpcs:disable WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase
  22. namespace Automattic\Jetpack\Autoloader;
  23. use Composer\Autoload\ClassMapGenerator;
  24. use Composer\Composer;
  25. use Composer\Config;
  26. use Composer\Installer\InstallationManager;
  27. use Composer\IO\IOInterface;
  28. use Composer\Package\PackageInterface;
  29. use Composer\Repository\InstalledRepositoryInterface;
  30. use Composer\Util\Filesystem;
  31. use Composer\Util\PackageSorter;
  32. /**
  33. * Class AutoloadGenerator.
  34. */
  35. class AutoloadGenerator {
  36. /**
  37. * The filesystem utility.
  38. *
  39. * @var Filesystem
  40. */
  41. private $filesystem;
  42. /**
  43. * Instantiate an AutoloadGenerator object.
  44. *
  45. * @param IOInterface $io IO object.
  46. */
  47. public function __construct( IOInterface $io = null ) {
  48. $this->io = $io;
  49. $this->filesystem = new Filesystem();
  50. }
  51. /**
  52. * Dump the Jetpack autoloader files.
  53. *
  54. * @param Composer $composer The Composer object.
  55. * @param Config $config Config object.
  56. * @param InstalledRepositoryInterface $localRepo Installed Repository object.
  57. * @param PackageInterface $mainPackage Main Package object.
  58. * @param InstallationManager $installationManager Manager for installing packages.
  59. * @param string $targetDir Path to the current target directory.
  60. * @param bool $scanPsrPackages Whether or not PSR packages should be converted to a classmap.
  61. * @param string $suffix The autoloader suffix.
  62. */
  63. public function dump(
  64. Composer $composer,
  65. Config $config,
  66. InstalledRepositoryInterface $localRepo,
  67. PackageInterface $mainPackage,
  68. InstallationManager $installationManager,
  69. $targetDir,
  70. $scanPsrPackages = false,
  71. $suffix = null
  72. ) {
  73. $this->filesystem->ensureDirectoryExists( $config->get( 'vendor-dir' ) );
  74. $packageMap = $composer->getAutoloadGenerator()->buildPackageMap( $installationManager, $mainPackage, $localRepo->getCanonicalPackages() );
  75. $autoloads = $this->parseAutoloads( $packageMap, $mainPackage );
  76. // Convert the autoloads into a format that the manifest generator can consume more easily.
  77. $basePath = $this->filesystem->normalizePath( realpath( getcwd() ) );
  78. $vendorPath = $this->filesystem->normalizePath( realpath( $config->get( 'vendor-dir' ) ) );
  79. $processedAutoloads = $this->processAutoloads( $autoloads, $scanPsrPackages, $vendorPath, $basePath );
  80. unset( $packageMap, $autoloads );
  81. // Make sure none of the legacy files remain that can lead to problems with the autoloader.
  82. $this->removeLegacyFiles( $vendorPath );
  83. // Write all of the files now that we're done.
  84. $this->writeAutoloaderFiles( $vendorPath . '/jetpack-autoloader/', $suffix );
  85. $this->writeManifests( $vendorPath . '/' . $targetDir, $processedAutoloads );
  86. if ( ! $scanPsrPackages ) {
  87. $this->io->writeError( '<warning>You are generating an unoptimized autoloader. If this is a production build, consider using the -o option.</warning>' );
  88. }
  89. }
  90. /**
  91. * Compiles an ordered list of namespace => path mappings
  92. *
  93. * @param array $packageMap Array of array(package, installDir-relative-to-composer.json).
  94. * @param PackageInterface $mainPackage Main package instance.
  95. *
  96. * @return array The list of path mappings.
  97. */
  98. public function parseAutoloads( array $packageMap, PackageInterface $mainPackage ) {
  99. $rootPackageMap = array_shift( $packageMap );
  100. $sortedPackageMap = $this->sortPackageMap( $packageMap );
  101. $sortedPackageMap[] = $rootPackageMap;
  102. array_unshift( $packageMap, $rootPackageMap );
  103. $psr0 = $this->parseAutoloadsType( $packageMap, 'psr-0', $mainPackage );
  104. $psr4 = $this->parseAutoloadsType( $packageMap, 'psr-4', $mainPackage );
  105. $classmap = $this->parseAutoloadsType( array_reverse( $sortedPackageMap ), 'classmap', $mainPackage );
  106. $files = $this->parseAutoloadsType( $sortedPackageMap, 'files', $mainPackage );
  107. krsort( $psr0 );
  108. krsort( $psr4 );
  109. return array(
  110. 'psr-0' => $psr0,
  111. 'psr-4' => $psr4,
  112. 'classmap' => $classmap,
  113. 'files' => $files,
  114. );
  115. }
  116. /**
  117. * Sorts packages by dependency weight
  118. *
  119. * Packages of equal weight retain the original order
  120. *
  121. * @param array $packageMap The package map.
  122. *
  123. * @return array
  124. */
  125. protected function sortPackageMap( array $packageMap ) {
  126. $packages = array();
  127. $paths = array();
  128. foreach ( $packageMap as $item ) {
  129. list( $package, $path ) = $item;
  130. $name = $package->getName();
  131. $packages[ $name ] = $package;
  132. $paths[ $name ] = $path;
  133. }
  134. $sortedPackages = PackageSorter::sortPackages( $packages );
  135. $sortedPackageMap = array();
  136. foreach ( $sortedPackages as $package ) {
  137. $name = $package->getName();
  138. $sortedPackageMap[] = array( $packages[ $name ], $paths[ $name ] );
  139. }
  140. return $sortedPackageMap;
  141. }
  142. /**
  143. * Returns the file identifier.
  144. *
  145. * @param PackageInterface $package The package instance.
  146. * @param string $path The path.
  147. */
  148. protected function getFileIdentifier( PackageInterface $package, $path ) {
  149. return md5( $package->getName() . ':' . $path );
  150. }
  151. /**
  152. * Returns the path code for the given path.
  153. *
  154. * @param Filesystem $filesystem The filesystem instance.
  155. * @param string $basePath The base path.
  156. * @param string $vendorPath The vendor path.
  157. * @param string $path The path.
  158. *
  159. * @return string The path code.
  160. */
  161. protected function getPathCode( Filesystem $filesystem, $basePath, $vendorPath, $path ) {
  162. if ( ! $filesystem->isAbsolutePath( $path ) ) {
  163. $path = $basePath . '/' . $path;
  164. }
  165. $path = $filesystem->normalizePath( $path );
  166. $baseDir = '';
  167. if ( 0 === strpos( $path . '/', $vendorPath . '/' ) ) {
  168. $path = substr( $path, strlen( $vendorPath ) );
  169. $baseDir = '$vendorDir';
  170. if ( false !== $path ) {
  171. $baseDir .= ' . ';
  172. }
  173. } else {
  174. $path = $filesystem->normalizePath( $filesystem->findShortestPath( $basePath, $path, true ) );
  175. if ( ! $filesystem->isAbsolutePath( $path ) ) {
  176. $baseDir = '$baseDir . ';
  177. $path = '/' . $path;
  178. }
  179. }
  180. if ( strpos( $path, '.phar' ) !== false ) {
  181. $baseDir = "'phar://' . " . $baseDir;
  182. }
  183. return $baseDir . ( ( false !== $path ) ? var_export( $path, true ) : '' );
  184. }
  185. /**
  186. * This function differs from the composer parseAutoloadsType in that beside returning the path.
  187. * It also return the path and the version of a package.
  188. *
  189. * Supports PSR-4, PSR-0, and classmap parsing.
  190. *
  191. * @param array $packageMap Map of all the packages.
  192. * @param string $type Type of autoloader to use.
  193. * @param PackageInterface $mainPackage Instance of the Package Object.
  194. *
  195. * @return array
  196. */
  197. protected function parseAutoloadsType( array $packageMap, $type, PackageInterface $mainPackage ) {
  198. $autoloads = array();
  199. foreach ( $packageMap as $item ) {
  200. list($package, $installPath) = $item;
  201. $autoload = $package->getAutoload();
  202. if ( $package === $mainPackage ) {
  203. $autoload = array_merge_recursive( $autoload, $package->getDevAutoload() );
  204. }
  205. if ( null !== $package->getTargetDir() && $package !== $mainPackage ) {
  206. $installPath = substr( $installPath, 0, -strlen( '/' . $package->getTargetDir() ) );
  207. }
  208. if ( in_array( $type, array( 'psr-4', 'psr-0' ), true ) && isset( $autoload[ $type ] ) && is_array( $autoload[ $type ] ) ) {
  209. foreach ( $autoload[ $type ] as $namespace => $paths ) {
  210. $paths = is_array( $paths ) ? $paths : array( $paths );
  211. foreach ( $paths as $path ) {
  212. $relativePath = empty( $installPath ) ? ( empty( $path ) ? '.' : $path ) : $installPath . '/' . $path;
  213. $autoloads[ $namespace ][] = array(
  214. 'path' => $relativePath,
  215. 'version' => $package->getVersion(), // Version of the class comes from the package - should we try to parse it?
  216. );
  217. }
  218. }
  219. }
  220. if ( 'classmap' === $type && isset( $autoload['classmap'] ) && is_array( $autoload['classmap'] ) ) {
  221. foreach ( $autoload['classmap'] as $paths ) {
  222. $paths = is_array( $paths ) ? $paths : array( $paths );
  223. foreach ( $paths as $path ) {
  224. $relativePath = empty( $installPath ) ? ( empty( $path ) ? '.' : $path ) : $installPath . '/' . $path;
  225. $autoloads[] = array(
  226. 'path' => $relativePath,
  227. 'version' => $package->getVersion(), // Version of the class comes from the package - should we try to parse it?
  228. );
  229. }
  230. }
  231. }
  232. if ( 'files' === $type && isset( $autoload['files'] ) && is_array( $autoload['files'] ) ) {
  233. foreach ( $autoload['files'] as $paths ) {
  234. $paths = is_array( $paths ) ? $paths : array( $paths );
  235. foreach ( $paths as $path ) {
  236. $relativePath = empty( $installPath ) ? ( empty( $path ) ? '.' : $path ) : $installPath . '/' . $path;
  237. $autoloads[ $this->getFileIdentifier( $package, $path ) ] = array(
  238. 'path' => $relativePath,
  239. 'version' => $package->getVersion(), // Version of the file comes from the package - should we try to parse it?
  240. );
  241. }
  242. }
  243. }
  244. }
  245. return $autoloads;
  246. }
  247. /**
  248. * Given Composer's autoloads this will convert them to a version that we can use to generate the manifests.
  249. *
  250. * When the $scanPsrPackages argument is true, PSR-4 namespaces are converted to classmaps. When $scanPsrPackages
  251. * is false, PSR-4 namespaces are not converted to classmaps.
  252. *
  253. * PSR-0 namespaces are always converted to classmaps.
  254. *
  255. * @param array $autoloads The autoloads we want to process.
  256. * @param bool $scanPsrPackages Whether or not PSR-4 packages should be converted to a classmap.
  257. * @param string $vendorPath The path to the vendor directory.
  258. * @param string $basePath The path to the current directory.
  259. *
  260. * @return array $processedAutoloads
  261. */
  262. private function processAutoloads( $autoloads, $scanPsrPackages, $vendorPath, $basePath ) {
  263. $processor = new AutoloadProcessor(
  264. function ( $path, $excludedClasses, $namespace ) use ( $basePath ) {
  265. $dir = $this->filesystem->normalizePath(
  266. $this->filesystem->isAbsolutePath( $path ) ? $path : $basePath . '/' . $path
  267. );
  268. return ClassMapGenerator::createMap(
  269. $dir,
  270. $excludedClasses,
  271. null, // Don't pass the IOInterface since the normal autoload generation will have reported already.
  272. empty( $namespace ) ? null : $namespace
  273. );
  274. },
  275. function ( $path ) use ( $basePath, $vendorPath ) {
  276. return $this->getPathCode( $this->filesystem, $basePath, $vendorPath, $path );
  277. }
  278. );
  279. return array(
  280. 'psr-4' => $processor->processPsr4Packages( $autoloads, $scanPsrPackages ),
  281. 'classmap' => $processor->processClassmap( $autoloads, $scanPsrPackages ),
  282. 'files' => $processor->processFiles( $autoloads ),
  283. );
  284. }
  285. /**
  286. * Removes all of the legacy autoloader files so they don't cause any problems.
  287. *
  288. * @param string $outDir The directory legacy files are written to.
  289. */
  290. private function removeLegacyFiles( $outDir ) {
  291. $files = array(
  292. 'autoload_functions.php',
  293. 'class-autoloader-handler.php',
  294. 'class-classes-handler.php',
  295. 'class-files-handler.php',
  296. 'class-plugins-handler.php',
  297. 'class-version-selector.php',
  298. );
  299. foreach ( $files as $file ) {
  300. $this->filesystem->remove( $outDir . '/' . $file );
  301. }
  302. }
  303. /**
  304. * Writes all of the autoloader files to disk.
  305. *
  306. * @param string $outDir The directory to write to.
  307. * @param string $suffix The unique autoloader suffix.
  308. */
  309. private function writeAutoloaderFiles( $outDir, $suffix ) {
  310. $this->io->writeError( "<info>Generating jetpack autoloader ($outDir)</info>" );
  311. // We will remove all autoloader files to generate this again.
  312. $this->filesystem->emptyDirectory( $outDir );
  313. // Write the autoloader files.
  314. AutoloadFileWriter::copyAutoloaderFiles( $this->io, $outDir, $suffix );
  315. }
  316. /**
  317. * Writes all of the manifest files to disk.
  318. *
  319. * @param string $outDir The directory to write to.
  320. * @param array $processedAutoloads The processed autoloads.
  321. */
  322. private function writeManifests( $outDir, $processedAutoloads ) {
  323. $this->io->writeError( "<info>Generating jetpack autoloader manifests ($outDir)</info>" );
  324. $manifestFiles = array(
  325. 'classmap' => 'jetpack_autoload_classmap.php',
  326. 'psr-4' => 'jetpack_autoload_psr4.php',
  327. 'files' => 'jetpack_autoload_filemap.php',
  328. );
  329. foreach ( $manifestFiles as $key => $file ) {
  330. // Make sure the file doesn't exist so it isn't there if we don't write it.
  331. $this->filesystem->remove( $outDir . '/' . $file );
  332. if ( empty( $processedAutoloads[ $key ] ) ) {
  333. continue;
  334. }
  335. $content = ManifestGenerator::buildManifest( $key, $file, $processedAutoloads[ $key ] );
  336. if ( empty( $content ) ) {
  337. continue;
  338. }
  339. if ( file_put_contents( $outDir . '/' . $file, $content ) ) {
  340. $this->io->writeError( " <info>Generated: $file</info>" );
  341. } else {
  342. $this->io->writeError( " <error>Error: $file</error>" );
  343. }
  344. }
  345. }
  346. }