Completed
Pull Request — master (#46)
by Jitendra
02:19
created

BaseCommand::getTemplatePaths()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 16
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the PHINT package.
5
 *
6
 * (c) Jitendra Adhikari <[email protected]>
7
 *     <https://github.com/adhocore>
8
 *
9
 * Licensed under MIT license.
10
 */
11
12
namespace Ahc\Phint\Console;
13
14
use Ahc\Cli\Input\Command;
15
use Ahc\Cli\IO\Interactor;
16
use Ahc\Phint\Util\Composer;
17
use Ahc\Phint\Util\Git;
18
use Ahc\Phint\Util\Path;
19
20
abstract class BaseCommand extends Command
21
{
22
    /** @var string Full path of log file */
23
    protected $_logFile;
24
25
    /** @var string Current working dir */
26
    protected $_workDir;
27
28
    public function __construct()
29
    {
30
        $logFile = @\tempnam(\sys_get_temp_dir(), 'PHT') ?: '';
31
32
        $this->_logFile  = $logFile;
33
        $this->_pathUtil = new Path;
0 ignored issues
show
Bug Best Practice introduced by
The property _pathUtil does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
34
        $this->_workDir  = \realpath(\getcwd());
35
        $this->_git      = new Git(null, $logFile);
0 ignored issues
show
Bug Best Practice introduced by
The property _git does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
36
        $this->_composer = new Composer(null, $logFile);
0 ignored issues
show
Bug Best Practice introduced by
The property _composer does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
37
38
        $this->defaults();
39
        $this->onConstruct();
40
    }
41
42
    protected function onConstruct()
43
    {
44
        // ;)
45
    }
46
47
    protected function promptAll(Interactor $io, array $promptConfig)
48
    {
49
        $template = ['default' => null, 'choices' => [], 'retry' => 1, 'extra' => '', 'restore' => false];
50
51
        foreach ($this->missingOptions($promptConfig) as $name => $option) {
52
            $config  = ($promptConfig[$name] ?? []) + $template;
53
            $default = ($config['default'] ?? $option->default()) ?: null;
54
55
            if ($config['choices']) {
56
                $value = $io->choice($option->desc(), $config['choices'], $default);
57
            } else {
58
                $value = $io->prompt($option->desc() . $config['extra'], $default, null, $config['retry']);
59
            }
60
61
            if ($config['restore']) {
62
                $value = $config['choices'][$value] ?? $value;
63
            }
64
65
            $this->set($name, $value);
66
        }
67
    }
68
69
    protected function missingOptions(array $config)
70
    {
71
        return \array_filter($this->userOptions(), function ($name) use ($config) {
72
            return false !== ($config[$name] ?? null) && \in_array($this->$name, [null, []], true);
73
        }, \ARRAY_FILTER_USE_KEY);
74
    }
75
76
    protected function getCachePath(): string
77
    {
78
        if (!\Phar::running(false)) {
79
            return __DIR__ . '/../../.cache';
80
        }
81
82
        return $this->_pathUtil->getPhintPath('.cache');
83
    }
84
85
    protected function logging(string $mode = 'start')
86
    {
87
        if (!$logFile = $this->_logFile) {
88
            return;
89
        }
90
91
        if ('end' === $mode) {
92
            $this->app()->io()->comment("Check detailed logs at $logFile", true);
93
        } else {
94
            $this->app()->io()->comment("Logging to $logFile", true);
95
        }
96
    }
97
98
    protected function getTemplatePaths(array $parameters): array
99
    {
100
        // Phint provided path.
101
        $templatePaths = [\realpath(__DIR__ . '/../../resources')];
102
        $userPath      = $parameters['template'] ?? null;
103
104
        if (empty($userPath)) {
105
            return $templatePaths;
106
        }
107
108
        $userPath = $this->_pathUtil->expand($userPath, $this->_workDir);
109
110
        // User supplied path comes first.
111
        \array_unshift($templatePaths, $userPath);
112
113
        return $templatePaths;
114
    }
115
}
116