1
|
|
|
<?php |
2
|
|
|
namespace PressCLI\Option; |
3
|
|
|
|
4
|
|
|
use PressCLI\Option\DefaultConfiguration; |
5
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
6
|
|
|
|
7
|
|
|
class GlobalConfiguration |
8
|
|
|
{ |
9
|
|
|
use DefaultConfiguration; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Creates the configuration file. |
13
|
|
|
* |
14
|
|
|
* @param OutputInterface $output |
15
|
|
|
*/ |
16
|
|
|
public static function create(OutputInterface $output) |
17
|
|
|
{ |
18
|
|
|
$output->writeln("<info>Creating global configuration...</info>"); |
19
|
|
|
$configFile = self::getConfigFile(); |
20
|
|
|
|
21
|
|
|
if (self::exists()) { |
22
|
|
|
$output->writeln("<comment>Global configuration already exists at {$configFile}.</comment>"); |
23
|
|
|
|
24
|
|
|
return; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
// Add configuration from skeleton. |
28
|
|
|
$config = json_encode(self::configSkeleton(), JSON_PRETTY_PRINT); |
29
|
|
|
$config = stripslashes($config); |
30
|
|
|
file_put_contents($configFile, $config); |
31
|
|
|
|
32
|
|
|
$output->writeln("<info>Global configuration created at {$configFile}!</info>"); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get the configuration. |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
View Code Duplication |
public static function get() |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
if (!self::exists()) { |
43
|
|
|
return []; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$config = json_decode(file_get_contents(self::getConfigFile()), true); |
47
|
|
|
|
48
|
|
|
return $config; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Configuration skeleton |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
protected static function configSkeleton() |
57
|
|
|
{ |
58
|
|
|
$config = self::getDefaultConfiguration(); |
59
|
|
|
|
60
|
|
|
return $config; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Checks if the configuration exists. |
65
|
|
|
* |
66
|
|
|
* @return boolean |
67
|
|
|
*/ |
68
|
|
|
public static function exists() |
69
|
|
|
{ |
70
|
|
|
return file_exists(self::getConfigFile()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Retrieves the configuration file. |
75
|
|
|
* |
76
|
|
|
* @return string The path to the configuration file. |
77
|
|
|
*/ |
78
|
|
|
protected static function getConfigFile() |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
return "{$_SERVER['HOME']}/.press-cli.json"; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.