|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of fast-forward/dev-tools. |
| 7 | + * |
| 8 | + * This source file is subject to the license bundled |
| 9 | + * with this source code in the file LICENSE. |
| 10 | + * |
| 11 | + * @copyright Copyright (c) 2026 Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 12 | + * @license https://opensource.org/licenses/MIT MIT License |
| 13 | + * |
| 14 | + * @see https://github.com/php-fast-forward/dev-tools |
| 15 | + * @see https://github.com/php-fast-forward |
| 16 | + * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 17 | + */ |
| 18 | + |
| 19 | +namespace FastForward\DevTools\Command; |
| 20 | + |
| 21 | +use Composer\Factory; |
| 22 | +use FastForward\DevTools\License\Generator; |
| 23 | +use FastForward\DevTools\License\GeneratorInterface; |
| 24 | +use FastForward\DevTools\License\PlaceholderResolver; |
| 25 | +use FastForward\DevTools\License\Reader; |
| 26 | +use FastForward\DevTools\License\Resolver; |
| 27 | +use FastForward\DevTools\License\TemplateLoader; |
| 28 | +use SplFileObject; |
| 29 | +use Symfony\Component\Console\Input\InputInterface; |
| 30 | +use Symfony\Component\Console\Output\OutputInterface; |
| 31 | +use Symfony\Component\Filesystem\Filesystem; |
| 32 | + |
| 33 | +/** |
| 34 | + * Generates and copies LICENSE files to projects. |
| 35 | + * |
| 36 | + * This command generates a LICENSE file if one does not exist and a supported |
| 37 | + * license is declared in composer.json. |
| 38 | + */ |
| 39 | +final class CopyLicenseCommand extends AbstractCommand |
| 40 | +{ |
| 41 | + /** |
| 42 | + * Creates a new CopyLicenseCommand instance. |
| 43 | + * |
| 44 | + * @param Filesystem|null $filesystem the filesystem component |
| 45 | + * @param GeneratorInterface|null $generator the generator component |
| 46 | + */ |
| 47 | + public function __construct( |
| 48 | + ?Filesystem $filesystem = null, |
| 49 | + private readonly ?GeneratorInterface $generator = null, |
| 50 | + ) { |
| 51 | + parent::__construct($filesystem); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @return GeneratorInterface |
| 56 | + */ |
| 57 | + private function getGenerator(): GeneratorInterface |
| 58 | + { |
| 59 | + return $this->generator ?? new Generator( |
| 60 | + new Reader(new SplFileObject(Factory::getComposerFile())), |
| 61 | + new Resolver(), |
| 62 | + new TemplateLoader(), |
| 63 | + new PlaceholderResolver(), |
| 64 | + $this->filesystem, |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Configures the current command. |
| 70 | + * |
| 71 | + * This method MUST define the name, description, and help text for the command. |
| 72 | + */ |
| 73 | + protected function configure(): void |
| 74 | + { |
| 75 | + $this |
| 76 | + ->setName('license') |
| 77 | + ->setDescription('Generates a LICENSE file from composer.json license information.') |
| 78 | + ->setHelp( |
| 79 | + 'This command generates a LICENSE file if one does not exist and a supported license is declared in composer.json.' |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Executes the license generation process. |
| 85 | + * |
| 86 | + * Generates a LICENSE file if one does not exist and a supported license is declared in composer.json. |
| 87 | + * |
| 88 | + * @param InputInterface $input the input interface |
| 89 | + * @param OutputInterface $output the output interface |
| 90 | + * |
| 91 | + * @return int the status code |
| 92 | + */ |
| 93 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 94 | + { |
| 95 | + $targetPath = $this->getConfigFile('LICENSE', true); |
| 96 | + |
| 97 | + if ($this->filesystem->exists($targetPath)) { |
| 98 | + $output->writeln('<info>LICENSE file already exists. Skipping generation.</info>'); |
| 99 | + |
| 100 | + return self::SUCCESS; |
| 101 | + } |
| 102 | + |
| 103 | + $license = $this->getGenerator() |
| 104 | + ->generate($targetPath); |
| 105 | + |
| 106 | + if (null === $license) { |
| 107 | + $output->writeln( |
| 108 | + '<comment>No supported license found in composer.json or license is unsupported. Skipping LICENSE generation.</comment>' |
| 109 | + ); |
| 110 | + |
| 111 | + return self::SUCCESS; |
| 112 | + } |
| 113 | + |
| 114 | + $output->writeln('<info>LICENSE file generated successfully.</info>'); |
| 115 | + |
| 116 | + return self::SUCCESS; |
| 117 | + } |
| 118 | +} |
0 commit comments