forked from TYPO3/class-alias-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Register alias loader before autoload dump
In order to have the class alias loader present in post autoload execution, we now register it before Composer generates autoloading. As a side effect, we can remove the autoload.php rewrite hack.
- Loading branch information
Showing
7 changed files
with
350 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
$composerAutoLoader = require dirname(__DIR__) . '/autoload.php'; | ||
$classAliasMap = require dirname(__DIR__) . '/composer/autoload_classaliasmap.php'; | ||
$classAliasLoader = new TYPO3\ClassAliasLoader\ClassAliasLoader($composerAutoLoader); | ||
$classAliasLoader->setAliasMap($classAliasMap); | ||
$classAliasLoader->setCaseSensitiveClassLoading('{$sensitive-loading}'); | ||
$classAliasLoader->register('{$prepend}'); | ||
TYPO3\ClassAliasLoader\ClassAliasMap::setClassAliasLoader($classAliasLoader); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
namespace TYPO3\ClassAliasLoader; | ||
|
||
/* | ||
* This file is part of the class alias loader package. | ||
* | ||
* (c) Helmut Hummel <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Composer\Composer; | ||
use Composer\IO\IOInterface; | ||
use Composer\Util\Filesystem; | ||
use TYPO3\CMS\Composer\Plugin\Core\IncludeFile\TokenInterface; | ||
|
||
class IncludeFile | ||
{ | ||
const INCLUDE_FILE = '/typo3/alias-loader-include.php'; | ||
const INCLUDE_FILE_TEMPLATE = '/res/php/alias-loader-include.tmpl.php'; | ||
|
||
/** | ||
* @var TokenInterface[] | ||
*/ | ||
private $tokens; | ||
|
||
/** | ||
* @var Filesystem | ||
*/ | ||
private $filesystem; | ||
/** | ||
* @var IOInterface | ||
*/ | ||
private $io; | ||
/** | ||
* @var Composer | ||
*/ | ||
private $composer; | ||
|
||
/** | ||
* IncludeFile constructor. | ||
* | ||
* @param IOInterface $io | ||
* @param Composer $composer | ||
* @param TokenInterface[] $tokens | ||
* @param Filesystem $filesystem | ||
*/ | ||
public function __construct(IOInterface $io, Composer $composer, array $tokens, Filesystem $filesystem = null) | ||
{ | ||
$this->io = $io; | ||
$this->composer = $composer; | ||
$this->tokens = $tokens; | ||
$this->filesystem = $filesystem ?: new Filesystem(); | ||
} | ||
|
||
public function register() | ||
{ | ||
$this->io->writeError('<info>Register typo3/class-alias-loader file in root package autoload definition</info>', true, IOInterface::VERBOSE); | ||
|
||
// Generate and write the file | ||
$includeFile = $this->composer->getConfig()->get('vendor-dir') . self::INCLUDE_FILE; | ||
file_put_contents($includeFile, $this->getIncludeFileContent(dirname($includeFile))); | ||
|
||
// Register the file in the root package | ||
$rootPackage = $this->composer->getPackage(); | ||
$autoloadDefinition = $rootPackage->getAutoload(); | ||
$autoloadDefinition['files'][] = $includeFile; | ||
$rootPackage->setAutoload($autoloadDefinition); | ||
} | ||
|
||
/** | ||
* Constructs the include file content | ||
* | ||
* @param string $includeFilePath | ||
* @throws \RuntimeException | ||
* @throws \InvalidArgumentException | ||
* @return string | ||
*/ | ||
protected function getIncludeFileContent(string $includeFilePath) | ||
{ | ||
$includeFileTemplate = $this->filesystem->normalizePath(dirname(__DIR__) . self::INCLUDE_FILE_TEMPLATE); | ||
$includeFileContent = file_get_contents($includeFileTemplate); | ||
foreach ($this->tokens as $token) { | ||
$includeFileContent = self::replaceToken($token->getName(), $token->getContent($includeFilePath), $includeFileContent); | ||
} | ||
|
||
return $includeFileContent; | ||
} | ||
|
||
/** | ||
* Replaces a token in the subject (PHP code) | ||
* | ||
* @param string $name | ||
* @param string $content | ||
* @param string $subject | ||
* @return string | ||
*/ | ||
private static function replaceToken($name, $content, $subject) | ||
{ | ||
return str_replace('\'{$' . $name . '}\'', $content, $subject); | ||
} | ||
} |
Oops, something went wrong.