Componentoser自動ロードメカニズムソースプロファイル
13081 ワード
1、autoload.phpはComposerの自動ロードを使用するには、まずこのファイルを導入する必要があります.
2、autoload_real.php
3、ClassLoader.php(一部コアコード)
①PSR-0ルール
②PSR-4ルール
③Class-map方式
上記で登録したloadClass関数を呼び出すのはいつですか?例えば
これでComposer自動ロードプロセスが完了しました.具体的なコードは皆さんが自分で研究する必要があります.驚嘆せざるを得なくて、Composerがあって本当に便利です!
<?php // autoload.php @generated by Composer // autoload_real.php require_once __DIR__ . '/composer' . '/autoload_real.php'; // composer Composer , autoload_real.php getLoader() return ComposerAutoloaderInit85b4dbf6b714d62ec745fcf3dd1a5041::getLoader();
2、autoload_real.php
<?php // autoload_real.php @generated by Composer class ComposerAutoloaderInit85b4dbf6b714d62ec745fcf3dd1a5041 { private static $loader; // ClassLoader public static function loadClassLoader($class) { // getLoader __autoload , , , // Composer\Autoload\ClassLoader if ('Composer\Autoload\ClassLoader' === $class) { require __DIR__ . '/ClassLoader.php'; } } public static function getLoader() { if (null !== self::$loader) { return self::$loader; } // loadClassLoader __autoload spl_autoload_register(array('ComposerAutoloaderInit85b4dbf6b714d62ec745fcf3dd1a5041', 'loadClassLoader'), true, true); // , // loadClassLoader ClassLoader self::$loader = $loader = new \Composer\Autoload\ClassLoader(); spl_autoload_unregister(array('ComposerAutoloaderInit85b4dbf6b714d62ec745fcf3dd1a5041', 'loadClassLoader')); // PSR-0 $map = require __DIR__ . '/autoload_namespaces.php'; foreach ($map as $namespace => $path) { $loader->set($namespace, $path); } // PSR-4 $map = require __DIR__ . '/autoload_psr4.php'; foreach ($map as $namespace => $path) { $loader->setPsr4($namespace, $path); } $classMap = require __DIR__ . '/autoload_classmap.php'; if ($classMap) { $loader->addClassMap($classMap); } // __autoload , // Class-Map $loader->register(true); // Files $includeFiles = require __DIR__ . '/autoload_files.php'; foreach ($includeFiles as $fileIdentifier => $file) { composerRequire85b4dbf6b714d62ec745fcf3dd1a5041($fileIdentifier, $file); } return $loader; } } function composerRequire85b4dbf6b714d62ec745fcf3dd1a5041($fileIdentifier, $file) { if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { // require $file; $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; } }
3、ClassLoader.php(一部コアコード)
①PSR-0ルール
/** * Registers a set of PSR-0 directories for a given prefix, * replacing any others previously set for this prefix. * * @param string $prefix The prefix * @param array|string $paths The PSR-0 base directories */
// $prefix、$paths autoload_namespaces.php key value
public function set($prefix, $paths) {
if (!$prefix) {
$this->fallbackDirsPsr0 = (array) $paths;
} else {
// $prefix[0]
$this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths;
}
}
②PSR-4ルール
/** * Registers a set of PSR-4 directories for a given namespace, * replacing any others previously set for this namespace. * * @param string $prefix The prefix/namespace, with trailing '\\' * @param array|string $paths The PSR-4 base directories * * @throws \InvalidArgumentException */
public function setPsr4($prefix, $paths) {
if (!$prefix) {
$this->fallbackDirsPsr4 = (array) $paths;
} else {
$length = strlen($prefix);
// PSR-4 \ , ,
if ('\\' !== $prefix[$length - 1]) {
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
}
$this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
$this->prefixDirsPsr4[$prefix] = (array) $paths;
}
}
③Class-map方式
/** * @param array $classMap Class to filename map */
public function addClassMap(array $classMap)
{
if ($this->classMap) {
$this->classMap = array_merge($this->classMap, $classMap);
} else {
$this->classMap = $classMap;
}
}
/** * Registers this instance as an autoloader. * * @param bool $prepend Whether to prepend the autoloader or not */
public function register($prepend = false)
{ // $prepend true ,spl_autoload_register() , 。 ( php )
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
}
/** * Loads the given class or interface. * * @param string $class The name of the class * @return bool|null True if loaded, null otherwise */
public function loadClass($class) {
if ($file = $this->findFile($class)) {
includeFile($file);
return true;
}
}
上記で登録したloadClass関数を呼び出すのはいつですか?例えば
<?php require './vendor/autoload.php'; // TestController , spl_autoload_register loadClass, loadClass findFile , require $test = new TestController; echo $test->show();
これでComposer自動ロードプロセスが完了しました.具体的なコードは皆さんが自分で研究する必要があります.驚嘆せざるを得なくて、Composerがあって本当に便利です!