|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PHPCensor\Plugin; |
| 4 | + |
| 5 | +use PHPCensor; |
| 6 | +use PHPCensor\Builder; |
| 7 | +use PHPCensor\Model\Build; |
| 8 | +use PHPCensor\Model\BuildError; |
| 9 | +use PHPCensor\Plugin; |
| 10 | +use PHPCensor\ZeroConfigPluginInterface; |
| 11 | + |
| 12 | +use hollodotme\CrontabValidator\CrontabValidator; |
| 13 | + |
| 14 | +/** |
| 15 | + * Crontab Linter. |
| 16 | + * |
| 17 | + * @author David Sloan <dave@d3r.com> |
| 18 | + */ |
| 19 | +class CronLint extends Plugin implements ZeroConfigPluginInterface |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + protected $buildDir; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var string[] files to lint |
| 28 | + */ |
| 29 | + protected $files = []; |
| 30 | + |
| 31 | + /** |
| 32 | + * @return string |
| 33 | + */ |
| 34 | + public static function pluginName() |
| 35 | + { |
| 36 | + return 'cron_lint'; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * {@inheritdoc} |
| 41 | + */ |
| 42 | + public function __construct(Builder $builder, Build $build, array $options = []) |
| 43 | + { |
| 44 | + parent::__construct($builder, $build, $options); |
| 45 | + |
| 46 | + $this->buildDir = $this->builder->buildPath; |
| 47 | + |
| 48 | + $this->files = []; |
| 49 | + if (isset($options['files']) && is_array($options['files'])) { |
| 50 | + $this->files = $options['files']; |
| 51 | + } |
| 52 | + |
| 53 | + $this->validator = new CrontabValidator(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * {@inheritdoc} |
| 58 | + */ |
| 59 | + public static function canExecute($stage, Builder $builder, Build $build) |
| 60 | + { |
| 61 | + return ($stage == Build::STAGE_TEST); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Lint each of the files from the options. |
| 66 | + */ |
| 67 | + public function execute() |
| 68 | + { |
| 69 | + // $this->builder->logExecOutput(false); |
| 70 | + // |
| 71 | + |
| 72 | + $this->validator = $validator = new CrontabValidator(); |
| 73 | + |
| 74 | + if (empty($this->files)) { |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + foreach ($this->files as $fileName) { |
| 79 | + $cronFile = sprintf('%s%s', $this->buildDir, $fileName); |
| 80 | + printf("%s\n", $cronFile); |
| 81 | + |
| 82 | + if (!$this->validateFile($cronFile)) { |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + $cronTab = file_get_contents($cronFile); |
| 87 | + $cronLines = explode("\n", $cronTab); |
| 88 | + foreach ($cronLines as $line) { |
| 89 | + if (!$this->validateLine($line)) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return true; |
| 96 | + } |
| 97 | + |
| 98 | + // ------------------------------------------------------------------------ |
| 99 | + // Validation Checks |
| 100 | + // ------------------------------------------------------------------------ |
| 101 | + /** |
| 102 | + * Validate that the file exists. |
| 103 | + * |
| 104 | + * @param string $file |
| 105 | + * @return bool |
| 106 | + */ |
| 107 | + protected function validateFile(string $file) : bool |
| 108 | + { |
| 109 | + if (!file_exists($file)) { |
| 110 | + $this->build->reportError( |
| 111 | + $this->builder, |
| 112 | + self::pluginName(), |
| 113 | + 'Missing Cron File', |
| 114 | + BuildError::SEVERITY_NORMAL, |
| 115 | + $fileName |
| 116 | + ); |
| 117 | + |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + return true; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Validate Cron Line. |
| 126 | + * |
| 127 | + * @param string $line |
| 128 | + * @return bool |
| 129 | + */ |
| 130 | + protected function validateLine(string $line) : bool |
| 131 | + { |
| 132 | + if (!$this->validator->isIntervalValid($line)) { |
| 133 | + $this->build->reportError( |
| 134 | + $this->builder, |
| 135 | + self::pluginName(), |
| 136 | + sprintf('Invalid expression - %s', $line), |
| 137 | + BuildError::SEVERITY_HIGH, |
| 138 | + $fileName |
| 139 | + ); |
| 140 | + return false; |
| 141 | + } |
| 142 | + |
| 143 | + return true; |
| 144 | + } |
| 145 | +} |
0 commit comments