Completed
Pull Request — develop (#406)
by
unknown
06:09
created

GenerateCommand   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 13
c 4
b 0
f 1
lcom 1
cbo 7
dl 0
loc 123
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 7 1
B execute() 0 31 6
B executeGenerateBootstrap() 0 40 3
A _getCopyrightHeader() 0 15 2
1
<?php
2
3
/**
4
 * This file is part of BraincraftedBootstrapBundle.
5
 *
6
 * (c) 2012-2013 by Florian Eckerstorfer
7
 */
8
9
namespace Braincrafted\Bundle\BootstrapBundle\Command;
10
11
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
use Braincrafted\Bundle\BootstrapBundle\Util\PathUtil;
16
use Symfony\Component\Filesystem\Filesystem;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpKernel\Kernel;
19
20
/**
21
 * GenerateCommand
22
 *
23
 * @package    BraincraftedBootstrapBundle
24
 * @subpackage Command
25
 * @author     Florian Eckerstorfer <[email protected]>
26
 * @copyright  2012-2013 Florian Eckerstorfer
27
 * @license    http://opensource.org/licenses/MIT The MIT License
28
 * @link       http://bootstrap.braincrafted.com BraincraftedBootstrapBundle
29
 */
30
class GenerateCommand extends ContainerAwareCommand
31
{
32
    const TWBS_BOOTSTRAP_LESS      =   '/../vendor/twbs/bootstrap/less/bootstrap.less';
33
    const TWBS_COPYRIGHT_HEADER    =   '/../vendor/twbs/bootstrap/dist/css/bootstrap.css';
34
35
    /** @var PathUtil */
36
    private $pathUtil;
37
38
    /**
39
     * {@inheritDoc}
40
     */
41
    public function __construct($name = null)
42
    {
43
        $this->pathUtil = new PathUtil;
44
45
        parent::__construct($name);
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     *
51
     * @codeCoverageIgnore
52
     */
53
    protected function configure()
54
    {
55
        $this
56
            ->setName('braincrafted:bootstrap:generate')
57
            ->setDescription('Generates a custom bootstrap.less')
58
        ;
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64
    protected function execute(InputInterface $input, OutputInterface $output)
65
    {
66
        $config = $this->getContainer()->getParameter('braincrafted_bootstrap.customize');
67
68
        if (false === isset($config['variables_file']) || null === $config['variables_file']) {
69
            $output->writeln('<error>Found no custom variables.less file.</error>');
70
71
            return;
72
        }
73
74
        $filter = $this->getContainer()->getParameter('braincrafted_bootstrap.less_filter');
75
        if ('less' !== $filter && 'lessphp' !== $filter) {
76
            $output->writeln(
77
                '<error>Bundle must be configured with "less" or "lessphp" to generated bootstrap.less</error>'
78
            );
79
80
            return;
81
        }
82
83
        if (false === (new Filesystem())->exists($this->getContainer()->get('kernel')->getRootDir() . self::TWBS_BOOTSTRAP_LESS)) {
84
            $output->writeln(
85
                '<error>Required budle "twbs/bootstrap" not found in composer.json, please run "composer require twbs/bootstrap && composer update"</error>'
86
            );
87
88
            return;
89
        }
90
91
        $output->writeln('<comment>Found custom variables file. Generating...</comment>');
92
        $this->executeGenerateBootstrap($config, $output);
93
        $output->writeln(sprintf('Saved to <info>%s</info>', $config['bootstrap_output']));
94
    }
95
96
    protected function executeGenerateBootstrap(array $config, OutputInterface $output)
97
    {
98
        // In the template for bootstrap.less we need the path where Bootstraps .less files are stored and the path
99
        // to the variables.less file.
100
        // Absolute path do not work in LESSs import statement, we have to calculate the relative ones
101
102
        $lessDir = $this->pathUtil->getRelativePath(
103
                dirname($config['bootstrap_output']),
104
                $this->getContainer()->getParameter('braincrafted_bootstrap.assets_dir')
105
            ) . 'less/';
106
107
        $variablesDir = $this->pathUtil->getRelativePath(
108
            dirname($config['bootstrap_output']),
109
            dirname($config['variables_file'])
110
        );
111
        $variablesFile = sprintf(
112
            '%s%s%s',
113
            $variablesDir,
114
            strlen($variablesDir) > 0 ? '/' : '',
115
            basename($config['variables_file'])
116
        );
117
118
        $container = $this->getContainer();
119
120
        if (Kernel::VERSION_ID >= 20500) {
121
            $container->enterScope('request');
122
            $container->set('request', new Request(), 'request');
123
        }
124
125
        // Load the content from the original twbs bootstrap.less file
126
        // and make some modifications and save it to our custom
127
        // bootstrap less file.
128
129
        $content = $this->_getCopyrightHeader($output) . "\xA";
130
        $content .= file_get_contents($this->getContainer()->get('kernel')->getRootDir() . self::TWBS_BOOTSTRAP_LESS);
131
        $content = str_replace('@import "', '@import "' . $lessDir, $content);
132
        $content = str_replace('variables.less";', 'variables.less";' . "\xA" . '@import "' . $variablesFile . '";', $content);
133
134
        file_put_contents($config['bootstrap_output'], $content);
135
    }
136
137
    private function _getCopyrightHeader(OutputInterface $output) {
0 ignored issues
show
Coding Style introduced by
function _getCopyrightHeader() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style Naming introduced by
The method _getCopyrightHeader is not named in camelCase.

This check marks method names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
138
        $copyrightFile = $this->getContainer()->get('kernel')->getRootDir() . self::TWBS_COPYRIGHT_HEADER;
139
        $content = file_get_contents($copyrightFile);
140
        preg_match_all('/^\/\*!.+copyright.+\*\/$/imsU', $content, $matches);
141
142
        if (!isset($matches[0][0])) {
143
            $output->writeln(
144
                '<comment>Unable to fetch copyright header from ' . $copyrightFile . ', please add it manually.</comment>'
145
            );
146
147
            return;
148
        }
149
150
        return $matches[0][0];
151
    }
152
}
153