Skip to content

Commit

Permalink
Added support for generating default alias for extensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
qiangxue committed Nov 3, 2013
1 parent d8b94d6 commit 3f3a09e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
58 changes: 53 additions & 5 deletions extensions/composer/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Composer\Installer\LibraryInstaller;
use Composer\Repository\InstalledRepositoryInterface;
use Composer\Script\CommandEvent;
use Composer\Util\Filesystem;

/**
* @author Qiang Xue <qiang.xue@gmail.com>
Expand All @@ -22,6 +23,8 @@ class Installer extends LibraryInstaller
const EXTRA_WRITABLE = 'writable';
const EXTRA_EXECUTABLE = 'executable';

const EXTENSION_FILE = 'yiisoft/extensions.php';

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -65,8 +68,11 @@ protected function addPackage(PackageInterface $package)
'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];
}
Expand All @@ -76,6 +82,28 @@ protected function addPackage(PackageInterface $package)
$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' => '<vendor-dir>/" . 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();
Expand All @@ -85,14 +113,34 @@ protected function removePackage(PackageInterface $package)

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] = '<vendor-dir>' . substr($path, $n);
}
}
}
}

return $extensions;
}

protected function saveExtensions(array $extensions)
{
$file = $this->vendorDir . '/yiisoft/extensions.php';
file_put_contents($file, "<?php\nreturn " . var_export($extensions, true) . ";\n");
$file = $this->vendorDir . '/' . self::EXTENSION_FILE;
$array = str_replace("'<vendor-dir>", '$vendorDir . \'', var_export($extensions, true));
file_put_contents($file, "<?php\n\n\$vendorDir = dirname(__DIR__);\n\nreturn $array;\n");
}


Expand Down
5 changes: 5 additions & 0 deletions framework/yii/base/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ public function init()
protected function initExtensions($extensions)
{
foreach ($extensions as $extension) {
if (!empty($extension['alias'])) {
foreach ($extension['alias'] as $name => $path) {
Yii::setAlias($name, $path);
}
}
if (isset($extension['bootstrap'])) {
/** @var Extension $class */
$class = $extension['bootstrap'];
Expand Down

0 comments on commit 3f3a09e

Please sign in to comment.