DebugBarCommand::copyResources()   B
last analyzed

Complexity

Conditions 8
Paths 11

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 12
nc 11
nop 2
dl 0
loc 22
rs 8.4444
c 0
b 0
f 0
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.9.9
13
 */
14
15
namespace Quantum\Console\Commands;
16
17
use Quantum\Libraries\Storage\Exceptions\FileSystemException;
18
use Quantum\Libraries\Storage\Factories\FileSystemFactory;
19
use Quantum\Config\Exceptions\ConfigException;
20
use Quantum\App\Exceptions\BaseException;
21
use Quantum\Libraries\Storage\FileSystem;
22
use Quantum\Di\Exceptions\DiException;
23
use Quantum\Console\QtCommand;
24
use ReflectionException;
25
26
/**
27
 * Class DebugBarAssetsCommand
28
 * @package Quantum\Console
29
 */
30
class DebugBarCommand extends QtCommand
31
{
32
    /**
33
     * File System
34
     * @var FileSystem
35
     */
36
    protected $fs;
37
38
    /**
39
     * Command name
40
     * @var string
41
     */
42
    protected $name = 'install:debugbar';
43
44
    /**
45
     * Command description
46
     * @var string
47
     */
48
    protected $description = 'Publishes debugbar assets';
49
50
    /**
51
     * Command help text
52
     * @var string
53
     */
54
    protected $help = 'The command will publish debugbar assets';
55
56
    /**
57
     * Path to public debug bar resources
58
     * @var string
59
     */
60
    private $publicDebugBarFolderPath = 'public/assets/DebugBar/Resources';
61
62
    /**
63
     * Path to vendor debug bar resources
64
     * @var string
65
     */
66
    private $vendorDebugBarFolderPath = 'vendor/php-debugbar/php-debugbar/src/DebugBar/Resources';
67
68
    /**
69
     * Executes the command and publishes the debug bar assets
70
     * @throws BaseException
71
     * @throws FileSystemException
72
     * @throws ConfigException
73
     * @throws DiException
74
     * @throws ReflectionException
75
     */
76
    public function exec()
77
    {
78
        $this->fs = FileSystemFactory::get();
79
80
        if ($this->fs->exists(assets_dir() . DS . 'DebugBar' . DS . 'Resources' . DS . 'debugbar.css')) {
81
            $this->error('The debug ber already installed');
82
            return;
83
        }
84
85
        $this->copyResources($this->vendorDebugBarFolderPath, $this->publicDebugBarFolderPath);
86
87
        $this->info('Debugbar resources successfully published');
88
    }
89
90
    /**
91
     * Recursively copies the debug bar assets
92
     * @param string $src
93
     * @param string $dst
94
     * @return void
95
     * @throws FileSystemException
96
     */
97
    private function copyResources(string $src, string $dst)
98
    {
99
        $dir = opendir($src);
100
101
        if ($dst != $this->publicDebugBarFolderPath) {
102
            if ($this->fs->makeDirectory($dst) === false) {
103
                throw FileSystemException::directoryNotWritable($dst);
104
            }
105
        }
106
107
        if (is_resource($dir)) {
108
            while (($file = readdir($dir))) {
109
                if (($file != '.') && ($file != '..')) {
110
                    if ($this->fs->isDirectory($src . DS . $file)) {
111
                        $this->copyResources($src . DS . $file, $dst . DS . $file);
112
                    } else {
113
                        copy($src . DS . $file, $dst . DS . $file);
114
                    }
115
                }
116
            }
117
118
            closedir($dir);
119
        }
120
    }
121
}