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