In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to make Composer's autoload support custom file suffixes, the article is very detailed, has a certain reference value, interested friends must read it!
The Composer tool of PHP standardizes the loading format of various resource libraries in the system. With the help of the automatic loading mechanism of PHP, the scene of referencing class library files in the process of application development can be greatly simplified. But so far, it is not a problem, that is, the file suffix only supports .php, while the old assets developed based on some frameworks, the class file suffix name is .class.php, want to use Composer's automatic loading specification, it is not very pure, generally want to mix the two, or modify the loading rules under other frameworks.
Is there a more convenient solution?
First of all, as long as you can have such a question, you win. As for the answer, it is likely to be found.
The code that implements the auto-loading mechanism in Composer is so concise that you can understand it at a glance.
When you see the findFileWithExtension method in the ClassLoader.php file, a $ext appears in the parameter, and you see hope. As long as you can override this $ext parameter at the right time.
The original code is as follows:
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 {
Modify it a little bit:
In the autload_psr4.php configuration file, the corresponding format changes:
Return array ('Qiniu\\' = > array ($vendorDir. '/ qiniu/php-sdk/src/Qiniu'), / / change the string format to the two-dimensional array format' Liniu\\'= > array ([$vendorDir. '/ Liniu/php-sdk/src/Liniu',' .class.php']),)
Post the code:
Private function findFileWithExtension ($class, $ext) {/ / PSR-4 lookup $logicalPathPsr4 = strtr ($class,'\', DIRECTORY_SEPARATOR); $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) {$_ ext = $ext; $_ dir = $dir If (is_array ($dir) & & count ($dir) = = 2) {$_ ext = $dir [1]; $_ dir = $dir [0];} if (file_exists ($file = $_ dir. DIRECTORY_SEPARATOR. Substr ($logicalPathPsr4. $_ ext, $length)) {return $file;} / / PSR-4 fallback dirs foreach ($this- > fallbackDirsPsr4 as $dir) {if (file_exists ($file = $dir. DIRECTORY_SEPARATOR. $logicalPathPsr4. $ext) {return $file;}} / / PSR-0 lookup if (false! = = $pos = strrpos ($class,'\')) {/ / namespaced class name $logicalPathPsr0 = substr ($logicalPathPsr4. Ext, 0, $pos + 1). Strtr (substr ($logicalPathPsr4. $ext, $pos + 1),'_', DIRECTORY_SEPARATOR);} else {
Coding, there is a pure fun.
The above is all the contents of the article "how to make Composer's autoload support custom file suffixes". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.