livecoding-console/src/Deblan/Application.php
2015-07-14 00:19:31 +02:00

115 lines
2.5 KiB
PHP

<?php
namespace Deblan;
use ReflectionClass;
use ReflectionException;
use Symfony\Component\Console\Application as BaseApplication;
use Symfony\Component\Finder\Finder;
use Pimple\Container;
/**
* Class Application
* @author Simon Vieille
*/
class Application extends BaseApplication
{
protected $commandsPaths = [];
protected $configLoader;
protected $container;
public function addCommandsPath($path, $namespace)
{
$this->commandsPaths[$path] = trim($namespace, '\\');
return $this;
}
public function chdir($directory)
{
chdir($directory);
return $this;
}
public function loadCommands()
{
foreach ($this->commandsPaths as $path => $namespace) {
$finder = new Finder();
$finder->name('*Command.php')->in($path);
foreach ($finder as $file) {
$className = $namespace.'\\'.str_replace('.php', '', $file->getFilename());
try {
$reflexion = new ReflectionClass($className);
if ($reflexion->isInstantiable()) {
$command = new $className();
$this->addCommands(array(
$command,
));
}
} catch (ReflectionException $e) {
}
}
}
return $this;
}
public function setConfigLoader(ConfigLoader $configLoader)
{
$this->configLoader = $configLoader;
return $this;
}
public function getConfigLoader()
{
return $this->configLoader;
}
public function buildUrl($uri = '')
{
return sprintf('https://www.livecoding.tv/%s', ltrim($uri, '/'));
}
public function buildStreamUrl($stream)
{
return sprintf(
'rtmp://eumedia1.livecoding.tv:1935/livecodingtv/%s',
trim($stream, '/')
);
}
public function getPlayer($stream)
{
$stream = $this->buildStreamUrl($stream);
return str_replace(
'%stream%',
escapeshellarg($stream),
$this->getConfigLoader()->getConfig()['player']['command']
);
}
public function isDebugMode()
{
return $this->getConfigLoader()->getConfig()['debug'];
}
public function getContainer()
{
if (null !== $this->container) {
return $this->container;
}
return $this->container = new Container();
}
}