public function actionInstall($package) { $installed = Extensions::isPackageInstalled($package); if ($path = $this->checkInJson($package, true)) { // it's in studio Extensions::installStudioPackage($package, $path); } else { // it's in composer Extensions::installExtension($package, !$this->checkInJson($package, false)); } }
public function actionInstallExtension($name, $updateComposer = '0') { $updateComposer = boolval($updateComposer); $extension = null; /** @var app\modules\core\helpers\UpdateHelper $updateHelper */ if (Extensions::isPackageInstalled($name) === true) { // we should just activate it $extension = Extensions::findByName($name); } else { $client = new app\modules\core\components\PackagistClient(); try { $package = $client->get($name); } catch (\Guzzle\Http\Exception\ClientErrorResponseException $e) { Yii::$app->session->setFlash('error', Yii::t('app', 'Package not found on packagist.')); return $this->handleActionEnd(); } /** @var Version[] $versions */ $versions = $package->getVersions(); /** @var Version $version */ // we are assuming that first version is latest $version = array_shift($versions); $extension = new Extensions(); $extension->name = $name; $extension->homepage = $version->getHomepage(); $extension->current_package_version_timestamp = date('Y-m-d H:i:s', strtotime($version->getTime())); $extension->latest_package_version_timestamp = date('Y-m-d H:i:s', strtotime($version->getTime())); $autoload = $version->getAutoload(); if (isset($autoload['psr-4'])) { $namespaces = array_keys($autoload['psr-4']); $prefix = array_shift($namespaces); if (isset(array_keys($autoload['psr-4'])[0])) { $extension->namespace_prefix = $prefix; } } $extension->is_active = 0; if ($extension->save() === false) { Yii::$app->session->setFlash('error', Yii::t('app', 'Unable to save extension to database') . var_export($extension->errors, true)); return $this->handleActionEnd(); } if ($extension->installExtensionPackage() === false) { return $this->handleActionEnd(); } $loader = (require Yii::getAlias('@app/vendor/autoload.php')); $psr4 = (require Yii::getAlias('@app/vendor/composer/autoload_psr4.php')); foreach ($psr4 as $prefix => $paths) { $loader->setPsr4($prefix, $paths); } } if ($extension === null) { throw new NotFoundHttpException(); } try { $result = $extension->activateExtension($updateComposer); } catch (\Exception $e) { Yii::$app->session->setFlash('error', Yii::t('app', 'Unable to activate extension') . ': ' . $e->getMessage()); return $this->handleActionEnd(); } if ($result) { $extension->is_active = 1; $extension->save(); } return $this->handleActionEnd(); }
echo Html::encode($package->getName()); ?> <small><?php echo $version->getVersion(); ?> </small> </h2> <?php echo Yii::t('app', 'Extension homepage'); ?> : <?php echo Html::a($version->getHomepage(), $version->getHomepage(), ['target' => '_blank']); ?> <div class="description"> <?php echo Html::encode($version->getDescription()); ?> </div> <div class="btn-group"> <?php if (Extensions::isPackageInstalled($package->getName())) { echo $this->render('_installed-package', ['package' => $package]); } else { echo Html::a(Icon::show('cloud-download') . ' ' . Yii::t('app', 'Install extension'), ['/core/backend-extensions/install-extension', 'name' => $package->getName()], ['class' => 'btn btn-primary', 'data-action' => 'post']); } ?> </div> </div>
public static function installStudioPackage($name, $path) { $extension = null; /** @var app\modules\core\helpers\UpdateHelper $updateHelper */ if (Extensions::isPackageInstalled($name) === true) { // we should just activate it $extension = Extensions::findByName($name); } else { $extension = new Extensions(); $extension->name = $name; $extension->current_package_version_timestamp = date('Y-m-d H:i:s'); $extension->latest_package_version_timestamp = date('Y-m-d H:i:s'); $composerPath = rtrim($path, '/') . '/composer.json'; $data = Json::decode(file_get_contents($composerPath), true); $autoload = ArrayHelper::getValue($data, 'autoload', []); if (isset($autoload['psr-4'])) { $namespaces = array_keys($autoload['psr-4']); $prefix = array_shift($namespaces); if (isset(array_keys($autoload['psr-4'])[0])) { $extension->namespace_prefix = $prefix; } } $extension->is_active = 0; if ($extension->save() === false) { throw new ErrorException(Yii::t('app', 'Unable to save extension to database')); } $loader = (require Yii::getAlias('@app/vendor/autoload.php')); $psr4 = (require Yii::getAlias('@app/vendor/composer/autoload_psr4.php')); foreach ($psr4 as $prefix => $paths) { $loader->setPsr4($prefix, $paths); } } //try { $result = $extension->activateExtension(false); // } catch (\Exception $e) { // throw new ErrorException(Yii::t('app', 'Unable to activate extension').': '.$e->getMessage()); // } if ($result) { $extension->is_active = 1; $extension->save(); } return $extension; }