Created
February 7, 2012 10:56
-
-
Save sveneisenschmidt/1759104 to your computer and use it in GitHub Desktop.
Silex CLI Routing Prototype (Class)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Provider; | |
use Silex\Application; | |
use Silex\ServiceProviderInterface; | |
use Symfony\Component\Routing\Exception\ResourceNotFoundException; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\HttpKernel\HttpKernel; | |
use Symfony\Component\HttpKernel\HttpKernelInterface; | |
class CliProvider implements ServiceProviderInterface | |
{ | |
public function register(Application $app) | |
{ | |
if(php_sapi_name() === 'cli') { | |
$app->before(function() use($app) { | |
$argv = $_SERVER['argv']; | |
$path = !isset($argv[1]) ? '/' : $argv[1]; | |
$request = Request::create($path); | |
if(isset($app['url_generator'])) { | |
$hostmap = isset($app['cli.hostmap']) ? $app['cli.hostmap'] : array(); | |
$hostname = gethostname(); | |
$context = $app['request_context']; | |
$generator = $app['url_generator']; | |
$options = array( | |
'baseUrl' => $context->getBaseUrl(), | |
'method' => $context->getMethod(), | |
'host' => $context->getHost(), | |
'scheme' => $context->getScheme(), | |
'httpPort' => $context->getHttpPort() ?: $app['request.http_port'], | |
'httpsPort' => $context->getHttpsPort(), | |
'parameters'=> $context->getParameters() | |
); | |
if(!empty($hostmap)) { | |
foreach($hostmap as $mapping) { | |
$name = isset($mapping[0]) ? $mapping[0] : ''; | |
$opts = isset($mapping[1]) ? $mapping[1] : array(); | |
if(is_array($name) && in_array($hostname, $name) || | |
is_string($name) && $hostname = $name | |
) { | |
$options = array_merge($options, $opts); | |
break; | |
} | |
} | |
foreach($options as $key => $value) { | |
$method = 'set' . ucfirst($key); | |
$value = str_replace(array( | |
'%hostname%' | |
), array( | |
$hostname | |
), $value); | |
call_user_func_array(array($context, $method), array($value)); | |
} | |
} | |
} | |
try { | |
return $app->handle($request, HttpKernelInterface::SUB_REQUEST, false); | |
} catch (\Exception $e) {} | |
return new Response(null); | |
}); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$app->register(new App\Provider\CliProvider(), array( | |
'cli.hostmap' => array( | |
array( | |
array('ic-dev-02', 'localhost'), | |
array( | |
'baseUrl' => '/projects/testenvironment/frontend', | |
'host' => '%hostname%' | |
) | |
), | |
array( | |
'ic-dev-02-selenium-php', | |
array( | |
'baseUrl' => '/testing-frontend', | |
'host' => '192.168.0.17' | |
) | |
) | |
) | |
)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment