예제 #1
0
 public function actionShowPackage($name)
 {
     $client = new app\modules\core\components\PackagistClient();
     try {
         $package = $client->get($name);
         /** @var Version[] $versions */
         $versions = $package->getVersions();
         /** @var Version $version */
         // we are assuming that first version is latest
         $version = array_shift($versions);
         $data = ['package' => $package, 'version' => $version];
         if (Yii::$app->request->isAjax === true) {
             return $this->renderAjax('show-package', $data);
         } else {
             return $this->render('show-package', $data);
         }
     } catch (\Guzzle\Http\Exception\ClientErrorResponseException $e) {
         Yii::$app->session->setFlash('error', Yii::t('app', 'Package not found on packagist.'));
         return $this->handleActionEnd();
     }
 }
예제 #2
0
 public static function installExtension($name, $updateComposer = false)
 {
     $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();
         $package = $client->get($name);
         /** @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) {
             throw new ErrorException(Yii::t('app', 'Unable to save extension to database'));
         }
         if ($extension->installExtensionPackage() === false) {
             throw new ErrorException(Yii::t('app', 'Could not install extension package'));
         }
         $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) {
         throw new ErrorException(Yii::t('app', 'Unable to activate extension') . ': ' . $e->getMessage());
     }
     if ($result) {
         $extension->is_active = 1;
         $extension->save();
     }
     return $extension;
 }