Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
14.29% covered (danger)
14.29%
3 / 21
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
IncludeAutoloaderCommand
14.29% covered (danger)
14.29%
3 / 21
25.00% covered (danger)
25.00%
1 / 4
20.74
0.00% covered (danger)
0.00%
0 / 1
 configure
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 execute
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
6
 loadProjectComposerPackage
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 loadConfigFromComposerJson
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
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
16namespace BrianHenryIE\Strauss\Console\Commands;
17
18use BrianHenryIE\Strauss\Composer\ProjectComposerPackage;
19use BrianHenryIE\Strauss\Pipeline\Autoload\VendorComposerAutoload;
20use Composer\Factory;
21use Exception;
22use Psr\Log\LoggerAwareTrait;
23use Symfony\Component\Console\Command\Command;
24use Symfony\Component\Console\Input\InputInterface;
25use Symfony\Component\Console\Output\OutputInterface;
26
27class IncludeAutoloaderCommand extends AbstractRenamespacerCommand
28{
29    use LoggerAwareTrait;
30
31    /**
32     * Set name and description, add CLI arguments, call parent class to add dry-run, verbosity options.
33     *
34     * @used-by \Symfony\Component\Console\Command\Command::__construct
35     * @override {@see \Symfony\Component\Console\Command\Command::configure()} empty method.
36     *
37     * @return void
38     */
39    protected function configure()
40    {
41        $this->setName('include-autoloader');
42        $this->setDescription("Adds `require autoload_aliases.php` and `require vendor-prefixed/autoload.php` to `vendor/autoload.php`.");
43
44        parent::configure();
45    }
46
47    /**
48     * @param InputInterface $input
49     * @param OutputInterface $output
50     *
51     * @see Command::execute()
52     *
53     */
54    protected function execute(InputInterface $input, OutputInterface $output): int
55    {
56        try {
57            // Pipeline
58            $this->loadProjectComposerPackage();
59            $this->loadConfigFromComposerJson();
60
61            parent::execute($input, $output);
62
63            // TODO: check for `--no-dev` somewhere.
64
65            $vendorComposerAutoload = new VendorComposerAutoload(
66                $this->config,
67                $this->filesystem,
68                $this->logger
69            );
70
71            $vendorComposerAutoload->addAliasesFileToComposer();
72            $vendorComposerAutoload->addVendorPrefixedAutoloadToVendorAutoload();
73        } catch (Exception $e) {
74            $this->logger->error($e->getMessage());
75
76            return Command::FAILURE;
77        }
78
79        return Command::SUCCESS;
80    }
81
82
83    /**
84     * 1. Load the composer.json.
85     *
86     * @throws Exception
87     */
88    protected function loadProjectComposerPackage(): void
89    {
90        $this->logger->notice('Loading package...');
91
92        $this->projectComposerPackage = new ProjectComposerPackage($this->workingDir . Factory::getComposerFile());
93    }
94
95    protected function loadConfigFromComposerJson(): void
96    {
97        $this->logger->notice('Loading composer.json config...');
98
99        $this->config = $this->projectComposerPackage->getStraussConfig();
100    }
101}