Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
20.00% |
6 / 30 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
IncludeAutoloaderCommand | |
20.00% |
6 / 30 |
|
25.00% |
1 / 4 |
17.80 | |
0.00% |
0 / 1 |
configure | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
6 | |||
loadProjectComposerPackage | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
loadConfigFromComposerJson | |
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\Composer\ProjectComposerPackage; |
22 | use BrianHenryIE\Strauss\Helpers\FileSystem; |
23 | use BrianHenryIE\Strauss\Pipeline\Autoload\VendorComposerAutoload; |
24 | use BrianHenryIE\Strauss\Pipeline\Prefixer; |
25 | use Composer\Factory; |
26 | use Exception; |
27 | use League\Flysystem\Local\LocalFilesystemAdapter; |
28 | use Psr\Log\LoggerAwareTrait; |
29 | use Psr\Log\LogLevel; |
30 | use Symfony\Component\Console\Command\Command; |
31 | use Symfony\Component\Console\Input\InputInterface; |
32 | use Symfony\Component\Console\Logger\ConsoleLogger; |
33 | use Symfony\Component\Console\Output\OutputInterface; |
34 | |
35 | class IncludeAutoloaderCommand extends Command |
36 | { |
37 | use LoggerAwareTrait; |
38 | |
39 | /** @var string */ |
40 | protected string $workingDir; |
41 | |
42 | protected StraussConfig $config; |
43 | |
44 | protected Filesystem $filesystem; |
45 | protected ProjectComposerPackage $projectComposerPackage; |
46 | |
47 | /** |
48 | * @return void |
49 | */ |
50 | protected function configure() |
51 | { |
52 | $this->setName('include-autoloader'); |
53 | $this->setDescription("Adds `require autoload_aliases.php` and `require vendor-prefixed/autoload.php` to `vendor/autoload.php`."); |
54 | |
55 | // TODO: permissions? |
56 | $this->filesystem = new Filesystem( |
57 | new \League\Flysystem\Filesystem(new LocalFilesystemAdapter('/')), |
58 | getcwd() . '/' |
59 | ); |
60 | } |
61 | |
62 | /** |
63 | * @param InputInterface $input |
64 | * @param OutputInterface $output |
65 | * |
66 | * @see Command::execute() |
67 | * |
68 | */ |
69 | protected function execute(InputInterface $input, OutputInterface $output): int |
70 | { |
71 | $logger = new ConsoleLogger( |
72 | $output, |
73 | [ LogLevel::INFO => OutputInterface::VERBOSITY_NORMAL ] |
74 | ); |
75 | |
76 | $this->setLogger($logger); |
77 | |
78 | $workingDir = getcwd() . '/'; |
79 | $this->workingDir = $workingDir; |
80 | |
81 | try { |
82 | $this->loadProjectComposerPackage(); |
83 | $this->loadConfigFromComposerJson(); |
84 | |
85 | // Pipeline |
86 | |
87 | // TODO: check for `--no-dev` somewhere. |
88 | |
89 | $vendorComposerAutoload = new VendorComposerAutoload( |
90 | $this->config, |
91 | $this->filesystem, |
92 | $logger |
93 | ); |
94 | |
95 | $vendorComposerAutoload->addAliasesFileToComposer(); |
96 | $vendorComposerAutoload->addVendorPrefixedAutoloadToVendorAutoload(); |
97 | } catch (Exception $e) { |
98 | $this->logger->error($e->getMessage()); |
99 | |
100 | return Command::FAILURE; |
101 | } |
102 | |
103 | return Command::SUCCESS; |
104 | } |
105 | |
106 | /** |
107 | * 1. Load the composer.json. |
108 | * |
109 | * @throws Exception |
110 | */ |
111 | protected function loadProjectComposerPackage(): void |
112 | { |
113 | $this->logger->notice('Loading package...'); |
114 | |
115 | $this->projectComposerPackage = new ProjectComposerPackage($this->workingDir . Factory::getComposerFile()); |
116 | } |
117 | |
118 | protected function loadConfigFromComposerJson(): void |
119 | { |
120 | $this->logger->notice('Loading composer.json config...'); |
121 | |
122 | $this->config = $this->projectComposerPackage->getStraussConfig(); |
123 | } |
124 | } |