Completed
Push — master ( c45fe6...da9ea2 )
by Rougin
06:15
created

CreateLayoutCommand::isEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Rougin\Combustor\Commands;
4
5
use Symfony\Component\Console\Input\InputOption;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
use Rougin\Combustor\Common\Tools;
11
12
/**
13
 * Create Layout Command
14
 *
15
 * Creates a new header and footer file for CodeIgniter
16
 * 
17
 * @package Combustor
18
 * @author  Rougin Royce Gutib <[email protected]>
19
 */
20
class CreateLayoutCommand extends AbstractCommand
21
{
22
    /**
23
     * Checks whether the command is enabled or not in the current environment.
24
     *
25
     * Override this to check for x or y and return false if the command can not
26
     * run properly under the current conditions.
27
     *
28
     * @return bool
29
     */
30
    public function isEnabled()
31
    {
32
        return ! Tools::hasLayout();
33
    }
34
35
    /**
36
     * Sets the configurations of the specified command.
37
     *
38
     * @return void
39
     */
40
    protected function configure()
41
    {
42
        $this->setName('create:layout')
43
            ->setDescription('Creates a new header and footer file')
44
            ->addOption(
45
                'bootstrap',
46
                NULL,
47
                InputOption::VALUE_NONE,
48
                'Includes the Bootstrap CSS/JS Framework tags'
49
            );
50
    }
51
52
    /**
53
     * Executes the command.
54
     * 
55
     * @param \Symfony\Component\Console\Input\InputInterface   $input
56
     * @param \Symfony\Component\Console\Output\OutputInterface $output
57
     * @return object|\Symfony\Component\Console\Output\OutputInterface
58
     */
59
    protected function execute(InputInterface $input, OutputInterface $output)
60
    {
61
        $layoutPath = APPPATH . 'views/layout';
62
63
        $data = [];
64
65
        $data['bootstrapContainer'] = '';
66
        $data['scripts'] = [];
67
        $data['styleSheets'] = [];
68
69
        $css = '//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css';
70
71
        if ( ! is_dir('bower_components/font-awesome') && system('bower install font-awesome')) {
72
            $css = '<?php echo base_url(\'bower_components/font-awesome/css/font-awesome.min.css\'); ?>';
73
        }
74
75
        $data['styleSheets'][0] = $css;
76
77
        if ($input->getOption('bootstrap')) {
78
            $data['bootstrapContainer'] = 'container';
79
80
            $css = 'https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css';
81
            $js = 'https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.js';
82
            $jquery = 'https://code.jquery.com/jquery-2.1.1.min.js';
83
84
            if ( ! is_dir('bower_components/bootstrap') && system('bower install bootstrap')) {
85
                $css = '<?php echo base_url(\'bower_components/bootstrap/dist/css/bootstrap.min.css\'); ?>';
86
                $js = '<?php echo base_url(\'bower_components/bootstrap/dist/js/bootstrap.min.js\'); ?>';
87
                $jquery = '<?php echo base_url(\'bower_components/jquery/dist/jquery.min.js\'); ?>';
88
            }
89
 
90
            array_push($data['styleSheets'], $css);
91
            array_push($data['scripts'], $jquery);
92
            array_push($data['scripts'], $js);
93
        }
94
95
        if ( ! @mkdir($layoutPath, 0777, true)) {
96
            $message = 'The layout directory already exists!';
97
98
            return $output->writeln('<error>' . $message . '</error>');
99
        }
100
101
        $header = $this->renderer->render('Views/Layout/header.template', $data);
102
        $footer = $this->renderer->render('Views/Layout/footer.template', $data);
103
104
        $headerFile = fopen($layoutPath . '/header.php', 'wb');
105
        $footerFile = fopen($layoutPath . '/footer.php', 'wb');
106
107
        file_put_contents($layoutPath . '/header.php', $header);
108
        file_put_contents($layoutPath . '/footer.php', $footer);
109
110
        fclose($headerFile);
111
        fclose($footerFile);
112
113
        $message = 'The layout folder has been created successfully!';
114
115
        return $output->writeln('<info>'.$message.'</info>');
116
    }
117
}
118