/** * {@inheritdoc} * @codeCoverageIgnore */ public function findFile($className) { /** * Composer remembers that files don't exist even after they are generated. This clears the entry for * $className so we can check the filesystem again for class existence. */ if ($className[0] === '\\') { $className = substr($className, 1); } $this->autoloader->addClassMap([$className => null]); return $this->autoloader->findFile($className); }
/** * Initializes Maam by generating new class files for all PHP files in the supplied sourcePath * and adds the new classmap to Composer's loader. * * @param ClassLoader $loader Composer's class loader. * @throws RuntimeException * @return void */ public function init(ClassLoader $loader) { if ($this->getMode() === self::MODE_DEVELOPMENT) { $this->runGeneratorCommand(); } $loader->addClassMap(require $this->getGenerationPath() . '/classmap.php'); }
/** * Autoloads all registered extension files with an instance of the app. * * @return void **/ public function autoload($app) { $loader = new ClassLoader(); $mapfile = $this->basefolder . '/vendor/composer/autoload_psr4.php'; if (is_readable($mapfile)) { $map = (require $mapfile); foreach ($map as $namespace => $path) { $loader->setPsr4($namespace, $path); } $mapfile = $this->basefolder . '/vendor/composer/autoload_classmap.php'; if (is_readable($mapfile)) { $map = (require $mapfile); $loader->addClassMap($map); } $loader->register(); } $filepath = $this->basefolder . '/vendor/composer/autoload_files.php'; if (is_readable($filepath)) { $files = (include $filepath); foreach ($files as $file) { try { if (is_readable($file)) { require $file; } } catch (\Exception $e) { $this->logInitFailure('Error importing extension class', $file, $e, Logger::ERROR); } } } }
/** * Discovers all available tests in all extensions. * * @param string $extension * (optional) The name of an extension to limit discovery to; e.g., 'node'. * * @return array * An array of tests keyed by the first @group specified in each test's * PHPDoc comment block, and then keyed by class names. For example: * @code * $groups['block'] => array( * 'Drupal\block\Tests\BlockTest' => array( * 'name' => 'Drupal\block\Tests\BlockTest', * 'description' => 'Tests block UI CRUD functionality.', * 'group' => 'block', * ), * ); * @endcode * * @throws \ReflectionException * If a discovered test class does not match the expected class name. * * @todo Remove singular grouping; retain list of groups in 'group' key. * @see https://www.drupal.org/node/2296615 * @todo Add base class groups 'Kernel' + 'Web', complementing 'PHPUnit'. */ public function getTestClasses($extension = NULL) { if (!isset($extension)) { if ($this->cacheBackend && ($cache = $this->cacheBackend->get('simpletest:discovery:classes'))) { return $cache->data; } } $list = array(); $classmap = $this->findAllClassFiles($extension); // Prevent expensive class loader lookups for each reflected test class by // registering the complete classmap of test classes to the class loader. // This also ensures that test classes are loaded from the discovered // pathnames; a namespace/classname mismatch will throw an exception. $this->classLoader->addClassMap($classmap); foreach ($classmap as $classname => $pathname) { try { $class = new \ReflectionClass($classname); } catch (\ReflectionException $e) { // Re-throw with expected pathname. $message = $e->getMessage() . " in expected {$pathname}"; throw new \ReflectionException($message, $e->getCode(), $e); } // Skip interfaces, abstract classes, and traits. if (!$class->isInstantiable()) { continue; } // Skip non-test classes. if (!$class->isSubclassOf('Drupal\\simpletest\\TestBase') && !$class->isSubclassOf('PHPUnit_Framework_TestCase')) { continue; } $info = static::getTestInfo($class); // Skip this test class if it requires unavailable modules. // @todo PHPUnit skips tests with unmet requirements when executing a test // (instead of excluding them upfront). Refactor test runner to follow // that approach. // @see https://www.drupal.org/node/1273478 if (!empty($info['requires']['module'])) { if (array_diff($info['requires']['module'], $this->availableExtensions['module'])) { continue; } } $list[$info['group']][$classname] = $info; } // Sort the groups and tests within the groups by name. uksort($list, 'strnatcasecmp'); foreach ($list as &$tests) { uksort($tests, 'strnatcasecmp'); } // Allow modules extending core tests to disable originals. \Drupal::moduleHandler()->alter('simpletest', $list); if (!isset($extension)) { if ($this->cacheBackend) { $this->cacheBackend->set('simpletest:discovery:classes', $list); } } return $list; }
/** * Discovers all available tests in all extensions. * * @param string $extension * (optional) The name of an extension to limit discovery to; e.g., 'node'. * * @return array * An array of tests keyed by the first @group specified in each test's * PHPDoc comment block, and then keyed by class names. For example: * @code * $groups['block'] => array( * 'Drupal\block\Tests\BlockTest' => array( * 'name' => 'Drupal\block\Tests\BlockTest', * 'description' => 'Tests block UI CRUD functionality.', * 'group' => 'block', * ), * ); * @endcode * * @throws \ReflectionException * If a discovered test class does not match the expected class name. * * @todo Remove singular grouping; retain list of groups in 'group' key. * @see https://www.drupal.org/node/2296615 * @todo Add base class groups 'Kernel' + 'Web', complementing 'PHPUnit'. */ public function getTestClasses($extension = NULL) { $reader = new SimpleAnnotationReader(); $reader->addNamespace('Drupal\\simpletest\\Annotation'); if (!isset($extension)) { if ($this->cacheBackend && ($cache = $this->cacheBackend->get('simpletest:discovery:classes'))) { return $cache->data; } } $list = array(); $classmap = $this->findAllClassFiles($extension); // Prevent expensive class loader lookups for each reflected test class by // registering the complete classmap of test classes to the class loader. // This also ensures that test classes are loaded from the discovered // pathnames; a namespace/classname mismatch will throw an exception. $this->classLoader->addClassMap($classmap); foreach ($classmap as $classname => $pathname) { $finder = MockFileFinder::create($pathname); $parser = new StaticReflectionParser($classname, $finder, TRUE); try { $info = static::getTestInfo($classname, $parser->getDocComment()); } catch (MissingGroupException $e) { // If the class name ends in Test and is not a migrate table dump. if (preg_match('/Test$/', $classname) && strpos($classname, 'migrate_drupal\\Tests\\Table') === FALSE) { throw $e; } // If the class is @group annotation just skip it. Most likely it is an // abstract class, trait or test fixture. continue; } // Skip this test class if it requires unavailable modules. // @todo PHPUnit skips tests with unmet requirements when executing a test // (instead of excluding them upfront). Refactor test runner to follow // that approach. // @see https://www.drupal.org/node/1273478 if (!empty($info['requires']['module'])) { if (array_diff($info['requires']['module'], $this->availableExtensions['module'])) { continue; } } $list[$info['group']][$classname] = $info; } // Sort the groups and tests within the groups by name. uksort($list, 'strnatcasecmp'); foreach ($list as &$tests) { uksort($tests, 'strnatcasecmp'); } // Allow modules extending core tests to disable originals. \Drupal::moduleHandler()->alter('simpletest', $list); if (!isset($extension)) { if ($this->cacheBackend) { $this->cacheBackend->set('simpletest:discovery:classes', $list); } } return $list; }
public function registerClassMaps($classMap) { if (!$classMap) { return; } if (!is_array($classMap)) { $classMap = [$classMap]; } $loader = new ClassLoader(); $loader->addClassMap($classMap); $loader->register(true); }
/** * Check that the auto loading information is correct. * * @param array $information The autoload information. * * @param string $baseDir The base directory. * * @return bool */ public function validateComposerAutoLoadingClassMap($information, $baseDir) { $result = true; // Scan all directories mentioned and validate the class map against the entries. foreach ($information as $path) { $subPath = str_replace('//', '/', $baseDir . '/' . $path); $classMap = $this->createClassMap($subPath); $this->loader->addClassMap($classMap); $this->classMap = array_merge($this->classMap, $classMap); if (!$this->validateComposerAutoLoadingClassMapClassMap($classMap, $subPath)) { $result = false; } } return $result; }
/** * @covers ::findFile */ public function testFindFile() { $finder = new ClassFinder(); // The full path is returned therefore only tests with // assertStringEndsWith() so the test is portable. $this->assertStringEndsWith('core/tests/Drupal/Tests/UnitTestCase.php', $finder->findFile(UnitTestCase::class)); $class = 'Not\\A\\Class'; $this->assertNull($finder->findFile($class)); // Register an autoloader that can find this class. $loader = new ClassLoader(); $loader->addClassMap([$class => __FILE__]); $loader->register(); $this->assertEquals(__FILE__, $finder->findFile($class)); // This shouldn't prevent us from finding the original file. $this->assertStringEndsWith('core/tests/Drupal/Tests/UnitTestCase.php', $finder->findFile(UnitTestCase::class)); // Clean up the additional autoloader after the test. $loader->unregister(); }
public static function addComposerFiles($dir) { if (file_exists($dir . '/vendor/composer/autoload_namespaces.php')) { $map = (require $dir . '/vendor/composer/autoload_namespaces.php'); foreach ($map as $namespace => $path) { self::$classLoader->set($namespace, $path); } } if (file_exists($dir . '/vendor/composer/autoload_psr4.php')) { $map = (require $dir . '/vendor/composer/autoload_psr4.php'); foreach ($map as $namespace => $path) { self::$classLoader->setPsr4($namespace, $path); } } if (file_exists($dir . '/vendor/composer/autoload_classmap.php')) { $classMap = (require $dir . '/vendor/composer/autoload_classmap.php'); if ($classMap) { self::$classLoader->addClassMap($classMap); } } }
/** * Register core classes with autoloader * @param \Composer\Autoload\ClassLoader $loader */ static function preload_core_with_composer($loader) { $timer = microtime(1); $classes = self::$_preload; foreach ($classes as $classname => &$file) { $file = self::get_public(self::DIR_MODULES . 'core/' . $file . self::DOT_PHP); if (!is_string($classname)) { require $file; unset($classes[$classname]); } } $loader->addClassMap($classes); self::dprint("autoload_core - done %.5f", array(microtime(1) - $timer)); }
public function addClassInFile(string $className, string $filePath) { $this->classLoader->addClassMap([$className => $filePath]); }
/** * Registers an autoloader based on an autoload map returned by parseAutoloads * * @param array $autoloads see parseAutoloads return value * @return ClassLoader */ public function createLoader(array $autoloads) { $loader = new ClassLoader(); if (isset($autoloads['psr-0'])) { foreach ($autoloads['psr-0'] as $namespace => $path) { $loader->add($namespace, $path); } } if (isset($autoloads['psr-4'])) { foreach ($autoloads['psr-4'] as $namespace => $path) { $loader->addPsr4($namespace, $path); } } if (isset($autoloads['classmap'])) { foreach ($autoloads['classmap'] as $dir) { try { $loader->addClassMap($this->generateClassMap($dir)); } catch (\RuntimeException $e) { $this->io->writeError('<warning>' . $e->getMessage() . '</warning>'); } } } return $loader; }
protected function getStaticFile($suffix, $targetDir, $vendorPath, $basePath, &$staticPhpVersion) { $staticPhpVersion = 50600; $file = <<<HEADER <?php // autoload_static.php @generated by Composer namespace Composer\\Autoload; class ComposerStaticInit{$suffix} { HEADER; $loader = new ClassLoader(); $map = (require $targetDir . '/autoload_namespaces.php'); foreach ($map as $namespace => $path) { $loader->set($namespace, $path); } $map = (require $targetDir . '/autoload_psr4.php'); foreach ($map as $namespace => $path) { $loader->setPsr4($namespace, $path); } $classMap = (require $targetDir . '/autoload_classmap.php'); if ($classMap) { $loader->addClassMap($classMap); } $filesystem = new Filesystem(); $vendorPathCode = ' ' . $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true, true) . " . '/"; $appBaseDirCode = ' ' . $filesystem->findShortestPathCode(realpath($targetDir), $basePath, true, true) . " . '/"; $absoluteVendorPathCode = ' ' . substr(var_export(rtrim($vendorDir, '\\/') . '/', true), 0, -1); $absoluteAppBaseDirCode = ' ' . substr(var_export(rtrim($baseDir, '\\/') . '/', true), 0, -1); $initializer = ''; $prefix = "Composer\\Autoload\\ClassLoader"; $prefixLen = strlen($prefix); if (file_exists($targetDir . '/autoload_files.php')) { $maps = array('files' => require $targetDir . '/autoload_files.php'); } else { $maps = array(); } foreach ((array) $loader as $prop => $value) { if ($value && 0 === strpos($prop, $prefix)) { $maps[substr($prop, $prefixLen)] = $value; } } foreach ($maps as $prop => $value) { if (count($value) > 32767) { // Static arrays are limited to 32767 values on PHP 5.6 // See https://bugs.php.net/68057 $staticPhpVersion = 70000; } $value = var_export($value, true); $value = str_replace($absoluteVendorPathCode, $vendorPathCode, $value); $value = str_replace($absoluteAppBaseDirCode, $appBaseDirCode, $value); $value = ltrim(preg_replace('/^ */m', ' $0$0', $value)); $file .= sprintf(" public static \$%s = %s;\n\n", $prop, $value); if ('files' !== $prop) { $initializer .= " \$loader->{$prop} = ComposerStaticInit{$suffix}::\${$prop};\n"; } } return $file . <<<INITIALIZER public static function getInitializer(ClassLoader \$loader) { return \\Closure::bind(function () use (\$loader) { {$initializer} }, null, ClassLoader::class); } } INITIALIZER; }
/** * Prepare the composer fallback loader. * * @param EnumeratingClassLoader $enumLoader The enum loader to add to. * * @param string $baseDir The base dir where the composer.json resides. * * @param array $composer The contents of the composer.json. * * @return void */ private function prepareComposerFallbackLoader(EnumeratingClassLoader $enumLoader, $baseDir, $composer) { $vendorDir = $baseDir . DIRECTORY_SEPARATOR . 'vendor'; if (isset($composer['extra']['vendor-dir'])) { $vendorDir = $baseDir . DIRECTORY_SEPARATOR . $composer['extra']['vendor-dir']; } if (!is_dir($vendorDir)) { return; } $loader = new ClassLoader(); if ($map = $this->includeIfExists($vendorDir . '/composer/autoload_namespaces.php')) { foreach ($map as $namespace => $path) { $loader->set($namespace, $path); } } if ($map = $this->includeIfExists($vendorDir . '/composer/autoload_psr4.php')) { foreach ($map as $namespace => $path) { $loader->setPsr4($namespace, $path); } } if ($classMap = $this->includeIfExists($vendorDir . '/composer/autoload_classmap.php')) { $loader->addClassMap($classMap); } $enumLoader->add(array($loader, 'loadClass'), 'composer.fallback'); }
/** * {@inheritDoc} */ public function getLoader() { $loader = new ClassLoader(); $loader->addClassMap(iterator_to_array($this->getClassMap())); return array($loader, 'loadClass'); }
private function addGeneratedEntityClassLoader($class, $path) { // Prevent class not found bug on newly generated entity class $classLoader = new ClassLoader(); $classLoader->addClassMap(array($class => $path)); $classLoader->setClassMapAuthoritative(true); $classLoader->register(true); }
/** * Boot modules Class AutoLoader * * @param \CID\Core\Module\Json $module */ protected function bootClassAutoLoader(Json $module) { $autoload = $module->getModuleAutoLoad(); $loader = new ClassLoader(); // boot classmap if (array_key_exists('classmap', $autoload)) { $loader->addClassMap($autoload['classmap']); } // boot psr-0 if (array_key_exists('psr-0', $autoload)) { foreach ($autoload['psr-0'] as $prefix => $path) { $loader->add($prefix, $module->getBasePath() . DIRECTORY_SEPARATOR . $path); } } // boot-psr-4 if (array_key_exists('psr-4', $autoload)) { foreach ($autoload['psr-4'] as $prefix => $path) { $loader->addPsr4($prefix, $module->getBasePath() . DIRECTORY_SEPARATOR . $path); } } // register the autoloader $loader->register(); }
/** * Registers an autoloader based on an autoload map returned by parseAutoloads * * @param array $autoloads see parseAutoloads return value * @return ClassLoader */ public function createLoader(array $autoloads) { $loader = new ClassLoader(); if (isset($autoloads['psr-0'])) { foreach ($autoloads['psr-0'] as $namespace => $path) { $loader->add($namespace, $path); } } if (isset($autoloads['psr-4'])) { foreach ($autoloads['psr-4'] as $namespace => $path) { $loader->addPsr4($namespace, $path); } } if (isset($autoloads['classmap'])) { foreach ($autoloads['classmap'] as $dir) { $loader->addClassMap($this->generateClassMap($dir)); } } return $loader; }
/** * Add dependencies: */ protected function initAutoLoading() { $this->autoLoader->addPsr4('PhproTest\\DoctrineHydrationModule\\Tests\\', __DIR__ . '/Tests/'); $this->autoLoader->addPsr4('PhproTest\\DoctrineHydrationModule\\Fixtures\\', __DIR__ . '/Fixtures/'); $this->autoLoader->addClassMap(array('Doctrine\\ODM\\MongoDB\\Tests\\BaseTest' => PROJECT_BASE_PATH . '/vendor/doctrine/mongodb-odm/tests/Doctrine/ODM/MongoDB/Tests/BaseTest.php')); }