From 9ef953a85ca6803d87940c48e7af1c2ffa5717a4 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Mon, 7 Oct 2013 23:39:41 -0400 Subject: [PATCH] Yii extension composer installer WIP. --- extensions/composer/yii/composer/Installer.php | 134 +++++++++++++++++++++ .../composer/yii/composer/InstallerPlugin.php | 30 +++++ 2 files changed, 164 insertions(+) create mode 100644 extensions/composer/yii/composer/Installer.php create mode 100644 extensions/composer/yii/composer/InstallerPlugin.php diff --git a/extensions/composer/yii/composer/Installer.php b/extensions/composer/yii/composer/Installer.php new file mode 100644 index 0000000..6ac9e4a --- /dev/null +++ b/extensions/composer/yii/composer/Installer.php @@ -0,0 +1,134 @@ + + * @since 2.0 + */ +class Installer extends LibraryInstaller +{ + /** + * @inheritdoc + */ + public function supports($packageType) + { + return $packageType === 'yii2-extension'; + } + + /** + * @inheritdoc + */ + public function install(InstalledRepositoryInterface $repo, PackageInterface $package) + { + parent::install($repo, $package); + $this->addPackage($package); + } + + /** + * @inheritdoc + */ + public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target) + { + parent::update($repo, $initial, $target); + $this->removePackage($initial); + $this->addPackage($target); + } + + /** + * @inheritdoc + */ + public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package) + { + parent::uninstall($repo, $package); + $this->removePackage($package); + } + + protected function addPackage(PackageInterface $package) + { + $extension = array('name' => $package->getPrettyName()); + + $root = $package->getPrettyName(); + if ($targetDir = $package->getTargetDir()) { + $root .= '/' . trim($targetDir, '/'); + } + $root = trim($root, '/'); + + $extra = $package->getExtra(); + + if (isset($extra['preinit']) && is_string($extra['preinit'])) { + $extension['preinit'] = "/$root/" . ltrim(str_replace('\\', '/', $extra['preinit']), '/'); + } + if (isset($extra['init']) && is_string($extra['init'])) { + $extension['init'] = "/$root/" . ltrim(str_replace('\\', '/', $extra['init']), '/'); + } + + if (isset($extra['aliases']) && is_array($extra['aliases'])) { + foreach ($extra['aliases'] as $alias => $path) { + $extension['aliases']['@' . ltrim($alias, '@')] = "/$root/" . ltrim(str_replace('\\', '/', $path), '/'); + } + } + + if (!empty($aliases)) { + foreach ($aliases as $alias => $path) { + if (strncmp($alias, '@', 1) !== 0) { + $alias = '@' . $alias; + } + $path = trim(str_replace('\\', '/', $path), '/'); + $extension['aliases'][$alias] = $root . '/' . $path; + } + } + + $extensions = $this->loadExtensions(); + $extensions[$package->getId()] = $extension; + $this->saveExtensions($extensions); + } + + protected function removePackage(PackageInterface $package) + { + $packages = $this->loadExtensions(); + unset($packages[$package->getId()]); + $this->saveExtensions($packages); + } + + protected function loadExtensions() + { + $file = $this->vendorDir . '/yii-extensions.php'; + if (!is_file($file)) { + return array(); + } + $extensions = require($file); + /** @var string $vendorDir defined in yii-extensions.php */ + $n = strlen($vendorDir); + foreach ($extensions as &$extension) { + if (isset($extension['aliases'])) { + foreach ($extension['aliases'] as $alias => $path) { + $extension['aliases'][$alias] = '' . substr($path, $n); + } + } + if (isset($extension['preinit'])) { + $extension['preinit'] = '' . substr($extension['preinit'], $n); + } + if (isset($extension['init'])) { + $extension['init'] = '' . substr($extension['init'], $n); + } + } + return $extensions; + } + + protected function saveExtensions(array $extensions) + { + $file = $this->vendorDir . '/yii-extensions.php'; + $array = str_replace("'", '$vendorDir . \'', var_export($extensions, true)); + file_put_contents($file, " + * @since 2.0 + */ +class InstallerPlugin implements PluginInterface +{ + /** + * @inheritdoc + */ + public function activate(Composer $composer, IOInterface $io) + { + $installer = new Installer($io, $composer); + $composer->getInstallationManager()->addInstaller($installer); + } +}