vendor/ibexa/cron/src/bundle/Registry/CronJobsRegistry.php line 53

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. namespace Ibexa\Bundle\Cron\Registry;
  7. use Cron\Job\ShellJob;
  8. use Cron\Schedule\CrontabSchedule;
  9. use Ibexa\Core\MVC\Symfony\SiteAccess;
  10. use Symfony\Component\Console\Command\Command;
  11. use Symfony\Component\Process\PhpExecutableFinder;
  12. class CronJobsRegistry
  13. {
  14. public const DEFAULT_CATEGORY = 'default';
  15. /**
  16. * @var array
  17. */
  18. protected $cronJobs = [];
  19. /**
  20. * @var false|string
  21. */
  22. protected $executable;
  23. /**
  24. * @var string
  25. */
  26. protected $environment;
  27. /**
  28. * @var \Ibexa\Core\MVC\Symfony\SiteAccess
  29. */
  30. protected $siteaccess;
  31. /**
  32. * @var string
  33. */
  34. protected $options;
  35. public function __construct(string $environment, SiteAccess $siteaccess)
  36. {
  37. $finder = new PhpExecutableFinder();
  38. $this->executable = $finder->find();
  39. $this->environment = $environment;
  40. $this->siteaccess = $siteaccess;
  41. }
  42. public function addCronJob(Command $command, string $schedule = null, string $category = self::DEFAULT_CATEGORY, string $options = ''): void
  43. {
  44. $command = sprintf(
  45. '%s %s %s %s --siteaccess=%s --env=%s',
  46. $this->executable,
  47. $_SERVER['SCRIPT_NAME'],
  48. $command->getName(),
  49. $options,
  50. $this->siteaccess->name,
  51. $this->environment
  52. );
  53. $job = new ShellJob();
  54. $job->setSchedule(new CrontabSchedule($schedule));
  55. $job->setCommand($command);
  56. $this->cronJobs[$category][] = $job;
  57. }
  58. /**
  59. * @return \Cron\Job\ShellJob[]
  60. */
  61. public function getCategoryCronJobs(string $category): array
  62. {
  63. if (!isset($this->cronJobs[$category])) {
  64. return [];
  65. }
  66. return $this->cronJobs[$category];
  67. }
  68. }
  69. class_alias(CronJobsRegistry::class, 'EzSystems\EzPlatformCronBundle\Registry\CronJobsRegistry');