-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathGitify.php
More file actions
175 lines (150 loc) · 5.04 KB
/
Copy pathGitify.php
File metadata and controls
175 lines (150 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace modmore\Gitify;
use Kbjr\Git\Git;
use Kbjr\Git\GitRepo;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Yaml\Yaml;
/**
* Class Gitify
*/
class Gitify extends Application
{
/**
* Used to separate between meta data and content in object files.
*
* @var string
*/
public static $contentSeparator = "\n-----\n\n";
/**
* Universal directory separator for *Nix and Windows
*
* @var string
*/
public static $directorySeparator = "/";
/**
* @var \modX
*/
public static $modx;
public $environment = array();
public $repository;
/**
* Takes in an array of data, and turns it into blissful YAML using Symfony's YAML component.
*
* @param array $data
* @return string
*/
public static function toYAML(array $data = array())
{
return Yaml::dump($data, 4);
}
/**
* Takes YAML, and turns it into an array using Symfony's YAML component.
*
* @param string $input
* @return array
*/
public static function fromYAML($input = '')
{
return Yaml::parse($input);
}
/**
* Loads a new modX instance
*
* @throws \RuntimeException
* @return \modX
*/
public static function loadMODX()
{
if (self::$modx) {
return self::$modx;
}
if (!file_exists(GITIFY_WORKING_DIR . 'config.core.php')) {
throw new \RuntimeException('There does not seem to be a MODX installation here. ');
}
require_once(GITIFY_WORKING_DIR . 'config.core.php');
require_once(MODX_CORE_PATH . 'model/modx/modx.class.php');
$modx = new \modX();
$modx->initialize('mgr');
$modx->getService('error', 'error.modError', '', '');
$modx->setLogTarget('ECHO');
self::$modx = $modx;
return $modx;
}
/**
* @throws \RuntimeException
*/
public static function loadConfig($file = null)
{
if (null === $file) {
$file = '.gitify';
}
if (!file_exists(GITIFY_WORKING_DIR . $file)) {
throw new \RuntimeException("Directory is not a Gitify directory: " . GITIFY_WORKING_DIR);
}
$config = Gitify::fromYAML(file_get_contents(GITIFY_WORKING_DIR . $file));
if (!$config || !is_array($config)) {
throw new \RuntimeException("Error: " . GITIFY_WORKING_DIR . "{$file} file is not valid YAML, or is empty.");
}
return $config;
}
/**
* Returns the current environment based on the HTTP HOST.
*
* @return array
*/
public function getEnvironment ()
{
if (!empty($this->environment)) {
return $this->environment;
}
$config = static::loadConfig();
$envs = array();
if (isset($config['environments']) && is_array($config['environments'])) {
$envs = $config['environments'];
}
$defaults = array(
'name' => '-unidentified environment-',
'branch' => 'develop',
'auto_commit_and_push' => true,
'remote' => 'origin',
'partitions' => array(
'modResource' => 'content',
'modTemplate' => 'templates',
'modCategory' => 'categories',
'modTemplateVar' => 'template_variables',
'modChunk' => 'chunks',
'modSnippet' => 'snippets',
'modPlugin' => 'plugins'
)
);
if (isset($envs['defaults']) && is_array($envs['defaults'])) {
$defaults = array_merge($defaults, $envs['defaults']);
}
$host = (isset($_SERVER['HTTP_HOST'])) ? $_SERVER['HTTP_HOST'] : MODX_HTTP_HOST;
if (substr($host, 0, 4) == 'www.') {
$host = substr($host, 4);
}
$environment = (isset($envs[$host])) ? $envs[$host] : array();
$environment = array_merge($defaults, $environment);
$this->environment = $environment;
return $environment;
}
/**
* Gets the default input definition.
*
* @return InputDefinition An InputDefinition instance
*/
protected function getDefaultInputDefinition(): InputDefinition
{
return new InputDefinition(array(
new InputArgument('command', InputArgument::REQUIRED, 'The command to execute'),
new InputOption('--help', '-h', InputOption::VALUE_NONE, 'Display this help message.'),
new InputOption('--verbose', '-v|vv|vvv', InputOption::VALUE_NONE, 'Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug.'),
new InputOption('--version', '-V', InputOption::VALUE_NONE, 'Display the Gitify version.'),
new InputOption('--dotfile', null, InputOption::VALUE_REQUIRED, 'Gitify YAML file to use.', '.gitify'),
));
}
}