NelmioApiDocBundle/Extractor/CachingApiDocExtractor.php

115 lines
3.3 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the NelmioApiDocBundle.
*
* (c) Nelmio <hello@nelm.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Extractor;
use Doctrine\Common\Annotations\Reader;
use Nelmio\ApiDocBundle\Util\DocCommentExtractor;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
2015-03-15 23:48:57 +01:00
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\RouterInterface;
/**
* Class CachingApiDocExtractor
*
* @author Bez Hermoso <bez@activelamp.com>
*/
class CachingApiDocExtractor extends ApiDocExtractor
{
2015-07-21 17:13:40 +07:00
/**
* @var string
*/
private $cacheFile;
2015-07-21 17:13:40 +07:00
/**
* @var bool
*/
private $debug;
2015-07-21 17:13:40 +07:00
/**
2015-10-22 14:42:59 +02:00
* @param ContainerInterface $container
* @param RouterInterface $router
* @param Reader $reader
* @param DocCommentExtractor $commentExtractor
2015-07-21 17:13:40 +07:00
* @param ControllerNameParser $controllerNameParser
2015-10-22 14:42:59 +02:00
* @param array $handlers
* @param array $annotationsProviders
* @param string $cacheFile
* @param bool|false $debug
2015-07-21 17:13:40 +07:00
*/
public function __construct(
ContainerInterface $container,
RouterInterface $router,
Reader $reader,
DocCommentExtractor $commentExtractor,
2015-03-15 23:48:57 +01:00
ControllerNameParser $controllerNameParser,
array $handlers,
2015-03-15 23:48:57 +01:00
array $annotationsProviders,
$cacheFile,
$debug = false
) {
2015-03-15 23:48:57 +01:00
parent::__construct($container, $router, $reader, $commentExtractor, $controllerNameParser, $handlers, $annotationsProviders);
$this->cacheFile = $cacheFile;
2015-07-21 17:13:40 +07:00
$this->debug = $debug;
}
2015-07-21 17:13:40 +07:00
/**
2015-10-22 14:42:59 +02:00
* @param string $view View name
2015-07-21 17:13:40 +07:00
* @return array|mixed
*/
public function all($view = ApiDoc::DEFAULT_VIEW)
{
2015-07-21 17:13:40 +07:00
$cache = $this->getViewCache($view);
2015-10-23 10:07:45 +02:00
if (!$cache->isFresh()) {
$resources = array();
foreach ($this->getRoutes() as $route) {
if ( null !== ($method = $this->getReflectionMethod($route->getDefault('_controller')))
&& null !== ($annotation = $this->reader->getMethodAnnotation($method, self::ANNOTATION_CLASS))) {
2015-10-23 10:07:45 +02:00
$file = $method->getDeclaringClass()->getFileName();
$resources[] = new FileResource($file);
}
}
$resources = array_merge($resources, $this->router->getRouteCollection()->getResources());
$data = parent::all($view);
2015-07-21 17:13:40 +07:00
$cache->write(serialize($data), $resources);
2015-03-06 11:19:08 +01:00
return $data;
}
2015-10-23 10:07:45 +02:00
// For BC
if (method_exists($cache, 'getPath')) {
$cachePath = $cache->getPath();
} else {
$cachePath = (string) $cache;
}
2015-07-21 17:13:40 +07:00
2015-10-23 10:07:45 +02:00
return unserialize(file_get_contents($cachePath));
2015-07-21 17:13:40 +07:00
}
2015-07-21 17:13:40 +07:00
/**
2015-10-22 14:42:59 +02:00
* @param string $view
2015-07-21 17:13:40 +07:00
* @return ConfigCache
*/
private function getViewCache($view)
2015-07-21 17:13:40 +07:00
{
return new ConfigCache($this->cacheFile.'.'.$view, $this->debug);
}
2015-07-21 17:13:40 +07:00
2015-03-06 11:19:08 +01:00
}