2014-07-21 10:26:06 -07:00
|
|
|
<?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;
|
2015-05-16 12:17:59 +02:00
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
|
2024-10-01 15:54:04 +03:00
|
|
|
use Nelmio\ApiDocBundle\Util\DocCommentExtractor;
|
2014-07-21 10:26:06 -07:00
|
|
|
use Symfony\Component\Config\ConfigCache;
|
|
|
|
use Symfony\Component\Config\Resource\FileResource;
|
|
|
|
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
|
|
|
/**
|
2024-07-02 16:24:26 +03:00
|
|
|
* @param HandlerInterface[] $handlers
|
|
|
|
* @param AnnotationsProviderInterface[] $annotationsProviders
|
|
|
|
* @param string[] $excludeSections
|
|
|
|
* @param bool|false $debug
|
2015-07-21 17:13:40 +07:00
|
|
|
*/
|
2014-07-21 10:26:06 -07:00
|
|
|
public function __construct(
|
|
|
|
RouterInterface $router,
|
|
|
|
Reader $reader,
|
|
|
|
DocCommentExtractor $commentExtractor,
|
|
|
|
array $handlers,
|
2015-03-15 23:48:57 +01:00
|
|
|
array $annotationsProviders,
|
2024-07-02 16:24:26 +03:00
|
|
|
array $excludeSections,
|
|
|
|
private string $cacheFile,
|
2024-10-01 15:54:04 +03:00
|
|
|
private bool $debug = false,
|
2014-07-21 10:26:06 -07:00
|
|
|
) {
|
2024-07-02 16:24:26 +03:00
|
|
|
parent::__construct($router, $reader, $commentExtractor, $handlers, $annotationsProviders, $excludeSections);
|
2014-07-21 10:26:06 -07:00
|
|
|
}
|
|
|
|
|
2015-07-21 17:13:40 +07:00
|
|
|
/**
|
2024-10-01 15:54:04 +03:00
|
|
|
* @param string $view View name
|
|
|
|
*
|
2015-07-21 17:13:40 +07:00
|
|
|
* @return array|mixed
|
|
|
|
*/
|
2015-05-16 12:17:59 +02:00
|
|
|
public function all($view = ApiDoc::DEFAULT_VIEW)
|
2014-07-21 10:26:06 -07:00
|
|
|
{
|
2015-07-21 17:13:40 +07:00
|
|
|
$cache = $this->getViewCache($view);
|
|
|
|
|
2015-10-23 10:07:45 +02:00
|
|
|
if (!$cache->isFresh()) {
|
2024-10-01 15:54:04 +03:00
|
|
|
$resources = [];
|
2014-07-21 10:26:06 -07:00
|
|
|
foreach ($this->getRoutes() as $route) {
|
2024-10-01 15:54:04 +03:00
|
|
|
if (null !== ($method = $this->getReflectionMethod($route->getDefault('_controller')))
|
2014-07-21 10:26:06 -07:00
|
|
|
&& null !== ($annotation = $this->reader->getMethodAnnotation($method, self::ANNOTATION_CLASS))) {
|
2024-10-01 15:54:04 +03:00
|
|
|
$file = $method->getDeclaringClass()->getFileName();
|
2014-07-21 10:26:06 -07:00
|
|
|
$resources[] = new FileResource($file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$resources = array_merge($resources, $this->router->getRouteCollection()->getResources());
|
|
|
|
|
2015-05-16 12:17:59 +02:00
|
|
|
$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
|
|
|
|
2014-07-21 10:26:06 -07: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
|
|
|
}
|
2014-07-21 10:26:06 -07:00
|
|
|
|
2015-07-21 17:13:40 +07:00
|
|
|
/**
|
2024-10-01 15:54:04 +03:00
|
|
|
* @param string $view
|
|
|
|
*
|
2015-07-21 17:13:40 +07:00
|
|
|
* @return ConfigCache
|
|
|
|
*/
|
2015-07-22 11:20:20 +07:00
|
|
|
private function getViewCache($view)
|
2015-07-21 17:13:40 +07:00
|
|
|
{
|
2024-10-01 15:54:04 +03:00
|
|
|
return new ConfigCache($this->cacheFile . '.' . $view, $this->debug);
|
2014-07-21 10:26:06 -07:00
|
|
|
}
|
2015-03-06 11:19:08 +01:00
|
|
|
}
|