Passed
Pull Request — master (#87)
by
unknown
02:53
created

DebugBarAssetsCommand::installed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.8.0
13
 */
14
15
namespace Quantum\Console\Commands;
16
17
use Quantum\Libraries\Storage\FileSystem;
18
use Quantum\Console\QtCommand;
19
use Quantum\Di\Di;
20
21
/**
22
 * Class DebugBarAssetsCommand
23
 * @package Quantum\Console\Commands
24
 */
25
class DebugBarAssetsCommand extends QtCommand
26
{
27
28
    protected $fs;
29
30
    /**
31
     * Command name
32
     * @var string
33
     */
34
    protected $name = 'install:debugbar';
35
36
    /**
37
     * Command description
38
     * @var string
39
     */
40
    protected $description = 'Publishes debugbar assets';
41
42
    /**
43
     * Command help text
44
     * @var string
45
     */
46
    protected $help = 'The command will publish debugbar assets';
47
48
    /**
49
     * Path to public debug bar resources
50
     * @var string 
51
     */
52
    private $publicDebugbarFolderPath = 'public/assets/DebugBar/Resources';
53
54
    /**
55
     * Path to vendor debug bar resources
56
     * @var string 
57
     */
58
    private $vendorDebugbarFolderPath = 'vendor/maximebf/debugbar/src/DebugBar/Resources';
59
60
    /**
61
     * Executes the command and publishes the debug bar assets
62
     */
63
    public function exec()
64
    {
65
        $this->fs = Di::get(FileSystem::class);
66
        if ($this->fs->exists(assets_dir() . DS . 'DebugBar' . DS . 'Resources' . DS . 'debugbar.css')) {
67
            $this->error('The debuger already installed');
68
            return;
69
        }
70
71
        $this->recursive_copy($this->vendorDebugbarFolderPath, $this->publicDebugbarFolderPath);
72
73
        $this->info('Debugbar assets successfully published');
74
    }
75
76
    /**
77
     * Recursively copies the debug bar assets
78
     * @param string $src
79
     * @param string $dst
80
     * @throws \RuntimeException
81
     */
82
    private function recursive_copy(string $src, string $dst)
83
    {
84
        
85
86
        $dir = opendir($src);
87
88
        if ($dst != $this->publicDebugbarFolderPath) {
89
            if ($this->fs->makeDirectory($dst, 777, true) === false) {
90
                throw new \RuntimeException(t('exception.directory_cant_be_created', $dst));
91
            }
92
        }
93
94
        if (is_resource($dir)) {
95
            while (($file = readdir($dir))) {
96
                if (($file != '.') && ($file != '..')) {
97
                    if ($this->fs->isDirectory($src . '/' . $file)) {
98
                        $this->recursive_copy($src . '/' . $file, $dst . '/' . $file);
99
                    } else {
100
                        if ($file) {
101
                            copy($src . '/' . $file, $dst . '/' . $file);
102
                        }
103
                    }
104
                }
105
            }
106
107
            closedir($dir);
108
        }
109
    }
110
}
111