Ejemplo n.º 1
0
 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();
 }
Ejemplo n.º 2
0
 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;
 }