diff --git a/.gitignore b/.gitignore
index 28741bf..b07b45a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,6 @@
/vendor
/composer.phar
/.idea
+/build
+/logs
+/phpcs.xml
diff --git a/build.xml b/build.xml
new file mode 100644
index 0000000..237a660
--- /dev/null
+++ b/build.xml
@@ -0,0 +1,56 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/composer.lock b/composer.lock
index 425f0d1..252a655 100644
--- a/composer.lock
+++ b/composer.lock
@@ -135,16 +135,16 @@
},
{
"name": "ofbeaton/granite",
- "version": "0.3.0",
+ "version": "0.3.1",
"source": {
"type": "git",
"url": "https://github.com/ofbeaton/granite.git",
- "reference": "c051267b5017391296784da8e25b4cae7162296a"
+ "reference": "19d2cfc3712da0f1d91f5360bea0126e7d2c0e40"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/ofbeaton/granite/zipball/c051267b5017391296784da8e25b4cae7162296a",
- "reference": "c051267b5017391296784da8e25b4cae7162296a",
+ "url": "https://api.github.com/repos/ofbeaton/granite/zipball/19d2cfc3712da0f1d91f5360bea0126e7d2c0e40",
+ "reference": "19d2cfc3712da0f1d91f5360bea0126e7d2c0e40",
"shasum": ""
},
"require": {
@@ -178,7 +178,7 @@
"PHP_CodeSniffer",
"code sniffer"
],
- "time": "2015-07-28 22:45:10"
+ "time": "2015-08-07 01:57:45"
},
{
"name": "phing/phing",
@@ -431,16 +431,16 @@
},
{
"name": "symfony/console",
- "version": "v2.7.2",
+ "version": "v2.7.3",
"source": {
"type": "git",
"url": "https://github.com/symfony/Console.git",
- "reference": "8cf484449130cabfd98dcb4694ca9945802a21ed"
+ "reference": "d6cf02fe73634c96677e428f840704bfbcaec29e"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/symfony/Console/zipball/8cf484449130cabfd98dcb4694ca9945802a21ed",
- "reference": "8cf484449130cabfd98dcb4694ca9945802a21ed",
+ "url": "https://api.github.com/repos/symfony/Console/zipball/d6cf02fe73634c96677e428f840704bfbcaec29e",
+ "reference": "d6cf02fe73634c96677e428f840704bfbcaec29e",
"shasum": ""
},
"require": {
@@ -484,7 +484,7 @@
],
"description": "Symfony Console Component",
"homepage": "https://symfony.com",
- "time": "2015-07-09 16:07:40"
+ "time": "2015-07-28 15:18:12"
}
],
"aliases": [],
diff --git a/phpcs.xml.dist b/phpcs.xml.dist
new file mode 100644
index 0000000..5cdd72d
--- /dev/null
+++ b/phpcs.xml.dist
@@ -0,0 +1,11 @@
+
+
+ The default command line arguments
+
+ src
+ tests
+
+
+
+
+
diff --git a/src/Running.php b/src/Running.php
index 39f6d78..89439a7 100644
--- a/src/Running.php
+++ b/src/Running.php
@@ -2,26 +2,57 @@
namespace Ofbeaton\Command;
+/**
+ * Class Running
+ * @since 2015-07-30
+ */
class Running
{
-
+ /**
+ * @since 2015-07-30
+ */
const OS_LINUX = 'linux';
+ /**
+ * @since 2015-07-30
+ */
const OS_WINDOWS = 'windows';
+ /**
+ * @var array
+ * @since 2015-07-30
+ */
protected $supportedOs = [
- self::OS_LINUX,
- self::OS_WINDOWS,
+ self::OS_LINUX,
+ self::OS_WINDOWS,
];
+ /**
+ * @var null
+ * @since 2015-07-30
+ */
protected $os = null;
+ /**
+ * @var null
+ * @since 2015-07-30
+ */
protected $osRaw = null;
+ /**
+ * @var null
+ * @since 2015-07-30
+ */
protected $pidFile = null;
+ /**
+ * @param string $pidFile Filename and path to PID file.
+ * @param string $unknownOs OS to use if detected an unknown OS.
+ *
+ * @since 2015-07-30
+ */
public function __construct($pidFile = null, $unknownOs = self::OS_LINUX)
{
$this->detectOs($unknownOs);
@@ -29,12 +60,22 @@ public function __construct($pidFile = null, $unknownOs = self::OS_LINUX)
}//end __construct()
+ /**
+ * @return string OS
+ *
+ * @since 2015-07-30
+ */
public function getOs()
{
return $this->os;
}//end getOs()
+ /**
+ * @return boolean
+ *
+ * @since 2015-07-30
+ */
public function isWindows()
{
if ($this->os === self::OS_WINDOWS) {
@@ -45,6 +86,11 @@ public function isWindows()
}//end isWindows()
+ /**
+ * @return boolean
+ *
+ * @since 2015-07-30
+ */
public function isLinux()
{
if ($this->os === self::OS_LINUX) {
@@ -55,25 +101,34 @@ public function isLinux()
}//end isLinux()
+ /**
+ * @param string|null $unknownOs OS to use if OS is not supported.
+ *
+ * @return void
+ * @throws \InvalidArgumentException OS is not supported and no unknown OS specified.
+ *
+ * @since 2015-07-30
+ */
protected function detectOs($unknownOs = self::OS_LINUX)
{
- /*
- http://stackoverflow.com/questions/738823/possible-values-for-php-os
- some possible values:
- CYGWIN_NT-5.1
- Darwin
- FreeBSD
- HP-UX
- IRIX64
- Linux
- NetBSD
- OpenBSD
- SunOS
- Unix
- WIN32
- WINNT
- Windows
- */
+ /*
+ Http://stackoverflow.com/questions/738823/possible-values-for-php-os
+ some possible values:
+ CYGWIN_NT-5.1
+ Darwin
+ FreeBSD
+ HP-UX
+ IRIX64
+ Linux
+ NetBSD
+ OpenBSD
+ SunOS
+ Unix
+ WIN32
+ WINNT
+ Windows
+ */
+
$this->osRaw = php_uname('s');
if ($this->osRaw === 'Linux') {
@@ -83,18 +138,27 @@ protected function detectOs($unknownOs = self::OS_LINUX)
} elseif ($unknownOs !== null && in_array($unknownOs, $this->supportedOs) === true) {
$this->os = $unknownOs;
} else {
- throw new \InvalidArgumentException('unknownOs `'.$unknownOs.'` is not in list of supportedOs `'.implode(', ', $this->supportedOs));
+ $error = 'unknownOs `'.$unknownOs.'` is not in list of supportedOs `'.implode(', ', $this->supportedOs);
+ throw new \InvalidArgumentException($error);
}
}//end detectOs()
+ /**
+ * @param array $filters Filters.
+ *
+ * @return array
+ * @throws \InvalidArgumentException Filter value invalid.
+ *
+ * @since 2015-07-30
+ */
protected function transformFilters(array $filters)
{
foreach ($filters as $key => $value) {
if (is_string($value) === true) {
$filters[$key] = new RunningFilter();
$filters[$key]->setProcess($value);
- } elseif (is_array($value) === true && count($array) === 2) {
+ } elseif (is_array($value) === true && count($value) === 2) {
$filters[$key] = new RunningFilter();
$filters[$key]->setProcess($value[0]);
$filters[$key]->setOs($value[1]);
@@ -112,15 +176,19 @@ protected function transformFilters(array $filters)
* @param boolean $ignoreCase Ignores case in filters.
*
* @return array of pids matching the filters.
+ * @throws \RuntimeException OS not supported.
+ * @throws \RuntimeException Could not retrieve PID list.
+ *
+ * @since 2015-07-30
*/
public function getPids(array $filters, $ignoreCase = true)
{
$filters = $this->transformFilters($filters);
- if ($this->isWindows()) {
+ if ($this->isWindows() === true) {
// on windows, this command is very slow, and it's filters DO NOT speed it up
$cmd = 'tasklist /V /FO CSV /NH';
- } elseif ($this->isLinux()) {
+ } elseif ($this->isLinux() === true) {
// on linux
$cmd = 'ps -Ao "%p,%U,%a" --no-headers';
} else {
@@ -139,17 +207,19 @@ public function getPids(array $filters, $ignoreCase = true)
if ($this->isWindows() === true) {
$splitLine = trim($line, '"');
$splitLine = explode('","', $splitLine, 9);
- /*
- 0 "Image Name",
- 1 "PID",
- 2 "Session Name",
- 3 "Session#",
- 4 "Mem Usage",
- 5 "Status",
- 6 "User Name",
- 7 "CPU Time",
- 8 "Window Title"
- */
+
+ /*
+ 0 "Image Name",
+ 1 "PID",
+ 2 "Session Name",
+ 3 "Session#",
+ 4 "Mem Usage",
+ 5 "Status",
+ 6 "User Name",
+ 7 "CPU Time",
+ 8 "Window Title"
+ */
+
$details = [
'os' => $this->os,
'user' => $splitLine[6],
@@ -159,11 +229,13 @@ public function getPids(array $filters, $ignoreCase = true)
} elseif ($this->isLinux() === true) {
$splitLine = explode(',', $line, 3);
- /*
- 0 pid (padded)
- 1 user (padded)
- 2 command
- */
+
+ /*
+ 0 pid (padded)
+ 1 user (padded)
+ 2 command
+ */
+
$details = [
'os' => $this->os,
'user' => trim($splitLine[1]),
@@ -193,9 +265,12 @@ public function getPids(array $filters, $ignoreCase = true)
/**
- * @param integer $pid Pid.
+ * @param string $pid Pid.
+ * @param string|null $file File containing pid to delete.
*
* @return boolean Success.
+ * @throws \RuntimeException OS not supported.
+ * @throws \RuntimeException Could not execute kill command.
*
* @since 2015-07-29
*/
@@ -228,6 +303,13 @@ public function killPid($pid, $file = null)
}//end killPid()
+ /**
+ * @param string $file Filename including path.
+ *
+ * @return string|boolean Pid or false if no file.
+ *
+ * @since 2015-07-30
+ */
public function getPidFromFile($file)
{
if (file_exists($file) === true) {
@@ -239,6 +321,13 @@ public function getPidFromFile($file)
}//end getPidFromFile()
+ /**
+ * @param string $file Filename including path.
+ *
+ * @return boolean
+ *
+ * @since 2015-07-30
+ */
public function killPidFromFile($file)
{
$result = $this->killPid($this->getPidFromFile($file), $file);
@@ -246,6 +335,14 @@ public function killPidFromFile($file)
}//end killPidFromFile()
+ /**
+ * @param string $file Filename including path.
+ * @param boolean $kill Current recorded PID.
+ *
+ * @return boolean
+ *
+ * @since 2015-07-30
+ */
public function claimPidFile($file, $kill = false)
{
$pid = $this->getPidFromFile($file);
diff --git a/src/RunningFilter.php b/src/RunningFilter.php
index 086f8f0..2deafd5 100644
--- a/src/RunningFilter.php
+++ b/src/RunningFilter.php
@@ -2,64 +2,103 @@
namespace Ofbeaton\Command;
+/**
+ * Class RunningFilter
+ *
+ * @since 2015-07-30
+ */
class RunningFilter
{
+ /**
+ * @var array
+ * @since 2015-07-30
+ */
protected $fields = [];
+ /**
+ * @var array
+ * @since 2015-07-30
+ */
protected $mods = [];
+ /**
+ * @var array
+ * @since 2015-07-30
+ */
protected $validFields = [
- 'os',
- 'pid',
- 'user',
- 'process',
+ 'os',
+ 'pid',
+ 'user',
+ 'process',
];
+ /**
+ * @var array
+ * @since 2015-07-30
+ */
protected $validMods = [
- 'os' => [
- '=',
- '!',
- ],
- 'pid' => [
- '=',
- '!',
- ],
- 'user' => [
- '=',
- '!',
- ],
- 'process' => [
- '=',
- '!',
- '~=',
- '~!',
- ],
+ 'os' => [
+ '=',
+ '!',
+ ],
+ 'pid' => [
+ '=',
+ '!',
+ ],
+ 'user' => [
+ '=',
+ '!',
+ ],
+ 'process' => [
+ '=',
+ '!',
+ '~=',
+ '~!',
+ ],
];
+ /**
+ * @var array
+ * @since 2015-07-30
+ */
protected $defaultMods = [
- 'os' => '=',
- 'pid' => '=',
- 'user' => '=',
- 'process' => '~=',
+ 'os' => '=',
+ 'pid' => '=',
+ 'user' => '=',
+ 'process' => '~=',
];
+ /**
+ * @var array
+ * @since 2015-07-30
+ */
protected $autoRegEx = [
- 'os' => true,
- 'pid' => true,
- 'user' => true,
- 'process' => false,
+ 'os' => true,
+ 'pid' => true,
+ 'user' => true,
+ 'process' => false,
];
+ /**
+ * @param string $field Field.
+ * @param string $value Regex.
+ *
+ * @return string
+ * @throws \InvalidArgumentException Regex cannot contain space or tab.
+ *
+ * @since 2015-07-30
+ */
protected function transform($field, $value)
{
if (strpos($value, '/') === 0) {
// we got a regex
if (strpos($value, ' ') !== false || strpos($value, "\t") !== false) {
- throw new \InvalidArgumentException('Field `'.$field.'` regular expression cannot contain a space or tab. Use \s+ instead');
+ $error = 'Field `'.$field.'` regular expression cannot contain a space or tab. Use \s+ instead';
+ throw new \InvalidArgumentException($error);
}
- } elseif ($autoRegEx[$field] === true) {
+ } elseif ($this->autoRegEx[$field] === true) {
// turn it into a regex
if (in_array($this->mods[$field], ['=', '!']) === true) {
$value = '^'.$value.'$';
@@ -74,6 +113,14 @@ protected function transform($field, $value)
}//end transform()
+ /**
+ * @param string $field Field.
+ *
+ * @return mixed Field value.
+ * @throws \InvalidArgumentException Invalid field.
+ *
+ * @since 2015-07-30
+ */
public function getField($field)
{
if (in_array($field, $this->validFields) === false) {
@@ -86,6 +133,14 @@ public function getField($field)
}//end getField()
+ /**
+ * @param string $field Field.
+ *
+ * @return mixed Mod applied to a given Field.
+ * @throws \InvalidArgumentException Invalid field.
+ *
+ * @since 2015-07-30
+ */
public function getMod($field)
{
if (in_array($field, $this->validFields) === false) {
@@ -98,6 +153,16 @@ public function getMod($field)
}//end getMod()
+ /**
+ * @param string $field Field.
+ * @param string $value New value.
+ * @param string $mod Mod for field.
+ *
+ * @return $this
+ * @throws \InvalidArgumentException Invalid field.
+ *
+ * @since 2015-07-30
+ */
public function setField($field, $value, $mod = null)
{
if (in_array($field, $this->validFields) === false) {
@@ -119,6 +184,11 @@ public function setField($field, $value, $mod = null)
}//end setField()
+ /**
+ * @return mixed OS.
+ *
+ * @since 2015-07-30
+ */
public function getOs()
{
$result = $this->getField('os');
@@ -126,6 +196,14 @@ public function getOs()
}//end getOs()
+ /**
+ * @param string $os Value.
+ * @param string $mod Mod on OS field.
+ *
+ * @return $this
+ *
+ * @since 2015-07-30
+ */
public function setOs($os, $mod = null)
{
$this->setField('os', $os, $mod);
@@ -133,6 +211,11 @@ public function setOs($os, $mod = null)
}//end setOs()
+ /**
+ * @return int Value of PID field.
+ *
+ * @since 2015-07-30
+ */
public function getPid()
{
$result = $this->getField('pid');
@@ -140,6 +223,14 @@ public function getPid()
}//end getPid()
+ /**
+ * @param string $pid New Value.
+ * @param string $mod Mod on PID field.
+ *
+ * @return $this
+ *
+ * @since 2015-07-30
+ */
public function setPid($pid, $mod = null)
{
$this->setField('pid', $pid, $mod);
@@ -147,6 +238,11 @@ public function setPid($pid, $mod = null)
}//end setPid()
+ /**
+ * @return mixed Process.
+ *
+ * @since 2015-07-30
+ */
public function getProcess()
{
$result = $this->getField('process');
@@ -154,6 +250,14 @@ public function getProcess()
}//end getProcess()
+ /**
+ * @param string $process Process.
+ * @param string $mod Mod.
+ *
+ * @return $this
+ *
+ * @since 2015-07-30
+ */
public function setProcess($process, $mod = null)
{
$this->setField('process', $process, $mod);
@@ -161,6 +265,11 @@ public function setProcess($process, $mod = null)
}//end setProcess()
+ /**
+ * @return mixed User.
+ *
+ * @since 2015-07-30
+ */
public function getUser()
{
$result = $this->getField('user');
@@ -168,6 +277,14 @@ public function getUser()
}//end getUser()
+ /**
+ * @param string $user User.
+ * @param string $mod Mod.
+ *
+ * @return $this
+ *
+ * @since 2015-07-30
+ */
public function setUser($user, $mod = null)
{
$this->setField('user', $user, $mod);
@@ -175,7 +292,15 @@ public function setUser($user, $mod = null)
}//end setUser()
- public function isOk($details)
+ /**
+ * @param array $details List of fields.
+ *
+ * @return boolean are the details valid.
+ * @throws \RuntimeException Missing field.
+ *
+ * @since 2015-07-30
+ */
+ public function isOk(array $details)
{
foreach ($this->validFields as $field) {
if (isset($details[$field]) === false) {
@@ -189,6 +314,15 @@ public function isOk($details)
}//end isOk()
+ /**
+ * @param string $field Field.
+ * @param mixed $userValue Value to check.
+ * @param mixed $filterValue Filter to check value against.
+ *
+ * @return boolean
+ *
+ * @since 2015-07-30
+ */
protected function match($field, $userValue, $filterValue)
{
if ($filterValue === null) {