Refactored project structure.

This commit is contained in:
Dmitry Khomutov 2018-03-04 18:04:15 +07:00
commit c015d8c58b
No known key found for this signature in database
GPG key ID: EC19426474B37AAC
308 changed files with 39 additions and 47 deletions

View file

@ -0,0 +1,52 @@
<?php
namespace PHPCensor\ProcessControl;
/**
* Construct an appropriate ProcessControl instance.
*
* @author Adirelle <adirelle@gmail.com>
*/
class Factory
{
/**
* ProcessControl singleton.
*
* @var ProcessControlInterface
*/
protected static $instance = null;
/**
* Returns the ProcessControl singleton.
*
* @return ProcessControlInterface
*/
public static function getInstance()
{
if (static::$instance === null) {
static::$instance = static::createProcessControl();
}
return static::$instance;
}
/**
* Create a ProcessControl depending on available extensions and the underlying OS.
*
* Check PosixProcessControl, WindowsProcessControl and UnixProcessControl, in that order.
*
* @return ProcessControlInterface
*
* @throws \Exception
*/
public static function createProcessControl()
{
switch (true) {
case PosixProcessControl::isAvailable():
return new PosixProcessControl();
case UnixProcessControl::isAvailable():
return new UnixProcessControl();
}
throw new \Exception("No ProcessControl implementation available.");
}
}

View file

@ -0,0 +1,42 @@
<?php
namespace PHPCensor\ProcessControl;
/**
* Control process using the POSIX extension.
*
* @author Adirelle <adirelle@gmail.com>
*/
class PosixProcessControl implements ProcessControlInterface
{
/**
* @param integer $pid
*
* @return bool
*/
public function isRunning($pid)
{
// Signal "0" is not sent to the process, but posix_kill checks the process anyway;
return posix_kill($pid, 0);
}
/**
* {@inheritdoc}
*/
public function kill($pid, $forcefully = false)
{
return posix_kill($pid, $forcefully ? 9 : 15);
}
/**
* Check whether this posix_kill is available.
*
* @return bool
*
* @internal
*/
public static function isAvailable()
{
return function_exists('posix_kill');
}
}

View file

@ -0,0 +1,30 @@
<?php
namespace PHPCensor\ProcessControl;
/**
* A stateless service to check and kill system processes.
*
* @author Adirelle <adirelle@gmail.com>
*/
interface ProcessControlInterface
{
/**
* Checks if a process exists.
*
* @param int $pid The process identifier.
*
* @return boolean true is the process is running, else false.
*/
public function isRunning($pid);
/**
* Terminate a running process.
*
* @param int $pid The process identifier.
* @param bool $forcefully Whether to gently (false) or forcefully (true) terminate the process.
*
* @return boolean
*/
public function kill($pid, $forcefully = false);
}

View file

@ -0,0 +1,50 @@
<?php
namespace PHPCensor\ProcessControl;
/**
* Control processes using the "ps" and "kill" commands.
*
* @author Adirelle <adirelle@gmail.com>
*/
class UnixProcessControl implements ProcessControlInterface
{
/**
* Check process using the "ps" command.
*
* @param int $pid
*
* @return boolean
*/
public function isRunning($pid)
{
$output = $exitCode = null;
exec(sprintf("ps %d", $pid), $output, $exitCode);
return $exitCode === 0;
}
/**
* {@inheritdoc}
*/
public function kill($pid, $forcefully = false)
{
$output = [];
$result = 1;
exec(sprintf("kill -%d %d", $forcefully ? 9 : 15, $pid), $output, $result);
return !$result;
}
/**
* Check whether the commands "ps" and "kill" are available.
*
* @return bool
*
* @internal
*/
public static function isAvailable()
{
return DIRECTORY_SEPARATOR === '/' && exec("which ps") && exec("which kill");
}
}