Skip to content

Commit

Permalink
Merge pull request #3 from BPing/dev
Browse files Browse the repository at this point in the history
doc(*):文档信息
  • Loading branch information
BPing authored Sep 17, 2018
2 parents ea2fa80 + 7f1d063 commit f08bda8
Show file tree
Hide file tree
Showing 23 changed files with 613 additions and 213 deletions.
123 changes: 102 additions & 21 deletions src/FFMpegPush/Command/Command.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace FFMpegPush\Command;

use FFMpegPush\Configuration;
Expand All @@ -14,43 +15,80 @@
use Symfony\Component\Process\Process;
use Symfony\Component\Process\ProcessBuilder;

/**
* Class Command 命令基本类.
*/
class Command implements CommandInterface
{
/**
* 名字.
*
* @var string
*/
protected $name = '';

/** @var $process Process */
/**
* 底层命令进程句柄。
*
* @var Process
*/
protected $process;
/** @var $config Configuration */

/**
* 命令配置信息.
*
* @var Configuration
*/
protected $config;
/** @var $logger LoggerInterface */

/** @var $logger LoggerInterface */
protected $logger;
/** @var */

/**
* 命令可执行文件目录.
*
* @var string
*/
protected $binary;
/** one day */

/**
* 命令执行超时时间. 默认一天.
*/
const TimeOut = 86400;
/** @var $listeners ListenerInterface[] */
protected $listeners = array();

/**
* 监听者集合。
* 监听命令执行进度.
*
* @var ListenerInterface[]
*/
protected $listeners = [];

/**
* Command constructor.
*
* @param array $config
*<code>
* <?php
* array(
* "binarys"=>array(), // 可执行命令,数组类型。只有一个有效,优先级和数组顺序一致
* "timeout"=>"", // 默认一天。如果非法格式,也就是非数字类型,都统一采用默认值
* )
* @param $logger
*
* </code>
* @param $logger
*
* @throws ConfigException
* @throws ExecutableNotFoundException
*/
public function __construct($config = array(), LoggerInterface $logger = null)
public function __construct($config = [], LoggerInterface $logger = null)
{
if (is_array($config)) {
$this->config = new Configuration($config);
} elseif ($config instanceof ConfigurationInterface) {
$this->config = $config;
} else {
throw new ConfigException("config should not be null");
throw new ConfigException('config should not be null');
}

if (!$this->config->has('binaries')) {
Expand All @@ -63,7 +101,7 @@ public function __construct($config = array(), LoggerInterface $logger = null)
$finder = new ExecutableFinder();
$binary = null;
$binaries = $this->config->get('binaries');
$binaries = is_array($binaries) ? $binaries : array($binaries);
$binaries = is_array($binaries) ? $binaries : [$binaries];

foreach ($binaries as $candidate) {
if (file_exists($candidate) && is_executable($candidate)) {
Expand All @@ -83,14 +121,17 @@ public function __construct($config = array(), LoggerInterface $logger = null)
$this->binary = $binary;

if (null === $logger) {
$logger = new Logger(__NAMESPACE__ . ' logger');
$logger = new Logger(__NAMESPACE__.' logger');
$logger->pushHandler(new NullHandler());
}
$this->logger = $logger;
}

/**
* 初始进程句柄.
*
* @param $command
*
* @return Process
*/
protected function initProcess($command)
Expand All @@ -101,10 +142,13 @@ protected function initProcess($command)
->setTimeout($this->config->get('timeout'));
$this->process = $processBuilder->getProcess();
}

return $this->process;
}

/**
* 获取底层命令执行进程句柄.
*
* @return Process
*/
public function getProcess()
Expand All @@ -113,16 +157,22 @@ public function getProcess()
}

/**
* 添加监听者.
*
* @param ListenerInterface $listener
*
* @return $this
*/
public function addListener(ListenerInterface $listener)
{
$this->listeners[] = $listener;

return $this;
}

/**
* 获取所有监听者.
*
* @return ListenerInterface[]
*/
public function getListeners()
Expand All @@ -131,7 +181,10 @@ public function getListeners()
}

/**
* 执行命令.
*
* @param $command
*
* @return string
*/
public function command($command)
Expand All @@ -154,19 +207,24 @@ public function command($command)
}

/**
* 停止执行
* 停止执行.
*/
public function stop()
{
if ($this->process) {
$this->logger->info(sprintf('%s stopping.pid【%s】', $this->name, $this->process->getPid()));

return $this->process->stop();
}

return 0;
}

/**
* 返回监听回调函数体.
*
* @param $listeners
*
* @return \Closure
*/
private function buildCallback($listeners)
Expand All @@ -178,56 +236,79 @@ private function buildCallback($listeners)
};
}

/**
* 获取执行结果代码
*
* @return int|null
*/
public function getExitCode()
{
if ($this->process) {
return $this->process->getExitCode();
}
return null;
}

/**
* 获取执行命令具体文本信息.
*
* @return null|string
*/
public function getCommandLine()
{
if ($this->process) {
return $this->process->getCommandLine();
}
return null;
}

/**
* 获取执行结果代码文本信息.
*
* @return null|string
*/
public function getExitCodeText()
{
if ($this->process) {
return $this->process->getExitCodeText();
}
return null;
}

/**
* 获取错误输出内容.
*
* @return null|string
*/
public function getErrorOutput()
{
if ($this->process) {
return $this->process->getErrorOutput();
}
return null;
}

/**
* 获取输出内容.
*
* @return null|string
*/
public function getOutput()
{
if ($this->process) {
return $this->process->getOutput();
}
return null;
}

/**
* 是否执行成功
*
* @return bool|null
*/
public function isSuccessful()
{
if ($this->process) {
return $this->process->isSuccessful();
}
return null;
}

public function clear()
{

}
}
}
10 changes: 9 additions & 1 deletion src/FFMpegPush/Command/CommandInterface.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
<?php

namespace FFMpegPush\Command;

interface CommandInterface
{
/**
* 执行命令.
*
* @param mixed $command 命令参数
*
* @return mixed
*/
public function command($command);
}
}
15 changes: 10 additions & 5 deletions src/FFMpegPush/Command/FFMpegCommand.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
<?php

namespace FFMpegPush\Command;

use FFMpegPush\Configuration;
use FFMpegPush\ConfigurationInterface;
use Psr\Log\LoggerInterface;

/**
* ffmpeg 可执行命令.
*/
class FFMpegCommand extends Command
{
/**
* FFMpegCommand constructor.
* @param array $configuration
*
* @param array $configuration
* @param LoggerInterface|null $logger
*/
public function __construct($configuration = array(), LoggerInterface $logger = null)
public function __construct($configuration = [], LoggerInterface $logger = null)
{
$this->name = 'FFMpeg';
if (!$configuration instanceof ConfigurationInterface) {
$configuration = new Configuration($configuration);
}
$configuration->set('binaries', $configuration->get('ffmpeg.binaries', array('ffmpeg')));
$configuration->set('binaries', $configuration->get('ffmpeg.binaries', ['ffmpeg']));
parent::__construct($configuration, $logger);
}

public static function create($configuration = array(), LoggerInterface $logger = null)
public static function create($configuration = [], LoggerInterface $logger = null)
{
return new static($configuration, $logger);
}
}
}
Loading

0 comments on commit f08bda8

Please sign in to comment.