DumpPackagesCommand.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /*
  3. * This file is part of Packagist.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. * Nils Adermann <naderman@naderman.de>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Packagist\WebBundle\Command;
  12. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Input\InputArgument;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. /**
  18. * @author Jordi Boggiano <j.boggiano@seld.be>
  19. */
  20. class DumpPackagesCommand extends ContainerAwareCommand
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. protected function configure()
  26. {
  27. $this
  28. ->setName('packagist:dump')
  29. ->setDefinition(array(
  30. new InputOption('force', null, InputOption::VALUE_NONE, 'Force a dump of all packages'),
  31. ))
  32. ->setDescription('Dumps the packages into a packages.json + included files')
  33. ;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. protected function execute(InputInterface $input, OutputInterface $output)
  39. {
  40. $force = (Boolean) $input->getOption('force');
  41. $verbose = (Boolean) $input->getOption('verbose');
  42. $deployLock = $this->getContainer()->getParameter('kernel.cache_dir').'/deploy.globallock';
  43. if (file_exists($deployLock)) {
  44. if ($verbose) {
  45. $output->writeln('Aborting, '.$deployLock.' file present');
  46. }
  47. return;
  48. }
  49. $doctrine = $this->getContainer()->get('doctrine');
  50. if ($force) {
  51. $packages = $doctrine->getManager()->getConnection()->fetchAll('SELECT id FROM package ORDER BY id ASC');
  52. } else {
  53. $packages = $doctrine->getRepository('PackagistWebBundle:Package')->getStalePackagesForDumping();
  54. }
  55. $ids = array();
  56. foreach ($packages as $package) {
  57. $ids[] = $package['id'];
  58. }
  59. $lock = $this->getContainer()->getParameter('kernel.cache_dir').'/composer-dumper.lock';
  60. $timeout = 30*60;
  61. ini_set('memory_limit', -1);
  62. // another dumper is still active
  63. if (file_exists($lock) && filemtime($lock) > time() - $timeout) {
  64. if ($verbose) {
  65. $output->writeln('Aborting, '.$lock.' file present');
  66. }
  67. return;
  68. }
  69. touch($lock);
  70. $this->getContainer()->get('packagist.package_dumper')->dump($ids, $force, $verbose);
  71. unlink($lock);
  72. }
  73. }