Unverified Commit fa92bcab authored by Hussain Abbas's avatar Hussain Abbas
Browse files

Issue #3160022: Add PSR-4 autoloading for Drupal modules to preload script

parent 68edff09
Loading
Loading
Loading
Loading
+58 −0
Changes for assets/preload.php.stub: 58 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php
/**
 * Preloader Script 2.x
 *
 * This file is generated automatically by the Preloader package.
 *
 * The following script uses `@mechanism` syntax to preload each file in this list into Opcache.
 * To full enable preload, add this file to your `php.ini` in `opcache.preload` key to preload
 * this list of files PHP at startup. This file also includes some information about Opcache.
 *
 *
 * Add (or update) this line in `php.ini`:
 *
 *     opcache.preload=@output
 *
 *
 * --- Config ---
 * Generated at: @generated_at
 * Opcache
 *     - Used Memory: @opcache_memory_used MB
 *     - Free Memory: @opcache_memory_free MB
 *     - Wasted Memory: @opcache_memory_wasted MB
 *     - Cached files: @opcache_files
 *     - Hit rate: @opcache_hit_rate%
 *     - Misses: @opcache_misses
 * Preloader config
 *     - Memory limit: @preloader_memory_limit
 *     - Overwrite: @preloader_overwrite
 *     - Files excluded: @preloader_excluded
 *     - Files appended: @preloader_appended
 *
 *
 * For more information:
 * @see https://github.com/darkghosthunter/preloader
 */

/** @var \Composer\Autoload\ClassLoader $loader */
$loader = @autoload

@psr4_loader

$files = [@list];

foreach ($files as $file) {
    try {
        if (!(is_file($file) && is_readable($file))) {
            @failure
        }
        require_once $file;
    } catch (\Throwable $e) {
        echo 'Preloader Script has stopped with an error:' . \PHP_EOL;
        echo 'Message: ' . $e->getMessage() . \PHP_EOL;
        echo 'File: ' . $file . \PHP_EOL;

        throw $e;
    }
}
+1 −1
Changes for preloader.services.yml: 1 added line, 1 removed line.
Original line number Diff line number Diff line
services:
  preloader.generator:
    class: Drupal\preloader\PreloaderGenerator
    arguments: ['@file_system']
    arguments: ['@file_system', '@extension.list.module']
+43 −0
Changes for src/DrupalPreloaderCompiler.php: 43 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\preloader;

use DarkGhostHunter\Preloader\PreloaderCompiler;
use Drupal\Core\Extension\ModuleExtensionList;

class DrupalPreloaderCompiler extends PreloaderCompiler {

  /**
   * @var \Drupal\Core\Extension\ModuleExtensionList
   */
  protected $moduleExtensionList;

  public function __construct(ModuleExtensionList $extension_list) {
    $this->moduleExtensionList = $extension_list;
  }

  /**
   * {@inheritdoc}
   */
  public function compile(): string {
    $template = file_get_contents(__DIR__ . '/../assets/preload.php.stub');

    $psr4_loader = '';
    foreach ($this->moduleExtensionList->getList() as $name => $extension) {
      $path = DRUPAL_ROOT . '/' . $extension->getPath() . '/src';
      $psr4_loader .= "\$loader->addPsr4('Drupal\\{$name}\\\\', '{$path}');" . \PHP_EOL;
    }

    $replacing = array_merge($this->preloaderConfig, $this->opcacheConfig, [
      '@output' => $this->scriptRealPath(),
      '@generated_at' => date('Y-m-d H:i:s e'),
      '@autoload' => 'require_once \'' . realpath($this->autoloader) . '\';',
      '@list' => $this->parseList(),
      '@failure' => $this->ignoreNotFound ? 'continue;' : 'throw new \Exception("{$file} does not exist or is unreadable.");',
      '@psr4_loader' => $psr4_loader,
    ]);

    return str_replace(array_keys($replacing), $replacing, $template);
  }

}
+22 −2
Changes for src/PreloaderGenerator.php: 22 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -2,7 +2,10 @@

namespace Drupal\preloader;

use DarkGhostHunter\Preloader\Opcache;
use DarkGhostHunter\Preloader\Preloader;
use DarkGhostHunter\Preloader\PreloaderLister;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\File\FileSystemInterface;
use Symfony\Component\Finder\Finder;

@@ -28,11 +31,28 @@ class PreloaderGenerator {
   *
   * @param \Drupal\Core\File\FileSystemInterface $file_system
   *   The file system service.
   * @param \Drupal\Core\Extension\ModuleExtensionList $module_extension_list
   *   Module extension list service.
   */
  public function __construct(FileSystemInterface $file_system) {
  public function __construct(FileSystemInterface $file_system, ModuleExtensionList $module_extension_list) {
    $this->fileSystem = $file_system;
    $this->preloader = Preloader::make();
    $this->setupPreloader($module_extension_list);
  }

  /**
   * Setup the preloader generation script.
   *
   * @param \Drupal\Core\Extension\ModuleExtensionList $module_extension_list
   *   Module extension list service.
   */
  protected function setupPreloader(ModuleExtensionList $module_extension_list): void {
    $this->preloader = new Preloader(
      new DrupalPreloaderCompiler($module_extension_list),
      new PreloaderLister(),
      new Opcache()
    );
    $this->preloader->selfExclude();
    $this->preloader->ignoreNotFound();
    $this->setMemoryLimit();
  }