|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PLUS\GrumPHPBomTask; |
| 6 | + |
| 7 | +use Symfony\Component\Finder\SplFileInfo; |
| 8 | +use GrumPHP\Runner\TaskResult; |
| 9 | +use GrumPHP\Runner\TaskResultInterface; |
| 10 | +use GrumPHP\Task\Config\ConfigOptionsResolver; |
| 11 | +use GrumPHP\Task\Config\EmptyTaskConfig; |
| 12 | +use GrumPHP\Task\Config\TaskConfigInterface; |
| 13 | +use GrumPHP\Task\Context\ContextInterface; |
| 14 | +use GrumPHP\Task\Context\GitPreCommitContext; |
| 15 | +use GrumPHP\Task\Context\RunContext; |
| 16 | +use GrumPHP\Task\TaskInterface; |
| 17 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 18 | + |
| 19 | +final class BomFixerTask implements TaskInterface |
| 20 | +{ |
| 21 | + private EmptyTaskConfig|TaskConfigInterface $config; |
| 22 | + |
| 23 | + public function __construct() |
| 24 | + { |
| 25 | + $this->config = new EmptyTaskConfig(); |
| 26 | + } |
| 27 | + |
| 28 | + public static function getConfigurableOptions(): ConfigOptionsResolver |
| 29 | + { |
| 30 | + $resolver = new OptionsResolver(); |
| 31 | + $resolver->setDefaults( |
| 32 | + [ |
| 33 | + 'triggered_by' => ['php', 'css', 'scss', 'less', 'json', 'sql', 'yml', 'txt'], |
| 34 | + ] |
| 35 | + ); |
| 36 | + |
| 37 | + $resolver->addAllowedTypes('triggered_by', ['array']); |
| 38 | + return ConfigOptionsResolver::fromOptionsResolver($resolver); |
| 39 | + } |
| 40 | + |
| 41 | + public function getConfig(): TaskConfigInterface |
| 42 | + { |
| 43 | + return $this->config; |
| 44 | + } |
| 45 | + |
| 46 | + public function withConfig(TaskConfigInterface $config): TaskInterface |
| 47 | + { |
| 48 | + $new = clone $this; |
| 49 | + $new->config = $config; |
| 50 | + |
| 51 | + return $new; |
| 52 | + } |
| 53 | + |
| 54 | + public function canRunInContext(ContextInterface $context): bool |
| 55 | + { |
| 56 | + return $context instanceof RunContext || $context instanceof GitPreCommitContext; |
| 57 | + } |
| 58 | + |
| 59 | + public function run(ContextInterface $context): TaskResultInterface |
| 60 | + { |
| 61 | + $files = $context->getFiles()->extensions($this->config->getOptions()['triggered_by']); |
| 62 | + if (0 === count($files)) { |
| 63 | + return TaskResult::createSkipped($this, $context); |
| 64 | + } |
| 65 | + |
| 66 | + if (is_file('./vendor/bin/fixbom')) { |
| 67 | + $fixCommand = './vendor/bin/fixbom'; |
| 68 | + } elseif (is_file('./bin/fixbom')) { |
| 69 | + $fixCommand = './bin/fixbom'; |
| 70 | + } else { |
| 71 | + $fixCommand = 'fixbom'; |
| 72 | + } |
| 73 | + |
| 74 | + $shouldGetFixedLog = []; |
| 75 | + /** @var SplFileInfo $file */ |
| 76 | + foreach ($files as $file) { |
| 77 | + $execFile = $file->getPathname(); |
| 78 | + if ($this->isFileWithBOM($execFile)) { |
| 79 | + $shouldGetFixedLog[] = $execFile . ' has BOM and should be fixed'; |
| 80 | + $fixCommand .= " '" . $execFile . "'"; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if (count($shouldGetFixedLog) > 0) { |
| 85 | + $errorMessage = implode(PHP_EOL, $shouldGetFixedLog) . PHP_EOL |
| 86 | + . 'you can use this to fix them:' . PHP_EOL |
| 87 | + . $fixCommand; |
| 88 | + return TaskResult::createFailed($this, $context, $errorMessage); |
| 89 | + } |
| 90 | + |
| 91 | + return TaskResult::createPassed($this, $context); |
| 92 | + } |
| 93 | + |
| 94 | + private function isFileWithBOM(string $filename): bool |
| 95 | + { |
| 96 | + return $this->fileInfoSearch($filename, 'BOM'); |
| 97 | + } |
| 98 | + |
| 99 | + private function fileInfoSearch(string $filename, string $search): bool |
| 100 | + { |
| 101 | + $output = []; |
| 102 | + exec('file "' . $filename . '"', $output, $returnVar); |
| 103 | + if ($returnVar !== 0) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + if (empty($output[0])) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + return str_contains($output[0], $search); |
| 112 | + } |
| 113 | +} |
0 commit comments