You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
235 lines
6.2 KiB
235 lines
6.2 KiB
11 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
|
namespace yii\composer;
|
||
|
|
||
|
use Composer\Package\PackageInterface;
|
||
|
use Composer\Installer\LibraryInstaller;
|
||
|
use Composer\Repository\InstalledRepositoryInterface;
|
||
11 years ago
|
use Composer\Script\CommandEvent;
|
||
11 years ago
|
use Composer\Util\Filesystem;
|
||
11 years ago
|
|
||
|
/**
|
||
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
|
class Installer extends LibraryInstaller
|
||
|
{
|
||
11 years ago
|
const EXTRA_BOOTSTRAP = 'bootstrap';
|
||
11 years ago
|
const EXTRA_WRITABLE = 'writable';
|
||
|
const EXTRA_EXECUTABLE = 'executable';
|
||
11 years ago
|
|
||
11 years ago
|
const EXTENSION_FILE = 'yiisoft/extensions.php';
|
||
|
|
||
11 years ago
|
/**
|
||
11 years ago
|
* @inheritdoc
|
||
11 years ago
|
*/
|
||
|
public function supports($packageType)
|
||
|
{
|
||
11 years ago
|
return $packageType === 'yii2-extension';
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* @inheritdoc
|
||
11 years ago
|
*/
|
||
|
public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
|
||
|
{
|
||
11 years ago
|
// install the package the normal composer way
|
||
11 years ago
|
parent::install($repo, $package);
|
||
11 years ago
|
// add the package to yiisoft/extensions.php
|
||
11 years ago
|
$this->addPackage($package);
|
||
11 years ago
|
// ensure the yii2-dev package also provides Yii.php in the same place as yii2 does
|
||
|
if ($package->getName() == 'yiisoft/yii2-dev') {
|
||
|
$this->linkYiiBaseFiles();
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* @inheritdoc
|
||
11 years ago
|
*/
|
||
|
public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target)
|
||
|
{
|
||
|
parent::update($repo, $initial, $target);
|
||
|
$this->removePackage($initial);
|
||
|
$this->addPackage($target);
|
||
11 years ago
|
// ensure the yii2-dev package also provides Yii.php in the same place as yii2 does
|
||
11 years ago
|
if ($initial->getName() == 'yiisoft/yii2-dev') {
|
||
11 years ago
|
$this->linkYiiBaseFiles();
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* @inheritdoc
|
||
11 years ago
|
*/
|
||
|
public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package)
|
||
|
{
|
||
11 years ago
|
// uninstall the package the normal composer way
|
||
11 years ago
|
parent::uninstall($repo, $package);
|
||
11 years ago
|
// remove the package from yiisoft/extensions.php
|
||
11 years ago
|
$this->removePackage($package);
|
||
11 years ago
|
// remove links for Yii.php
|
||
|
if ($package->getName() == 'yiisoft/yii2-dev') {
|
||
|
$this->removeYiiBaseFiles();
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
|
protected function addPackage(PackageInterface $package)
|
||
|
{
|
||
11 years ago
|
$extension = [
|
||
11 years ago
|
'name' => $package->getName(),
|
||
11 years ago
|
'version' => $package->getVersion(),
|
||
|
];
|
||
11 years ago
|
|
||
11 years ago
|
$alias = $this->generateDefaultAlias($package);
|
||
|
if (!empty($alias)) {
|
||
|
$extension['alias'] = $alias;
|
||
|
}
|
||
11 years ago
|
$extra = $package->getExtra();
|
||
11 years ago
|
if (isset($extra[self::EXTRA_BOOTSTRAP]) && is_string($extra[self::EXTRA_BOOTSTRAP])) {
|
||
|
$extension['bootstrap'] = $extra[self::EXTRA_BOOTSTRAP];
|
||
11 years ago
|
}
|
||
|
|
||
|
$extensions = $this->loadExtensions();
|
||
11 years ago
|
$extensions[$package->getName()] = $extension;
|
||
11 years ago
|
$this->saveExtensions($extensions);
|
||
|
}
|
||
|
|
||
11 years ago
|
protected function generateDefaultAlias(PackageInterface $package)
|
||
|
{
|
||
|
$autoload = $package->getAutoload();
|
||
11 years ago
|
if (empty($autoload['psr-0'])) {
|
||
|
return false;
|
||
|
}
|
||
|
$fs = new Filesystem;
|
||
|
$vendorDir = $fs->normalizePath($this->vendorDir);
|
||
11 years ago
|
$aliases = [];
|
||
11 years ago
|
foreach ($autoload['psr-0'] as $name => $path) {
|
||
|
$name = str_replace('\\', '/', trim($name, '\\'));
|
||
|
if (!$fs->isAbsolutePath($path)) {
|
||
|
$path = $this->vendorDir . '/' . $package->getName() . '/' . $path;
|
||
|
}
|
||
|
$path = $fs->normalizePath($path);
|
||
|
if (strpos($path . '/', $vendorDir . '/') === 0) {
|
||
11 years ago
|
$aliases["@$name"] = '<vendor-dir>' . substr($path, strlen($vendorDir)) . '/' . $name;
|
||
11 years ago
|
} else {
|
||
11 years ago
|
$aliases["@$name"] = $path . '/' . $name;
|
||
11 years ago
|
}
|
||
|
}
|
||
11 years ago
|
return $aliases;
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
protected function removePackage(PackageInterface $package)
|
||
|
{
|
||
|
$packages = $this->loadExtensions();
|
||
11 years ago
|
unset($packages[$package->getName()]);
|
||
11 years ago
|
$this->saveExtensions($packages);
|
||
|
}
|
||
|
|
||
|
protected function loadExtensions()
|
||
|
{
|
||
11 years ago
|
$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;
|
||
11 years ago
|
}
|
||
|
|
||
|
protected function saveExtensions(array $extensions)
|
||
|
{
|
||
11 years ago
|
$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");
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
11 years ago
|
protected function linkYiiBaseFiles()
|
||
|
{
|
||
|
$yiiDir = $this->vendorDir . '/yiisoft/yii2/yii';
|
||
|
if (!file_exists($yiiDir)) {
|
||
|
mkdir($yiiDir, 0777, true);
|
||
|
}
|
||
|
foreach(['Yii.php', 'YiiBase.php', 'classes.php'] as $file) {
|
||
|
file_put_contents($yiiDir . '/' . $file, <<<EOF
|
||
|
<?php
|
||
|
/**
|
||
|
* This is a link provided by the yiisoft/yii2-dev package via yii2-composer plugin.
|
||
|
*
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
|
return require(__DIR__ . '/../../yii2-dev/framework/yii/$file');
|
||
|
|
||
|
EOF
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected function removeYiiBaseFiles()
|
||
|
{
|
||
|
$yiiDir = $this->vendorDir . '/yiisoft/yii2/yii';
|
||
|
foreach(['Yii.php', 'YiiBase.php', 'classes.php'] as $file) {
|
||
|
if (file_exists($yiiDir . '/' . $file)) {
|
||
|
unlink($yiiDir . '/' . $file);
|
||
|
}
|
||
|
}
|
||
|
if (file_exists($yiiDir)) {
|
||
|
rmdir($yiiDir);
|
||
|
}
|
||
|
}
|
||
11 years ago
|
|
||
|
/**
|
||
11 years ago
|
* Sets the correct permission for the files and directories listed in the extra section.
|
||
11 years ago
|
* @param CommandEvent $event
|
||
|
*/
|
||
11 years ago
|
public static function setPermission($event)
|
||
11 years ago
|
{
|
||
11 years ago
|
$options = array_merge([
|
||
11 years ago
|
self::EXTRA_WRITABLE => [],
|
||
|
self::EXTRA_EXECUTABLE => [],
|
||
11 years ago
|
], $event->getComposer()->getPackage()->getExtra());
|
||
11 years ago
|
|
||
11 years ago
|
foreach ((array)$options[self::EXTRA_WRITABLE] as $path) {
|
||
11 years ago
|
echo "Setting writable: $path ...";
|
||
|
if (is_dir($path)) {
|
||
|
chmod($path, 0777);
|
||
|
echo "done\n";
|
||
|
} else {
|
||
|
echo "The directory was not found: " . getcwd() . DIRECTORY_SEPARATOR . $path;
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
foreach ((array)$options[self::EXTRA_EXECUTABLE] as $path) {
|
||
11 years ago
|
echo "Setting executable: $path ...";
|
||
|
if (is_file($path)) {
|
||
|
chmod($path, 0755);
|
||
|
echo "done\n";
|
||
|
} else {
|
||
|
echo "\n\tThe file was not found: " . getcwd() . DIRECTORY_SEPARATOR . $path . "\n";
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
11 years ago
|
}
|