/** * Try to load a class. * * @param string $className The class name. * * @param string $file The file where the class should be contained. * * @return bool */ private function tryLoadClass($className, $file) { if ($this->isLoaded($className)) { return true; } $this->logger->debug('Trying to load {class} (should be located in file {file}).', array('class' => $className, 'file' => $file)); try { $this->loader->loadClass($className); if (!$this->loader->isClassFromFile($className, $file)) { $this->logger->warning('{class} was loaded from {realFile} (should be located in file {file}).', array('class' => $className, 'file' => $file, 'realFile' => $this->loader->getFileDeclaringClass($className))); } return true; } catch (ClassNotFoundException $exception) { $this->logger->error('The autoloader could not load {class} (should be located in file {file}).', array('class' => $className, 'file' => $file)); } catch (\ErrorException $exception) { $this->logger->error('Loading class {class} failed with reason: {error}.', array('class' => $className, 'error' => $exception->getMessage())); } return false; }
/** * Compile a hack function. * * @param callable $loader The loader function. * * @param string $hackName The name of the hack. * * @return \Closure */ private function compileHack($loader, $hackName) { $logger = $this->logger; return function ($class) use($loader, $hackName, $logger) { if (call_user_func($loader, $class)) { $logger->debug('Custom class loader hack {hackName} loaded {class}.', array('hackName' => $hackName, 'class' => $class)); return $hackName; } if (EnumeratingClassLoader::isLoaded($class)) { $logger->warning('Hack {hackName} appears to have loaded {class} but did return null. It SHOULD return true.', array('hackName' => $hackName, 'class' => $class)); return $hackName; } return null; }; }
* @filesource */ // WARNING!!!! This file is just a reference and should not be used literally. use PhpCodeQuality\AutoloadValidation\ClassLoader\EnumeratingClassLoader; use PhpCodeQuality\AutoloadValidation\ClassMapGenerator; use PhpCodeQuality\AutoloadValidation\Exception\ParentClassNotFoundException; // Support for Contao 4.1. if (is_dir($path = getcwd() . '/vendor/contao/core-bundle/src/Resources/contao')) { $loader = new \Composer\Autoload\ClassLoader(); foreach (array('classes', 'controllers', 'drivers', 'elements', 'forms', 'library', 'models', 'modules', 'pages', 'widgets') as $subDir) { $classMap = ClassMapGenerator::createMap($path . '/' . $subDir); $loader->addClassMap($classMap); } $loader->register(); } // This is the hack to mimic the Contao auto loader. spl_autoload_register(function ($class) { if (substr($class, 0, 7) === 'Contao\\') { return null; } try { spl_autoload_call('Contao\\' . $class); } catch (ParentClassNotFoundException $exception) { return null; } if (EnumeratingClassLoader::isLoaded('Contao\\' . $class) && !EnumeratingClassLoader::isLoaded($class)) { class_alias('Contao\\' . $class, $class); return true; } return null; });
/** * 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'); }