ScriptHandler.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sensio\Bundle\DistributionBundle\Composer;
  11. use Symfony\Component\ClassLoader\ClassCollectionLoader;
  12. use Symfony\Component\Process\Process;
  13. use Symfony\Component\Process\PhpExecutableFinder;
  14. /**
  15. * @author Jordi Boggiano <j.boggiano@seld.be>
  16. */
  17. class ScriptHandler
  18. {
  19. public static function buildBootstrap($event)
  20. {
  21. $options = self::getOptions($event);
  22. $appDir = $options['symfony-app-dir'];
  23. if (!is_dir($appDir)) {
  24. echo 'The symfony-app-dir ('.$appDir.') specified in composer.json was not found in '.getcwd().', can not build bootstrap file.'.PHP_EOL;
  25. return;
  26. }
  27. static::executeBuildBootstrap($appDir, $options['process-timeout']);
  28. }
  29. public static function clearCache($event)
  30. {
  31. $options = self::getOptions($event);
  32. $appDir = $options['symfony-app-dir'];
  33. if (!is_dir($appDir)) {
  34. echo 'The symfony-app-dir ('.$appDir.') specified in composer.json was not found in '.getcwd().', can not clear the cache.'.PHP_EOL;
  35. return;
  36. }
  37. static::executeCommand($event, $appDir, 'cache:clear --no-warmup', $options['process-timeout']);
  38. }
  39. public static function installAssets($event)
  40. {
  41. $options = self::getOptions($event);
  42. $appDir = $options['symfony-app-dir'];
  43. $webDir = $options['symfony-web-dir'];
  44. $symlink = '';
  45. if ($options['symfony-assets-install'] == 'symlink') {
  46. $symlink = '--symlink ';
  47. } elseif ($options['symfony-assets-install'] == 'relative') {
  48. $symlink = '--symlink --relative ';
  49. }
  50. if (!is_dir($webDir)) {
  51. echo 'The symfony-web-dir ('.$webDir.') specified in composer.json was not found in '.getcwd().', can not install assets.'.PHP_EOL;
  52. return;
  53. }
  54. static::executeCommand($event, $appDir, 'assets:install '.$symlink.escapeshellarg($webDir));
  55. }
  56. public static function installRequirementsFile($event)
  57. {
  58. $options = self::getOptions($event);
  59. $appDir = $options['symfony-app-dir'];
  60. if (!is_dir($appDir)) {
  61. echo 'The symfony-app-dir ('.$appDir.') specified in composer.json was not found in '.getcwd().', can not install the requirements file.'.PHP_EOL;
  62. return;
  63. }
  64. copy(__DIR__.'/../Resources/skeleton/app/SymfonyRequirements.php', $appDir.'/SymfonyRequirements.php');
  65. copy(__DIR__.'/../Resources/skeleton/app/check.php', $appDir.'/check.php');
  66. $webDir = $options['symfony-web-dir'];
  67. if (is_file($webDir.'/config.php')) {
  68. copy(__DIR__.'/../Resources/skeleton/web/config.php', $webDir.'/config.php');
  69. }
  70. }
  71. public static function doBuildBootstrap($appDir)
  72. {
  73. $file = $appDir.'/bootstrap.php.cache';
  74. if (file_exists($file)) {
  75. unlink($file);
  76. }
  77. ClassCollectionLoader::load(array(
  78. 'Symfony\\Component\\DependencyInjection\\ContainerAwareInterface',
  79. // Cannot be included because annotations will parse the big compiled class file
  80. //'Symfony\\Component\\DependencyInjection\\ContainerAware',
  81. 'Symfony\\Component\\DependencyInjection\\Container',
  82. 'Symfony\\Component\\HttpKernel\\Kernel',
  83. 'Symfony\\Component\\ClassLoader\\ClassCollectionLoader',
  84. 'Symfony\\Component\\ClassLoader\\ApcClassLoader',
  85. 'Symfony\\Component\\HttpKernel\\Bundle\\Bundle',
  86. 'Symfony\\Component\\Config\\ConfigCache',
  87. 'Symfony\\Bundle\\FrameworkBundle\\HttpKernel',
  88. // cannot be included as commands are discovered based on the path to this class via Reflection
  89. //'Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle',
  90. ), dirname($file), basename($file, '.php.cache'), false, false, '.php.cache');
  91. file_put_contents($file, sprintf("<?php
  92. namespace { \$loader = require_once __DIR__.'/autoload.php'; }
  93. %s
  94. namespace { return \$loader; }
  95. ", substr(file_get_contents($file), 5)));
  96. }
  97. protected static function executeCommand($event, $appDir, $cmd, $timeout = 300)
  98. {
  99. $php = escapeshellarg(self::getPhp());
  100. $console = escapeshellarg($appDir.'/console');
  101. if ($event->getIO()->isDecorated()) {
  102. $console.= ' --ansi';
  103. }
  104. $process = new Process($php.' '.$console.' '.$cmd, null, null, null, $timeout);
  105. $process->run(function ($type, $buffer) { echo $buffer; });
  106. if (!$process->isSuccessful()) {
  107. throw new \RuntimeException(sprintf('An error occurred when executing the "%s" command.', escapeshellarg($cmd)));
  108. }
  109. }
  110. protected static function executeBuildBootstrap($appDir, $timeout = 300)
  111. {
  112. $php = escapeshellarg(self::getPhp());
  113. $cmd = escapeshellarg(__DIR__.'/../Resources/bin/build_bootstrap.php');
  114. $appDir = escapeshellarg($appDir);
  115. $process = new Process($php.' '.$cmd.' '.$appDir, null, null, null, $timeout);
  116. $process->run(function ($type, $buffer) { echo $buffer; });
  117. if (!$process->isSuccessful()) {
  118. throw new \RuntimeException('An error occurred when generating the bootstrap file.');
  119. }
  120. }
  121. protected static function getOptions($event)
  122. {
  123. $options = array_merge(array(
  124. 'symfony-app-dir' => 'app',
  125. 'symfony-web-dir' => 'web',
  126. 'symfony-assets-install' => 'hard'
  127. ), $event->getComposer()->getPackage()->getExtra());
  128. $options['symfony-assets-install'] = getenv('SYMFONY_ASSETS_INSTALL') ?: $options['symfony-assets-install'];
  129. $options['process-timeout'] = $event->getComposer()->getConfig()->get('process-timeout');
  130. return $options;
  131. }
  132. protected static function getPhp()
  133. {
  134. $phpFinder = new PhpExecutableFinder;
  135. if (!$phpPath = $phpFinder->find()) {
  136. throw new \RuntimeException('The php executable could not be found, add it to your PATH environment variable and try again');
  137. }
  138. return $phpPath;
  139. }
  140. }