如题所述
composerçåºç°çæ¯è®©äººä»¬ç¼åä¸äº®ï¼webå¼åä»æ¤åæäºä¸ä»¶å¾ã好ç©ãçäºæ
ï¼å¼åä¸ä¸ªCMSå°±åå¨æ积æ¨ï¼ä»packagistä¸ååºã积æ¨ãæ建å¨èªå·±ç代ç ä¸ï¼ä¸ç¹ä¸ç¹æ建åºä¸ä¸ªå±äºèªå·±ççå½ã
ä»æ¤ä»¥åæåºæ¬å°±æå¼äºrequireåincludeå½æ°ï¼ä¸ä¸ªé¡¹ç®ä¸ï¼è¿ä¸¤ä¸ªå½æ°åªå¯è½åºç°ä¸æ¬¡ï¼é£å°±æ¯require '../vendor/autoload.php'ã
é£ä¹ï¼æ¢ç¶æå¼äºä¼ ç»çæ件å å«æ¹æ³ï¼æ们使ç¨ææç±»åºé½å°ç¨namespaceåcomposerèªå¸¦çautoloadãå¯æ¯ï¼æ们èªå·±ç¼åçå½æ°åºä¸ç±»åºï¼æä¹ç¨composerçæ¹æ³æ¥èªå¨å è½½å¢ï¼
è¿å°±è¦ä»composer.json说起ï¼æ们éè¦éè¿ä¿®æ¹è¿ä¸ªæ件æ¥è¾¾å°ç®çã
composer.jsonç¸å½äºæ¯composerçé ç½®æ件ï¼è¿ä¸ªé ç½®æ件ä¸æä¸ä¸ªautoload段ï¼æ¯å¦æçä¸ä¸ªé¡¹ç®ï¼
å ¶ä¸åå å«ä¸»è¦ç两个é项: files å psr-4ã
fileså°±æ¯éè¦composerèªå¨å¸®æ们å è½½çå½æ°åºï¼ä¸å«ç±»ï¼ï¼åªè¦å¨åé¢çæ°ç»ä¸å°å½æ°åºçæ件路å¾åå ¥å³å¯ã
psr-4顾åæä¹ï¼æ¯ä¸ä¸ªåºäºpsr-4ï¼http://www.php-fig.org/psr/psr-4/ï¼è§åçç±»åºèªå¨å 载对åºå ³ç³»ï¼åªè¦å¨å ¶åç对象ä¸ï¼ä»¥ "å½å空é´": "è·¯å¾" çæ¹å¼åå ¥èªå·±çç±»åºä¿¡æ¯å³å¯ã
ä¿®æ¹å®æåï¼åªè¦æ§è¡ä¸ä¸composer updateï¼å³å¯å®æ对åºå·¥ä½ã
ä¹åï¼æ们å¨é¡¹ç®ä¸ï¼ç¨å¦ä¸æ¹å¼å³å¯å è½½èªå®ä¹ç±»åºï¼
new \Core\View();
composerçautoloadå°ä¼èªå¨å å«"./core/view.php"ï¼å¹¶æ¾å°å ¶ä¸çCoreå½å空é´ä¸çViewç±»ã
æ们æ¥æ·±æä¸ä¸ï¼æ¢ç´¢ä¸ä¸autoloadçåçã
å¨æ们修æ¹å®composer.json并æ§è¡updateåï¼å°ä¼ä¿®æ¹./vender/composer/autoload_psr4.phpï¼æ¯å¦æçæ个项ç®ï¼å ¶ä¸å¢å äºè¿æ ·ä¸ä¸ªå¯¹åºå ³ç³»ï¼
è¿å ¶å®å°±æ¯æååå¨.jsonä¸æ·»å ç对åºå ³ç³»ï¼ä»çäºå°.josnçé ç½®æ件ï¼æ¢æäºphpçå½¢å¼ã
é£ä¹æçå°vendor/autoload.phpï¼
<?php
// autoload.php @generated by Composer
require_once __DIR__ . '/composer' . '/autoload_real.php';
return ComposerAutoloaderInitff1d77c91141523097b07ee2acc23326::getLoader();
å ¶æ§è¡äºä¸ä¸ªèªå¨çæçç±»ComposerAutoloaderInitff1d77c91141523097b07ee2acc23326ä¸çgetLoaderæ¹æ³ã
è·è¿ï¼
public static function getLoader()
{
if (null !== self::$loader) {
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInitff1d77c91141523097b07ee2acc23326', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
spl_autoload_unregister(array('ComposerAutoloaderInitff1d77c91141523097b07ee2acc23326', 'loadClassLoader'));
$map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
$loader->set($namespace, $path);
}
$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);
}
$loader->register(true);
$includeFiles = require __DIR__ . '/autoload_files.php';
foreach ($includeFiles as $file) {
composerRequireff1d77c91141523097b07ee2acc23326($file);
}
return $loader;
}
å¯ä»¥ææ¾çå°ï¼ä»å°autoload_namespaces.phpãautoload_psr4.phpãautoload_classmap.phpãautoload_files.phpçå 个é ç½®æ件å å«äºè¿æ¥ï¼å¹¶è¿è¡äºç¸å ³å¤çï¼setPsr4ï¼ï¼æå注åï¼registerï¼ã
é£ä¹æ们è·è¿registeræ¹æ³ï¼
public function register($prepend = false)
{
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
}
è¿å½æ°å°±ä¸è¡ï¼ä½ç®åæäºï¼ç´æ¥è°ç¨phpèªå¸¦çspl_autoload_registerå½æ°ï¼æ³¨åå¤ç__autoloadçæ¹æ³ï¼ä¹å°±æ¯loadClassæ¹æ³ãåè·è¿loadClassæ¹æ³ï¼
public function loadClass($class)
{
if ($file = $this->findFile($class)) {
includeFile($file);
return true;
}
}
ä»å½æ°ååå°±å¯ä»¥å¤§æ¦ç¥éæµç¨ï¼å¦æåå¨$class对åºçè¿ä¸ª$fileï¼åincludeè¿æ¥ã
é£ä¹è¿findFileæ¹æ³éççå§ï¼
public function findFile($class)
{
// work around for PHP 5.3.0 - 5.3.2 https://bugs.php.net/50731
if ('\\' == $class[0]) {
$class = substr($class, 1);
}
// class map lookup
if (isset($this->classMap[$class])) {
return $this->classMap[$class];
}
if ($this->classMapAuthoritative) {
return false;
}
$file = $this->findFileWithExtension($class, '.php');
// Search for Hack files if we are running on HHVM
if ($file === null && defined('HHVM_VERSION')) {
$file = $this->findFileWithExtension($class, '.hh');
}
if ($file === null) {
// Remember that this class does not exist.
return $this->classMap[$class] = false;
}
return $file;
}
éè¿ç±»åæ¾æ件ï¼æç»éå®å¨findFileWithExtensionæ¹æ³ä¸ã
ä¸è¿åç°äºä¸ä¸ªå°å®èï¼å¨php5.3.0~5.3.2çæ¬ä¸ï¼ç±»åç第ä¸ä¸ªå符æ¯\çå°bugï¼ä¹è®¸ä»¥åææ¼æ´ä¼ç¨ä¸ã
è¿æ¯è·è¿findFileWithExtensionæ¹æ³ï¼
private function findFileWithExtension($class, $ext)
{
// PSR-4 lookup
$logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;
$first = $class[0];
if (isset($this->prefixLengthsPsr4[$first])) {
foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) {
if (0 === strpos($class, $prefix)) {
foreach ($this->prefixDirsPsr4[$prefix] as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
return $file;
}
}
}
}
}
// PSR-4 fallback dirs
foreach ($this->fallbackDirsPsr4 as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
return $file;
}
}
// PSR-0 lookup
if (false !== $pos = strrpos($class, '\\')) {
// namespaced class name
$logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
. strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
} else {
// PEAR-like class name
$logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext;
}
if (isset($this->prefixesPsr0[$first])) {
foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
if (0 === strpos($class, $prefix)) {
foreach ($dirs as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
return $file;
}
}
}
}
}
// PSR-0 fallback dirs
foreach ($this->fallbackDirsPsr0 as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
return $file;
}
}
// PSR-0 include paths.
if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {
return $file;
}
}
æç»å®ç°å°å½å空é´\ç±»è¿æ ·çç±»åï¼ç»è½¬æ¢æç®å½å/ç±»å.phpè¿æ ·çè·¯å¾ï¼å¹¶è¿åå®æ´è·¯å¾ã
æåç°composerçautoloadä¸phpèªå¸¦çspl_autoloadï¼å¨å å«æ件æ¶æä¸ç¹å°åºå«ãé£å°±æ¯ï¼spl_autoloadä¼æ¥æ¾.incç±»åçæ件åï¼ä½composerä¸ä¼ã
å¦å¤ä¹å¯ä»¥åç°ï¼è½ç¶é ç½®æ件çååæ¯autoload_psr4.phpï¼ä½å®é ä¸psr0æ ¼å¼çèªå¨å è½½ä¹æ¯æ¯æçãäºè æ大çä¸åå°±æ¯psr0ä¸ç¨"_"æ¥ä»£æ¿ç®å½é´ç"\"ã
ä»æ¤ä»¥åæåºæ¬å°±æå¼äºrequireåincludeå½æ°ï¼ä¸ä¸ªé¡¹ç®ä¸ï¼è¿ä¸¤ä¸ªå½æ°åªå¯è½åºç°ä¸æ¬¡ï¼é£å°±æ¯require '../vendor/autoload.php'ã
é£ä¹ï¼æ¢ç¶æå¼äºä¼ ç»çæ件å å«æ¹æ³ï¼æ们使ç¨ææç±»åºé½å°ç¨namespaceåcomposerèªå¸¦çautoloadãå¯æ¯ï¼æ们èªå·±ç¼åçå½æ°åºä¸ç±»åºï¼æä¹ç¨composerçæ¹æ³æ¥èªå¨å è½½å¢ï¼
è¿å°±è¦ä»composer.json说起ï¼æ们éè¦éè¿ä¿®æ¹è¿ä¸ªæ件æ¥è¾¾å°ç®çã
composer.jsonç¸å½äºæ¯composerçé ç½®æ件ï¼è¿ä¸ªé ç½®æ件ä¸æä¸ä¸ªautoload段ï¼æ¯å¦æçä¸ä¸ªé¡¹ç®ï¼
å ¶ä¸åå å«ä¸»è¦ç两个é项: files å psr-4ã
fileså°±æ¯éè¦composerèªå¨å¸®æ们å è½½çå½æ°åºï¼ä¸å«ç±»ï¼ï¼åªè¦å¨åé¢çæ°ç»ä¸å°å½æ°åºçæ件路å¾åå ¥å³å¯ã
psr-4顾åæä¹ï¼æ¯ä¸ä¸ªåºäºpsr-4ï¼http://www.php-fig.org/psr/psr-4/ï¼è§åçç±»åºèªå¨å 载对åºå ³ç³»ï¼åªè¦å¨å ¶åç对象ä¸ï¼ä»¥ "å½å空é´": "è·¯å¾" çæ¹å¼åå ¥èªå·±çç±»åºä¿¡æ¯å³å¯ã
ä¿®æ¹å®æåï¼åªè¦æ§è¡ä¸ä¸composer updateï¼å³å¯å®æ对åºå·¥ä½ã
ä¹åï¼æ们å¨é¡¹ç®ä¸ï¼ç¨å¦ä¸æ¹å¼å³å¯å è½½èªå®ä¹ç±»åºï¼
new \Core\View();
composerçautoloadå°ä¼èªå¨å å«"./core/view.php"ï¼å¹¶æ¾å°å ¶ä¸çCoreå½å空é´ä¸çViewç±»ã
æ们æ¥æ·±æä¸ä¸ï¼æ¢ç´¢ä¸ä¸autoloadçåçã
å¨æ们修æ¹å®composer.json并æ§è¡updateåï¼å°ä¼ä¿®æ¹./vender/composer/autoload_psr4.phpï¼æ¯å¦æçæ个项ç®ï¼å ¶ä¸å¢å äºè¿æ ·ä¸ä¸ªå¯¹åºå ³ç³»ï¼
è¿å ¶å®å°±æ¯æååå¨.jsonä¸æ·»å ç对åºå ³ç³»ï¼ä»çäºå°.josnçé ç½®æ件ï¼æ¢æäºphpçå½¢å¼ã
é£ä¹æçå°vendor/autoload.phpï¼
<?php
// autoload.php @generated by Composer
require_once __DIR__ . '/composer' . '/autoload_real.php';
return ComposerAutoloaderInitff1d77c91141523097b07ee2acc23326::getLoader();
å ¶æ§è¡äºä¸ä¸ªèªå¨çæçç±»ComposerAutoloaderInitff1d77c91141523097b07ee2acc23326ä¸çgetLoaderæ¹æ³ã
è·è¿ï¼
public static function getLoader()
{
if (null !== self::$loader) {
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInitff1d77c91141523097b07ee2acc23326', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
spl_autoload_unregister(array('ComposerAutoloaderInitff1d77c91141523097b07ee2acc23326', 'loadClassLoader'));
$map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
$loader->set($namespace, $path);
}
$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);
}
$loader->register(true);
$includeFiles = require __DIR__ . '/autoload_files.php';
foreach ($includeFiles as $file) {
composerRequireff1d77c91141523097b07ee2acc23326($file);
}
return $loader;
}
å¯ä»¥ææ¾çå°ï¼ä»å°autoload_namespaces.phpãautoload_psr4.phpãautoload_classmap.phpãautoload_files.phpçå 个é ç½®æ件å å«äºè¿æ¥ï¼å¹¶è¿è¡äºç¸å ³å¤çï¼setPsr4ï¼ï¼æå注åï¼registerï¼ã
é£ä¹æ们è·è¿registeræ¹æ³ï¼
public function register($prepend = false)
{
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
}
è¿å½æ°å°±ä¸è¡ï¼ä½ç®åæäºï¼ç´æ¥è°ç¨phpèªå¸¦çspl_autoload_registerå½æ°ï¼æ³¨åå¤ç__autoloadçæ¹æ³ï¼ä¹å°±æ¯loadClassæ¹æ³ãåè·è¿loadClassæ¹æ³ï¼
public function loadClass($class)
{
if ($file = $this->findFile($class)) {
includeFile($file);
return true;
}
}
ä»å½æ°ååå°±å¯ä»¥å¤§æ¦ç¥éæµç¨ï¼å¦æåå¨$class对åºçè¿ä¸ª$fileï¼åincludeè¿æ¥ã
é£ä¹è¿findFileæ¹æ³éççå§ï¼
public function findFile($class)
{
// work around for PHP 5.3.0 - 5.3.2 https://bugs.php.net/50731
if ('\\' == $class[0]) {
$class = substr($class, 1);
}
// class map lookup
if (isset($this->classMap[$class])) {
return $this->classMap[$class];
}
if ($this->classMapAuthoritative) {
return false;
}
$file = $this->findFileWithExtension($class, '.php');
// Search for Hack files if we are running on HHVM
if ($file === null && defined('HHVM_VERSION')) {
$file = $this->findFileWithExtension($class, '.hh');
}
if ($file === null) {
// Remember that this class does not exist.
return $this->classMap[$class] = false;
}
return $file;
}
éè¿ç±»åæ¾æ件ï¼æç»éå®å¨findFileWithExtensionæ¹æ³ä¸ã
ä¸è¿åç°äºä¸ä¸ªå°å®èï¼å¨php5.3.0~5.3.2çæ¬ä¸ï¼ç±»åç第ä¸ä¸ªå符æ¯\çå°bugï¼ä¹è®¸ä»¥åææ¼æ´ä¼ç¨ä¸ã
è¿æ¯è·è¿findFileWithExtensionæ¹æ³ï¼
private function findFileWithExtension($class, $ext)
{
// PSR-4 lookup
$logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;
$first = $class[0];
if (isset($this->prefixLengthsPsr4[$first])) {
foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) {
if (0 === strpos($class, $prefix)) {
foreach ($this->prefixDirsPsr4[$prefix] as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
return $file;
}
}
}
}
}
// PSR-4 fallback dirs
foreach ($this->fallbackDirsPsr4 as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
return $file;
}
}
// PSR-0 lookup
if (false !== $pos = strrpos($class, '\\')) {
// namespaced class name
$logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
. strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
} else {
// PEAR-like class name
$logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext;
}
if (isset($this->prefixesPsr0[$first])) {
foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
if (0 === strpos($class, $prefix)) {
foreach ($dirs as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
return $file;
}
}
}
}
}
// PSR-0 fallback dirs
foreach ($this->fallbackDirsPsr0 as $dir) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
return $file;
}
}
// PSR-0 include paths.
if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {
return $file;
}
}
æç»å®ç°å°å½å空é´\ç±»è¿æ ·çç±»åï¼ç»è½¬æ¢æç®å½å/ç±»å.phpè¿æ ·çè·¯å¾ï¼å¹¶è¿åå®æ´è·¯å¾ã
æåç°composerçautoloadä¸phpèªå¸¦çspl_autoloadï¼å¨å å«æ件æ¶æä¸ç¹å°åºå«ãé£å°±æ¯ï¼spl_autoloadä¼æ¥æ¾.incç±»åçæ件åï¼ä½composerä¸ä¼ã
å¦å¤ä¹å¯ä»¥åç°ï¼è½ç¶é ç½®æ件çååæ¯autoload_psr4.phpï¼ä½å®é ä¸psr0æ ¼å¼çèªå¨å è½½ä¹æ¯æ¯æçãäºè æ大çä¸åå°±æ¯psr0ä¸ç¨"_"æ¥ä»£æ¿ç®å½é´ç"\"ã
温馨提示:答案为网友推荐,仅供参考