這里寫問(wèn)題描述
/workerman/webman-framework/src/support/helpers.php里的方法設(shè)置了檢測(cè)是否存在的校驗(yàn),但是如何才可以覆蓋helpers.php的里方法呢,因?yàn)樗扔? "autoload": {
"files": [
"./support/helpersDiy.php"
],加載的
要如何才可以覆蓋特定的方法呢,比如覆蓋session()方法
這里粘代碼或配置
namespace Composer\Autoload;
class ComposerStaticInit1adbc90cfda11ea12e116a44da43c60e
{
public static $files = array (
'9b552a3cc426e3287cc811caefa3cf53' => __DIR__ . '/..' . '/topthink/think-helper/src/helper.php',
'253c157292f75eb38082b5acb06f3f01' => __DIR__ . '/..' . '/nikic/fast-route/src/functions.php',
'7448f3465e10b5f033e4babb31eb0b06' => __DIR__ . '/..' . '/topthink/think-orm/src/helper.php',
'35fab96057f1bf5e7aba31a8a6d5fdde' => __DIR__ . '/..' . '/topthink/think-orm/stubs/load_stubs.php',
'f88f8987adfe3f7cf9978fa9a9d148bc' => __DIR__ . '/..' . '/workerman/psr7/src/functions_include.php',
'd2136ff22b54ac75cd96a40e0022218e' => __DIR__ . '/..' . '/workerman/webman-framework/src/support/helpers.php',
'240e12a27071fb3319a56fb01fd571f8' => __DIR__ . '/../..' . '/support/helpersDiy.php',
);
當(dāng)前的順序是 public static $files = array (
'9b552a3cc426e3287cc811caefa3cf53' => __DIR__ . '/..' . '/topthink/think-helper/src/helper.php',
'253c157292f75eb38082b5acb06f3f01' => __DIR__ . '/..' . '/nikic/fast-route/src/functions.php',
'7448f3465e10b5f033e4babb31eb0b06' => __DIR__ . '/..' . '/topthink/think-orm/src/helper.php',
'35fab96057f1bf5e7aba31a8a6d5fdde' => __DIR__ . '/..' . '/topthink/think-orm/stubs/load_stubs.php',
'f88f8987adfe3f7cf9978fa9a9d148bc' => __DIR__ . '/..' . '/workerman/psr7/src/functions_include.php',
'd2136ff22b54ac75cd96a40e0022218e' => __DIR__ . '/..' . '/workerman/webman-framework/src/support/helpers.php',
'240e12a27071fb3319a56fb01fd571f8' => __DIR__ . '/../..' . '/support/helpersDiy.php',
);想把最后一項(xiàng)提到倒數(shù)第二項(xiàng)的前面如何做
修改 src/AutoloadGenerator.php
、composer.json
,下邊的例子主要是擴(kuò)展了 extra.preload-files
的配置,讓這個(gè) composer
插件變得更加靈活,可以將需要自動(dòng)加載的文件插入到指定位置。
<?php
namespace CodeZero\ComposerPreloadFiles;
use Composer\Autoload\AutoloadGenerator as ComposerAutoloadGenerator;
use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Package\CompletePackage;
use Composer\Pcre\Preg;
use Composer\Util\Filesystem;
use Composer\Util\Platform;
class AutoloadGenerator extends ComposerAutoloadGenerator
{
/**
* Add preload files to the autoload files.
*/
public function addPreloadFilesToAutoloadFiles(Composer $composer, IOInterface $io, Filesystem $filesystem)
{
$io->writeError('<info>Adding preload files to the autoload files.</info>');
// Pathfinding...
$basePath = $filesystem->normalizePath(realpath(realpath(Platform::getCwd())));
$vendorDir = $composer->getConfig()->get('vendor-dir');
$vendorPath = $filesystem->normalizePath(realpath(realpath($vendorDir)));
$targetDir = $vendorPath.'/composer';
$rootPackage = $composer->getPackage();
$rootPackageConfig = $rootPackage->getExtra();
$preloadFilesConfig = $rootPackageConfig['preload-files'] ?? [];
$this->prependPreloadFilesToAutoloadFilesFile($filesystem, $preloadFilesConfig, $targetDir, $basePath, $vendorPath, $composer);
$this->regenerateAutoloadStaticFile($filesystem, $targetDir, $basePath, $vendorPath);
}
/**
* Prepend preload files to the 'autoload_files.php' file.
*/
protected function prependPreloadFilesToAutoloadFilesFile(Filesystem $filesystem, $preloadFilesConfig, $targetDir, $basePath, $vendorPath, Composer $composer)
{
$vendorPathCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true);
$appBaseDirCode = $filesystem->findShortestPathCode($vendorPath, $basePath, true);
$appBaseDirCode = str_replace('__DIR__', '$vendorDir', $appBaseDirCode);
$autoloadFilesFilePath = $targetDir.'/autoload_files.php';
$originalFiles = $this->getOriginalAutoloadFiles($autoloadFilesFilePath);
if (is_array($preloadFilesConfig) && (isset($preloadFilesConfig['before']) || isset($preloadFilesConfig['after']))) {
$allFiles = $originalFiles;
if (!empty($preloadFilesConfig['before'])) {
$beforeList = $this->normalizePreloadNodeList($preloadFilesConfig['before']);
foreach ($beforeList as $item) {
$nodeFiles = $this->parsePreloadFilesNode($item, $basePath, $vendorPath, $filesystem, $composer);
$allFiles = $this->insertArrayBeforeValue(
$allFiles,
$nodeFiles,
$item['node'] ?? ''
);
}
}
if (!empty($preloadFilesConfig['after'])) {
$afterList = $this->normalizePreloadNodeList($preloadFilesConfig['after']);
foreach (array_reverse($afterList) as $item) {
$nodeFiles = $this->parsePreloadFilesNode($item, $basePath, $vendorPath, $filesystem, $composer);
$allFiles = $this->insertArrayAfterValue(
$allFiles,
$nodeFiles,
$item['node'] ?? ''
);
}
}
} else {
$preloadFiles = $this->parsePreloadFilesList($preloadFilesConfig, $basePath, $vendorPath, $filesystem, $composer);
$allFiles = array_merge($preloadFiles, $originalFiles);
}
$filesystem->filePutContentsIfModified(
$autoloadFilesFilePath,
$this->getIncludeFilesFile($allFiles, $filesystem, $basePath, $vendorPath, $vendorPathCode, $appBaseDirCode)
);
}
/**
* Regenerate the 'autoload_static.php' file.
*/
protected function regenerateAutoloadStaticFile(Filesystem $filesystem, $targetDir, $basePath, $vendorPath)
{
$autoloadContent = file_get_contents($vendorPath.'/autoload.php');
$suffix = null;
if (Preg::isMatch('{ComposerAutoloaderInit([^:\s]+)::}', $autoloadContent, $match)) {
$suffix = $match[1];
}
$filesystem->filePutContentsIfModified(
$targetDir.'/autoload_static.php',
$this->getStaticFile($suffix, $targetDir, $vendorPath, $basePath)
);
}
/**
* Get the original files to autoload.
*/
protected function getOriginalAutoloadFiles($autoloadFilesFilePath)
{
if (file_exists($autoloadFilesFilePath)) {
return include $autoloadFilesFilePath;
}
return [];
}
/**
* Normalize before/after node list to always be array of associative arrays.
*/
protected function normalizePreloadNodeList($nodeList)
{
if (isset($nodeList['node']) && isset($nodeList['files'])) {
return [$nodeList];
}
return $nodeList;
}
protected function parsePreloadFilesNode($item, $basePath, $vendorPath, Filesystem $filesystem, Composer $composer)
{
$files = [];
$fileList = $item['files'] ?? [];
foreach ($fileList as $file) {
$fullPath = $this->resolveFilePath($file, $basePath, $vendorPath, $filesystem, $composer);
$identifier = $this->getFileIdentifierForAutoload($fullPath);
$files[$identifier] = $fullPath;
}
return $files;
}
protected function parsePreloadFilesList($fileList, $basePath, $vendorPath, Filesystem $filesystem, Composer $composer)
{
$files = [];
foreach ((array)$fileList as $file) {
$fullPath = $this->resolveFilePath($file, $basePath, $vendorPath, $filesystem, $composer);
$identifier = $this->getFileIdentifierForAutoload($fullPath);
$files[$identifier] = $fullPath;
}
return $files;
}
protected function resolveFilePath($file, $basePath, $vendorPath, Filesystem $filesystem, Composer $composer)
{
if (strpos($file, 'vendor/') === 0) {
return $filesystem->normalizePath($vendorPath . '/' . substr($file, 7));
}
if (strpos($file, ':') !== false) {
list($packageName, $packageFile) = explode(':', $file, 2);
$packagePath = '';
foreach ($composer->getRepositoryManager()->getLocalRepository()->getCanonicalPackages() as $package) {
if ($package->getName() === $packageName) {
$installPath = $composer->getInstallationManager()->getInstallPath($package);
$packagePath = $filesystem->normalizePath($installPath . '/' . ltrim($packageFile, '/'));
break;
}
}
if ($packagePath) {
return $packagePath;
}
}
return $filesystem->normalizePath($basePath . '/' . $file);
}
protected function getFileIdentifierForAutoload($filePath)
{
return md5($filePath);
}
protected function insertArrayBeforeValue(array $array, array $insertArray, $targetValuePart)
{
$result = [];
$inserted = false;
foreach ($array as $key => $value) {
if (!$inserted && $targetValuePart && strpos($value, $targetValuePart) !== false) {
foreach ($insertArray as $ik => $iv) {
$result[$ik] = $iv;
}
$inserted = true;
}
$result[$key] = $value;
}
if (!$inserted) {
foreach ($insertArray as $ik => $iv) {
$result[$ik] = $iv;
}
}
return $result;
}
protected function insertArrayAfterValue(array $array, array $insertArray, $targetValuePart)
{
$result = [];
$inserted = false;
foreach ($array as $key => $value) {
$result[$key] = $value;
if (!$inserted && $targetValuePart && strpos($value, $targetValuePart) !== false) {
foreach ($insertArray as $ik => $iv) {
$result[$ik] = $iv;
}
$inserted = true;
}
}
if (!$inserted) {
foreach ($insertArray as $ik => $iv) {
$result[$ik] = $iv;
}
}
return $result;
}
}
發(fā)布新的 composer package
,安裝
修改 webman
項(xiàng)目的 composer.json
,新增 extra.preload-files
節(jié)點(diǎn)
"extra": {
"preload-files": {
"before": [
{
"node": "workerman/webman-framework/src/support/helpers.php",
"files": [
"app/helpers.php"
]
}
],
"after": [
{
"node": "symfony/polyfill-ctype/bootstrap.php",
"files": [
"app/funcs.php"
]
}
]
}
},
效果如下:
<?php
// autoload_files.php @generated by Composer
$vendorDir = dirname(__DIR__);
$baseDir = dirname($vendorDir);
return array(
'6e3fae29631ef280660b3cdad06f25a8' => $vendorDir . '/symfony/deprecation-contracts/function.php',
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => $vendorDir . '/symfony/polyfill-mbstring/bootstrap.php',
'320cde22f66dd4f5d3fd621d3e88b98f' => $vendorDir . '/symfony/polyfill-ctype/bootstrap.php',
'db8f07921940950622db3b8c6eb0e789' => $baseDir . '/app/funcs.php',
'8825ede83f2f289127722d4e842cf7e8' => $vendorDir . '/symfony/polyfill-intl-grapheme/bootstrap.php',
'e69f7f6ee287b969198c3c9d6777bd38' => $vendorDir . '/symfony/polyfill-intl-normalizer/bootstrap.php',
'b6b991a57620e2fb6b2f66f03fe9ddc2' => $vendorDir . '/symfony/string/Resources/functions.php',
'253c157292f75eb38082b5acb06f3f01' => $vendorDir . '/nikic/fast-route/src/functions.php',
'199c49ddb343b99861cd561a257f7a69' => $baseDir . '/app/helpers.php',
'd2136ff22b54ac75cd96a40e0022218e' => $vendorDir . '/workerman/webman-framework/src/support/helpers.php',
);