Skip to content

Commit

Permalink
doc(*):文档信息
Browse files Browse the repository at this point in the history
  • Loading branch information
BPing committed Aug 29, 2018
1 parent cf3a56c commit 138cb94
Show file tree
Hide file tree
Showing 6 changed files with 376 additions and 12 deletions.
103 changes: 92 additions & 11 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,31 +15,69 @@
use Symfony\Component\Process\Process;
use Symfony\Component\Process\ProcessBuilder;

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

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

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

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

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

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

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

/**
* Command constructor.
*
* @param array $config
*<code>
* <?php
* array(
* "binarys"=>array(), // 可执行命令,数组类型。只有一个有效,优先级和数组顺序一致
* "timeout"=>"", // 默认一天。如果非法格式,也就是非数字类型,都统一采用默认值
* )
*
* </code>
* @param $logger
* @throws ConfigException
* @throws ExecutableNotFoundException
Expand All @@ -60,8 +99,8 @@ public function __construct($config = array(), LoggerInterface $logger = null)
$this->config->set('timeout', self::TimeOut);
}

$finder = new ExecutableFinder();
$binary = null;
$finder = new ExecutableFinder();
$binary = null;
$binaries = $this->config->get('binaries');
$binaries = is_array($binaries) ? $binaries : array($binaries);

Expand All @@ -70,26 +109,28 @@ public function __construct($config = array(), LoggerInterface $logger = null)
$binary = $candidate;
break;
}
if (null !== $binary = $finder->find($candidate)) {
if (null!==$binary = $finder->find($candidate)) {
break;
}
}
if (null === $binary) {
if (null===$binary) {
throw new ExecutableNotFoundException(sprintf(
'Executable not found, proposed : %s', implode(', ', $binaries)
));
}

$this->binary = $binary;

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

/**
* 初始进程句柄
*
* @param $command
* @return Process
*/
Expand All @@ -99,12 +140,14 @@ protected function initProcess($command)
$processBuilder = ProcessBuilder::create($command)
->setPrefix($this->binary)
->setTimeout($this->config->get('timeout'));
$this->process = $processBuilder->getProcess();
$this->process = $processBuilder->getProcess();
}
return $this->process;
}

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

/**
* 添加监听者
*
* @param ListenerInterface $listener
* @return $this
*/
Expand All @@ -123,6 +168,8 @@ public function addListener(ListenerInterface $listener)
}

/**
* 获取所有监听者
*
* @return ListenerInterface[]
*/
public function getListeners()
Expand All @@ -131,6 +178,8 @@ public function getListeners()
}

/**
* 执行命令
*
* @param $command
* @return string
*/
Expand Down Expand Up @@ -166,6 +215,8 @@ public function stop()
}

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

/**
* 获取执行结果代码
*
* @return int|null
*/
public function getExitCode()
{
if ($this->process) {
Expand All @@ -186,6 +242,11 @@ public function getExitCode()
return null;
}

/**
* 获取执行命令具体文本信息
*
* @return null|string
*/
public function getCommandLine()
{
if ($this->process) {
Expand All @@ -194,6 +255,11 @@ public function getCommandLine()
return null;
}

/**
* 获取执行结果代码文本信息
*
* @return null|string
*/
public function getExitCodeText()
{
if ($this->process) {
Expand All @@ -202,6 +268,11 @@ public function getExitCodeText()
return null;
}

/**
* 获取错误输出内容
*
* @return null|string
*/
public function getErrorOutput()
{
if ($this->process) {
Expand All @@ -210,6 +281,11 @@ public function getErrorOutput()
return null;
}

/**
* 获取输出内容
*
* @return null|string
*/
public function getOutput()
{
if ($this->process) {
Expand All @@ -218,6 +294,11 @@ public function getOutput()
return null;
}

/**
* 是否执行成功
*
* @return bool|null
*/
public function isSuccessful()
{
if ($this->process) {
Expand Down
7 changes: 7 additions & 0 deletions src/FFMpegPush/Command/CommandInterface.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
<?php

namespace FFMpegPush\Command;

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

namespace FFMpegPush\Command;

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

/**
* ffmpeg 可执行命令
*
* @package FFMpegPush\Command
*/
class FFMpegCommand extends Command
{
/**
Expand Down
Loading

0 comments on commit 138cb94

Please sign in to comment.