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