Bolt\Composer\Action\RequirePackage::execute PHP Method

execute() public method

Require (install) a package.
public execute ( array $package ) : integer
$package array Package names and version to require - Format: ['name' => '', 'version' => '']
return integer 0 on success or a positive error code on failure
    public function execute(array $package)
    {
        $this->getComposer();
        $io = $this->getIO();
        /** @var \Bolt\Filesystem\Handler\JsonFile $jsonFile */
        $jsonFile = $this->getOptions()->composerJson();
        $newlyCreated = !$jsonFile->exists();
        if ($newlyCreated) {
            $this->app['extend.manager.json']->update();
        }
        // Format the package array
        $package = $this->formatRequirements($package);
        // Validate requirements format
        $versionParser = new VersionParser();
        foreach ($package as $constraint) {
            $versionParser->parseConstraints($constraint);
        }
        // Get a back up of the file contents
        $composerBackup = $jsonFile->parse();
        // Update our JSON file now with a specific version.
        // This is what Composer will read, and use, internally during the process.
        // After that is complete, we'll re-save with a constraint
        $this->updateComposerJson($jsonFile, $package, false);
        // JSON file has been created/updated, if we're not installing, exit
        if ($this->getOptions()->noUpdate()) {
            return 0;
        }
        // Reload Composer config
        $composer = $this->resetComposer();
        /** @var $install \Composer\Installer */
        $install = Installer::create($io, $composer)->setVerbose($this->getOptions()->verbose())->setPreferSource($this->getOptions()->preferSource())->setPreferDist($this->getOptions()->preferDist())->setDevMode(!$this->getOptions()->updateNoDev())->setOptimizeAutoloader($this->getOptions()->optimizeAutoloader())->setClassMapAuthoritative($this->getOptions()->classmapAuthoritative())->setUpdate($this->getOptions()->update())->setUpdateWhitelist(array_keys($package))->setWhitelistDependencies($this->getOptions()->updateWithDependencies())->setIgnorePlatformRequirements($this->getOptions()->ignorePlatformReqs())->setRunScripts(!$this->getOptions()->noScripts());
        try {
            $status = $install->run();
            if ($status !== 0) {
                if ($newlyCreated) {
                    // Installation failed, deleting JSON
                    $jsonFile->delete();
                } else {
                    // Installation failed, reverting JSON to its original content
                    $jsonFile->dump($composerBackup);
                }
            }
            // Update our JSON file now with a constraint
            $this->updateComposerJson($jsonFile, $package, true);
            return $status;
        } catch (\Exception $e) {
            // Installation failed, reverting JSON to its original content
            $jsonFile->dump($composerBackup);
            $msg = sprintf('%s recieved an error from Composer: %s in %s::%s', __METHOD__, $e->getMessage(), $e->getFile(), $e->getLine());
            $this->app['logger.system']->critical($msg, ['event' => 'exception', 'exception' => $e]);
            throw new PackageManagerException($e->getMessage(), $e->getCode(), $e);
        }
    }

Usage Example

コード例 #1
0
ファイル: PackageManager.php プロジェクト: aleksabp/bolt
 /**
  * Require (install) packages.
  *
  * @param $packages array Associative array of package names/versions to remove
  *                        Format: array('name' => '', 'version' => '')
  *
  * @return integer 0 on success or a positive error code on failure
  */
 public function requirePackage(array $packages)
 {
     if (!$this->require) {
         $this->require = new RequirePackage($this->app);
     }
     // 0 on success or a positive error code on failure
     return $this->require->execute($packages);
 }
All Usage Examples Of Bolt\Composer\Action\RequirePackage::execute