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