Skip to content

Instantly share code, notes, and snippets.

Created December 31, 2012 17:55
Show Gist options
  • Save anonymous/4421603 to your computer and use it in GitHub Desktop.
Save anonymous/4421603 to your computer and use it in GitHub Desktop.
Symfony2 RouteLoader for GoogleApiBundle
<?php
namespace **\GoogleApiBundle\Routing;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Loader\LoaderResolverInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use GoogleApi\Client;
use GoogleApi\Contrib;
class RouteLoader implements LoaderInterface
{
private $loaded = false;
private $libdir = '/web/Symfony/vendor/evert/google-api-php-client/src/GoogleApi/Contrib';
private $skip = array(
'.', '..', 'apiAdsenseService.php', 'apiAnalyticsService.php', 'apiGanService.php',
'apiOrkutService.php', 'apiPagespeedonlineService.php', 'apiPlusService.php'
);
public function getServiceResources($apiService) {
$properties = get_object_vars($apiService);
$serviceResources = array();
foreach($properties as $p) {
if(is_object($p) && is_subclass_of($p, '\GoogleApi\\Service\\ServiceResource')) {
$serviceResources[] = $p;
}
}
return $serviceResources;
}
public function load($resource, $type = null)
{
if (true === $this->loaded) {
throw new \RuntimeException('Do not add this loader twice');
}
$routes = new RouteCollection();
$client = new Client();
$apiServices = scandir($this->libdir);
foreach($apiServices as $apiService) {
if(!in_array($apiService, $this->skip)) {
$className = 'GoogleApi\\Contrib\\' . str_replace('.php', '', $apiService);
$apiService = new $className($client);
$stem = $apiService->restBasePath;
$properties = get_object_vars($apiService);
$serviceResources = array();
foreach($properties as $p) {
if(is_object($p) && is_subclass_of($p, '\GoogleApi\\Service\\ServiceResource')) {
$serviceResources[] = $p;
}
}
$resourceMethods = array();
foreach($serviceResources as $serviceResource) {
$methods = $serviceResource->getMethods();
foreach($methods as $name => $method) {
if(is_array($method) && array_key_exists('id', $method) && array_key_exists('path', $method) && array_key_exists('httpMethod', $method)) {
$name = 'googleApi.' . $method['id'];
$pattern = $stem . $method['path'];
$defaults = array(
'_controller' => '**GoogleApiBundle:Default:extraRoute',
);
$requirements = array(
'_method' => $method['httpMethod'],
);
$route = new Route($pattern, $defaults, $requirements);
$routes->add($name, $route);
}
}
}
}
}
return $routes;
}
public function supports($resource, $type = null)
{
return 'extra' === $type;
}
public function getResolver()
{
}
public function setResolver(LoaderResolverInterface $resolver)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment