Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
22.22% |
6 / 27 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
IncludeAutoloaderCommand | |
22.22% |
6 / 27 |
|
33.33% |
1 / 3 |
11.53 | |
0.00% |
0 / 1 |
configure | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
6 | |||
createConfig | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Adds ~`require_once 'autoload_aliases.php'` to `vendor/autoload.php`. |
4 | * |
5 | * During development, when running Strauss as a phar, i.e. outside Composer's autoloading, we need to ensure the |
6 | * `autoload_aliases.php` file is loaded. This is injected into Composer's `vendor/autoload.php` when it is first |
7 | * generated, but when `composer dump-autoload` is run, the change is lost. This command is intended to be run in |
8 | * `post-dump-autoload` scripts in `composer.json` to ensure the aliases are loaded. |
9 | * |
10 | * This command DOES NOT generate the `autoload_aliases.php` files. It only inserts the `require` statement into |
11 | * `vendor/autoload.php`. |
12 | * |
13 | * @package brianhenryie/strauss |
14 | */ |
15 | |
16 | namespace BrianHenryIE\Strauss\Console\Commands; |
17 | |
18 | use BrianHenryIE\Strauss\Composer\ComposerPackage; |
19 | use BrianHenryIE\Strauss\Composer\Extra\ReplaceConfigInterface; |
20 | use BrianHenryIE\Strauss\Composer\Extra\StraussConfig; |
21 | use BrianHenryIE\Strauss\Helpers\FileSystem; |
22 | use BrianHenryIE\Strauss\Pipeline\Autoload\VendorComposerAutoload; |
23 | use BrianHenryIE\Strauss\Pipeline\Prefixer; |
24 | use Exception; |
25 | use League\Flysystem\Local\LocalFilesystemAdapter; |
26 | use Psr\Log\LoggerAwareTrait; |
27 | use Psr\Log\LogLevel; |
28 | use Symfony\Component\Console\Command\Command; |
29 | use Symfony\Component\Console\Input\InputInterface; |
30 | use Symfony\Component\Console\Logger\ConsoleLogger; |
31 | use Symfony\Component\Console\Output\OutputInterface; |
32 | |
33 | class IncludeAutoloaderCommand extends Command |
34 | { |
35 | use LoggerAwareTrait; |
36 | |
37 | /** @var string */ |
38 | protected string $workingDir; |
39 | |
40 | protected StraussConfig $config; |
41 | |
42 | protected Filesystem $filesystem; |
43 | |
44 | /** |
45 | * @return void |
46 | */ |
47 | protected function configure() |
48 | { |
49 | $this->setName('include-autoloader'); |
50 | $this->setDescription("Adds `require autoload_aliases.php` and `require vendor-prefixed/autoload.php` to `vendor/autoload.php`."); |
51 | |
52 | // TODO: permissions? |
53 | $this->filesystem = new Filesystem( |
54 | new \League\Flysystem\Filesystem(new LocalFilesystemAdapter('/')), |
55 | getcwd() . '/' |
56 | ); |
57 | } |
58 | |
59 | /** |
60 | * @param InputInterface $input |
61 | * @param OutputInterface $output |
62 | * |
63 | * @see Command::execute() |
64 | * |
65 | */ |
66 | protected function execute(InputInterface $input, OutputInterface $output): int |
67 | { |
68 | $logger = new ConsoleLogger( |
69 | $output, |
70 | [ LogLevel::INFO => OutputInterface::VERBOSITY_NORMAL ] |
71 | ); |
72 | |
73 | $this->setLogger($logger); |
74 | |
75 | $workingDir = getcwd() . '/'; |
76 | $this->workingDir = $workingDir; |
77 | |
78 | try { |
79 | $config = $this->createConfig($input); |
80 | |
81 | // Pipeline |
82 | |
83 | // TODO: check for `--no-dev` somewhere. |
84 | |
85 | $vendorComposerAutoload = new VendorComposerAutoload( |
86 | $config, |
87 | $this->filesystem, |
88 | $logger |
89 | ); |
90 | |
91 | $vendorComposerAutoload->addAliasesFileToComposer(); |
92 | $vendorComposerAutoload->addVendorPrefixedAutoloadToVendorAutoload(); |
93 | } catch (Exception $e) { |
94 | $this->logger->error($e->getMessage()); |
95 | |
96 | return Command::FAILURE; |
97 | } |
98 | |
99 | return Command::SUCCESS; |
100 | } |
101 | |
102 | /** |
103 | * TODO: This should be in a shared parent class/trait. |
104 | */ |
105 | protected function createConfig(InputInterface $input): StraussConfig |
106 | { |
107 | $config = new StraussConfig(); |
108 | return $config; |
109 | } |
110 | } |