Passed
Push — main ( dd4eea...91afa7 )
by Dimitri
03:22
created

Publish::execute()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 18
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 27
rs 9.3554
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Cli\Commands\Utilities;
13
14
use BlitzPHP\Cli\Console\Command;
15
use BlitzPHP\Publisher\Publisher;
16
17
/**
18
 * Découvre toutes les classes Publisher à partir du répertoire « Publishers/ » dans les espaces de noms.
19
 * Exécute `publish()` à partir de chaque instance, en analysant chaque résultat.
20
 */
21
class Publish extends Command
22
{
23
    /**
24
     * @var string Groupe
25
     */
26
    protected $group = 'BlitzPHP';
27
    
28
    /**
29
     * @var string Nom
30
     */
31
    protected $name = 'publish';
32
33
    /**
34
     * @var string Description
35
     */
36
    protected $description = 'Découvre et exécute toutes les classes Publisher prédéfinies.';
37
38
    /**
39
     * The Command's arguments
40
     *
41
     * @var array<string, string>
42
     */
43
    protected $arguments = [
44
        '[directory:Publishers]' => '[Facultatif] Le répertoire à analyser dans chaque espace de noms. Par défaut : "Publishers".',
45
    ];
46
47
    /**
48
     * Affiche l'aide du script klinge cli lui-même.
49
     */
50
    public function execute(array $params)
51
    {
52
        $directory = array_shift($params) ?? 'Publishers';
53
54
        if ([] === $publishers = Publisher::discover($directory)) {
55
            $this->write(lang('Publisher.publishMissing', [$directory]));
56
57
            return;
58
        }
59
60
        foreach ($publishers as $publisher) {
61
            if ($publisher->publish()) {
62
                $this->ok(lang('Publisher.publishSuccess', [
63
                    get_class($publisher),
64
                    count($publisher->getPublished()),
65
                    $publisher->getDestination(),
66
                ]));
67
            } else {
68
                $this->fail(lang('Publisher.publishFailure', [
69
                    get_class($publisher),
70
                    $publisher->getDestination(),
71
                ]));
72
73
                foreach ($publisher->getErrors() as $file => $exception) {
74
                    $this->write($file);
75
                    $this->fail($exception->getMessage());
76
                    $this->newLine();
77
                }
78
            }
79
        }
80
    }
81
}
82