-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathBaseCommand.php
More file actions
111 lines (97 loc) · 3.06 KB
/
Copy pathBaseCommand.php
File metadata and controls
111 lines (97 loc) · 3.06 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
<?php
namespace modmore\Gitify;
use modX;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Class Command
*
* @package modmore\Gitify\Command
*/
abstract class BaseCommand extends Command
{
/** @var modX $modx */
public $modx;
/** @var array $config Contains the contents of the .gitify file */
public $config = array();
/** \Symfony\Component\Console\Input\InputInterface $input */
public $input;
/** \Symfony\Component\Console\Output\OutputInterface $output */
public $output;
public $startTime;
public $loadConfig = true;
public $loadMODX = true;
/**
* Initializes the command just after the input has been validated.
*
* This is mainly useful when a lot of commands extends one main command
* where some things need to be initialized based on the input arguments and options.
*
* @param InputInterface $input An InputInterface instance
* @param OutputInterface $output An OutputInterface instance
*/
public function initialize(InputInterface $input, OutputInterface $output)
{
$this->startTime = microtime(true);
$this->input = $input;
$this->output = $output;
if ($this->loadConfig)
{
$this->config = Gitify::loadConfig();
}
if ($this->loadMODX)
{
$this->modx = Gitify::loadMODX();
}
}
/**
* @return string
*/
public function getRunStats()
{
$curTime = microtime(true);
$duration = $curTime - $this->startTime;
$output = 'Time: ' . number_format($duration * 1000, 0) . 'ms | ';
$output .= 'Memory Usage: ' . $this->convertBytes(memory_get_usage(false)) . ' | ';
$output .= 'Peak Memory Usage: ' . $this->convertBytes(memory_get_peak_usage(false));
return $output;
}
/**
* @param $bytes
* @return string
*/
public function convertBytes($bytes)
{
$unit = array('b','kb','mb','gb','tb','pb');
return @round($bytes/pow(1024,($i=floor(log($bytes,1024)))),2).' '.$unit[$i];
}
/**
* @param $path
* @return string
*/
public function normalizePath($path)
{
$normalized_path = str_replace('\\', Gitify::$directorySeparator, $path);
// especially for windows, convert utf8 to local encoding to fix umlauts in filenames
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$normalized_path = utf8_decode($normalized_path);
}
return $normalized_path;
}
/**
* @param string $partition
* @return array|null
*/
public function getPartitionCriteria($partition)
{
if (!isset($this->config['data']) || !isset($this->config['data'][$partition])) {
return null;
}
$options = $this->config['data'][$partition];
if (isset($options['where']) && !empty($options['where'])) {
return $options['where'];
}
return null;
}
}