InstallCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of Bowerphp.
5
 *
6
 * (c) Massimiliano Arione <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Bowerphp\Command;
13
14
use Bowerphp\Installer\Installer;
15
use Bowerphp\Package\Package;
16
use Bowerphp\Repository\GithubRepository;
17
use Bowerphp\Util\PackageNameVersionExtractor;
18
use Bowerphp\Util\ZipArchive;
19
use Symfony\Component\Console\Input\InputArgument;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Input\InputOption;
22
use Symfony\Component\Console\Output\OutputInterface;
23
24
/**
25
 * Inspired by Composer https://github.com/composer/composer
26
 */
27
class InstallCommand extends Command
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected function configure()
33
    {
34
        $this
35
            ->setName('install')
36
            ->setDescription('Installs the project dependencies from the bower.json file or a single specified package')
37
            ->addOption('save', 'S', InputOption::VALUE_NONE, 'Add installed package to bower.json file.')
38
            ->addArgument('package', InputArgument::OPTIONAL, 'Choose a package.')
39
            ->setHelp(<<<'EOT'
40
The <info>%command.name%</info> command reads the bower.json file from
41
the current directory, processes it, and downloads and installs all the
42
libraries and dependencies outlined in that file.
43
44
  <info>php %command.full_name%</info>
45
46
If an optional package name is passed, that package is installed.
47
48
  <info>php %command.full_name% packageName[#version]</info>
49
50
If an optional flag <comment>-S</comment> is passed, installed package is added
51
to bower.json file (only if bower.json file already exists).
52
53
EOT
54
            )
55
        ;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    protected function execute(InputInterface $input, OutputInterface $output)
62
    {
63
        $this->setGithubToken($output);
64
        $this->config->setSaveToBowerJsonFile($input->getOption('save'));
65
66
        $packageName = $input->getArgument('package');
67
68
        $bowerphp = $this->getBowerphp($output);
69
70
        try {
71
            $installer = new Installer($this->filesystem, new ZipArchive(), $this->config);
72
73
            if (is_null($packageName)) {
74
                $bowerphp->installDependencies($installer);
75
            } else {
76
                if ('bower.json' === substr($packageName, -10)) {
77
                    if (!is_readable($packageName)) {
78
                        $output->writeln(sprintf('<error>Cannot read file %s</error>', $packageName));
79
80
                        return 1;
81
                    }
82
                    $json = json_decode($this->filesystem->read($packageName), true);
83
                    if (empty($json['dependencies'])) {
84
                        $output->writeln(sprintf('<error>Nothing to install in %s</error>', $packageName));
85
86
                        return 1;
87
                    }
88
                    foreach ($json['dependencies'] as $name => $version) {
89
                        $package = new Package($name, $version);
90
                        $bowerphp->installPackage($package, $installer);
91
                    }
92
                } else {
93
                    $packageNameVersion = PackageNameVersionExtractor::fromString($packageName);
94
                    $package = new Package($packageNameVersion->name, $packageNameVersion->version);
95
                    $bowerphp->installPackage($package, $installer);
96
                }
97
            }
98
        } catch (\RuntimeException $e) {
99
            $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
100
            if (GithubRepository::VERSION_NOT_FOUND == $e->getCode() && !empty($package)) {
101
                $output->writeln(sprintf('Available versions: %s', implode(', ', $bowerphp->getPackageInfo($package, 'versions'))));
102
            }
103
104
            return 1;
105
        }
106
107
        $output->writeln('');
108
109
        return 0;
110
    }
111
}
112