From 3f3a09e988e2aa2be11dbe304de2e8cbd28a16e8 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Sat, 2 Nov 2013 22:43:11 -0400 Subject: [PATCH] Added support for generating default alias for extensions. --- extensions/composer/Installer.php | 58 ++++++++++++++++++++++++++++++++++---- framework/yii/base/Application.php | 5 ++++ 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/extensions/composer/Installer.php b/extensions/composer/Installer.php index 3a77671..34cbfed 100644 --- a/extensions/composer/Installer.php +++ b/extensions/composer/Installer.php @@ -11,6 +11,7 @@ use Composer\Package\PackageInterface; use Composer\Installer\LibraryInstaller; use Composer\Repository\InstalledRepositoryInterface; use Composer\Script\CommandEvent; +use Composer\Util\Filesystem; /** * @author Qiang Xue @@ -22,6 +23,8 @@ class Installer extends LibraryInstaller const EXTRA_WRITABLE = 'writable'; const EXTRA_EXECUTABLE = 'executable'; + const EXTENSION_FILE = 'yiisoft/extensions.php'; + /** * @inheritdoc */ @@ -65,8 +68,11 @@ class Installer extends LibraryInstaller 'version' => $package->getVersion(), ]; + $alias = $this->generateDefaultAlias($package); + if (!empty($alias)) { + $extension['alias'] = $alias; + } $extra = $package->getExtra(); - if (isset($extra[self::EXTRA_BOOTSTRAP]) && is_string($extra[self::EXTRA_BOOTSTRAP])) { $extension['bootstrap'] = $extra[self::EXTRA_BOOTSTRAP]; } @@ -76,6 +82,28 @@ class Installer extends LibraryInstaller $this->saveExtensions($extensions); } + protected function generateDefaultAlias(PackageInterface $package) + { + $autoload = $package->getAutoload(); + if (!empty($autoload['psr-0'])) { + $fs = new Filesystem; + $vendorDir = $fs->normalizePath($this->vendorDir); + foreach ($autoload['psr-0'] as $name => $path) { + $name = str_replace('\\', '/', trim($name, '\\')); + if (!$fs->isAbsolutePath($path)) { + $path = $this->vendorDir . '/' . $path; + } + $path = $fs->normalizePath($path); + if (strpos($path . '/', $vendorDir . '/') === 0) { + return "['@$name' => '/" . ltrim(var_export(substr($path, strlen($vendorDir)), true), "'") . "']"; + } else { + return "['@$name' => " . var_export($path, true) . ']'; + } + } + } + return false; + } + protected function removePackage(PackageInterface $package) { $packages = $this->loadExtensions(); @@ -85,14 +113,34 @@ class Installer extends LibraryInstaller protected function loadExtensions() { - $file = $this->vendorDir . '/yiisoft/extensions.php'; - return is_file($file) ? require($file) : []; + $file = $this->vendorDir . '/' . self::EXTENSION_FILE; + if (!is_file($file)) { + return []; + } + $extensions = require($file); + + $vendorDir = str_replace('\\', '/', $this->vendorDir); + $n = strlen($vendorDir); + + foreach ($extensions as &$extension) { + if (isset($extension['alias'])) { + foreach ($extension['alias'] as $alias => $path) { + $path = str_replace('\\', '/', $path); + if (strpos($path . '/', $vendorDir . '/') === 0) { + $extension['alias'][$alias] = '' . substr($path, $n); + } + } + } + } + + return $extensions; } protected function saveExtensions(array $extensions) { - $file = $this->vendorDir . '/yiisoft/extensions.php'; - file_put_contents($file, "vendorDir . '/' . self::EXTENSION_FILE; + $array = str_replace("'", '$vendorDir . \'', var_export($extensions, true)); + file_put_contents($file, " $path) { + Yii::setAlias($name, $path); + } + } if (isset($extension['bootstrap'])) { /** @var Extension $class */ $class = $extension['bootstrap'];