123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php
-
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
- namespace Sensio\Bundle\DistributionBundle\Composer;
-
- use Symfony\Component\ClassLoader\ClassCollectionLoader;
- use Symfony\Component\Process\Process;
- use Symfony\Component\Process\PhpExecutableFinder;
-
- /**
- * @author Jordi Boggiano <j.boggiano@seld.be>
- */
- class ScriptHandler
- {
- public static function buildBootstrap($event)
- {
- $options = self::getOptions($event);
- $appDir = $options['symfony-app-dir'];
-
- if (!is_dir($appDir)) {
- echo 'The symfony-app-dir ('.$appDir.') specified in composer.json was not found in '.getcwd().', can not build bootstrap file.'.PHP_EOL;
-
- return;
- }
-
- static::executeBuildBootstrap($appDir, $options['process-timeout']);
- }
-
- public static function clearCache($event)
- {
- $options = self::getOptions($event);
- $appDir = $options['symfony-app-dir'];
-
- if (!is_dir($appDir)) {
- echo 'The symfony-app-dir ('.$appDir.') specified in composer.json was not found in '.getcwd().', can not clear the cache.'.PHP_EOL;
-
- return;
- }
-
- static::executeCommand($event, $appDir, 'cache:clear --no-warmup', $options['process-timeout']);
- }
-
- public static function installAssets($event)
- {
- $options = self::getOptions($event);
- $appDir = $options['symfony-app-dir'];
- $webDir = $options['symfony-web-dir'];
-
- $symlink = '';
- if ($options['symfony-assets-install'] == 'symlink') {
- $symlink = '--symlink ';
- } elseif ($options['symfony-assets-install'] == 'relative') {
- $symlink = '--symlink --relative ';
- }
-
- if (!is_dir($webDir)) {
- echo 'The symfony-web-dir ('.$webDir.') specified in composer.json was not found in '.getcwd().', can not install assets.'.PHP_EOL;
-
- return;
- }
-
- static::executeCommand($event, $appDir, 'assets:install '.$symlink.escapeshellarg($webDir));
- }
-
- public static function installRequirementsFile($event)
- {
- $options = self::getOptions($event);
- $appDir = $options['symfony-app-dir'];
-
- if (!is_dir($appDir)) {
- echo 'The symfony-app-dir ('.$appDir.') specified in composer.json was not found in '.getcwd().', can not install the requirements file.'.PHP_EOL;
-
- return;
- }
-
- copy(__DIR__.'/../Resources/skeleton/app/SymfonyRequirements.php', $appDir.'/SymfonyRequirements.php');
- copy(__DIR__.'/../Resources/skeleton/app/check.php', $appDir.'/check.php');
-
- $webDir = $options['symfony-web-dir'];
-
- if (is_file($webDir.'/config.php')) {
- copy(__DIR__.'/../Resources/skeleton/web/config.php', $webDir.'/config.php');
- }
- }
-
- public static function doBuildBootstrap($appDir)
- {
- $file = $appDir.'/bootstrap.php.cache';
- if (file_exists($file)) {
- unlink($file);
- }
-
- ClassCollectionLoader::load(array(
- 'Symfony\\Component\\DependencyInjection\\ContainerAwareInterface',
- // Cannot be included because annotations will parse the big compiled class file
- //'Symfony\\Component\\DependencyInjection\\ContainerAware',
- 'Symfony\\Component\\DependencyInjection\\Container',
- 'Symfony\\Component\\HttpKernel\\Kernel',
- 'Symfony\\Component\\ClassLoader\\ClassCollectionLoader',
- 'Symfony\\Component\\ClassLoader\\ApcClassLoader',
- 'Symfony\\Component\\HttpKernel\\Bundle\\Bundle',
- 'Symfony\\Component\\Config\\ConfigCache',
- 'Symfony\\Bundle\\FrameworkBundle\\HttpKernel',
- // cannot be included as commands are discovered based on the path to this class via Reflection
- //'Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle',
- ), dirname($file), basename($file, '.php.cache'), false, false, '.php.cache');
-
- file_put_contents($file, sprintf("<?php
-
- namespace { \$loader = require_once __DIR__.'/autoload.php'; }
-
- %s
-
- namespace { return \$loader; }
- ", substr(file_get_contents($file), 5)));
- }
-
- protected static function executeCommand($event, $appDir, $cmd, $timeout = 300)
- {
- $php = escapeshellarg(self::getPhp());
- $console = escapeshellarg($appDir.'/console');
- if ($event->getIO()->isDecorated()) {
- $console.= ' --ansi';
- }
-
- $process = new Process($php.' '.$console.' '.$cmd, null, null, null, $timeout);
- $process->run(function ($type, $buffer) { echo $buffer; });
- if (!$process->isSuccessful()) {
- throw new \RuntimeException(sprintf('An error occurred when executing the "%s" command.', escapeshellarg($cmd)));
- }
- }
-
- protected static function executeBuildBootstrap($appDir, $timeout = 300)
- {
- $php = escapeshellarg(self::getPhp());
- $cmd = escapeshellarg(__DIR__.'/../Resources/bin/build_bootstrap.php');
- $appDir = escapeshellarg($appDir);
-
- $process = new Process($php.' '.$cmd.' '.$appDir, null, null, null, $timeout);
- $process->run(function ($type, $buffer) { echo $buffer; });
- if (!$process->isSuccessful()) {
- throw new \RuntimeException('An error occurred when generating the bootstrap file.');
- }
- }
-
- protected static function getOptions($event)
- {
- $options = array_merge(array(
- 'symfony-app-dir' => 'app',
- 'symfony-web-dir' => 'web',
- 'symfony-assets-install' => 'hard'
- ), $event->getComposer()->getPackage()->getExtra());
-
- $options['symfony-assets-install'] = getenv('SYMFONY_ASSETS_INSTALL') ?: $options['symfony-assets-install'];
-
- $options['process-timeout'] = $event->getComposer()->getConfig()->get('process-timeout');
-
- return $options;
- }
-
- protected static function getPhp()
- {
- $phpFinder = new PhpExecutableFinder;
- if (!$phpPath = $phpFinder->find()) {
- throw new \RuntimeException('The php executable could not be found, add it to your PATH environment variable and try again');
- }
-
- return $phpPath;
- }
- }
|