Skip to content

Commit

Permalink
Replace tedivm/stash with Symfony Cache
Browse files Browse the repository at this point in the history
Symfony Cache provides a PSR-6/16 compatible cache interface that, with
the Contracts, can be a simpler alternative to Stash.

In this change I have also done a couple of optimizations to the output
of phpDocumentor so that users see less notices and have a better
output.
  • Loading branch information
mvriel committed Nov 27, 2019
1 parent e6d1d61 commit c38c1e1
Show file tree
Hide file tree
Showing 21 changed files with 133 additions and 503 deletions.
7 changes: 4 additions & 3 deletions bin/phpdoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env php
<?php

use phpDocumentor\AutoloaderLocator;
use phpDocumentor\Kernel;
use Doctrine\Common\Annotations\AnnotationRegistry;
use phpDocumentor\Console\Application;
Expand All @@ -12,15 +13,15 @@ use Symfony\Component\Dotenv\Dotenv;
set_time_limit(0);

require __DIR__ . '/../src/phpDocumentor/AutoloaderLocator.php';
$loader = \phpDocumentor\AutoloaderLocator::autoload();
$loader = AutoloaderLocator::autoload();

if (!class_exists(SymfonyApplication::class)) {
throw new \RuntimeException('You need to add "symfony/framework-bundle" as a Composer dependency.');
throw new RuntimeException('You need to add "symfony/framework-bundle" as a Composer dependency.');
}

if (!isset($_SERVER['PHPDOC_ENV']) && !Kernel::isPhar()) {
if (!class_exists(Dotenv::class)) {
throw new \RuntimeException('APP_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.');
throw new RuntimeException('APP_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.');
}
(new Dotenv())->load(__DIR__.'/../.env');
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"phpdocumentor/type-resolver": "^0.7.1",
"psr/cache": "^1.0",
"psr/log": "^1.1",
"symfony/cache": "5.0.*",
"symfony/config": "5.0.*",
"symfony/console": "^5.0",
"symfony/dependency-injection": "5.0.*",
Expand All @@ -58,7 +59,6 @@
"symfony/routing": "5.0.*",
"symfony/stopwatch": "^5.0",
"symfony/yaml": "^5.0",
"tedivm/stash": "^0.14.2",
"twig/twig": "~2.0",
"webmozart/assert": "^1.3",
"zendframework/zend-serializer": "~2.1",
Expand Down
62 changes: 1 addition & 61 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions config/packages/framework.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@ framework:

php_errors:
log: true

cache:
app: cache.adapter.filesystem
system: cache.adapter.filesystem

pools:
files:
adapter: cache.adapter.filesystem
descriptors:
adapter: cache.adapter.filesystem
6 changes: 4 additions & 2 deletions config/packages/prod/monolog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ monolog:
process_psr_3_messages: false
channels: ["!event", "!doctrine"]
verbosity_levels:
VERBOSITY_NORMAL: INFO
VERBOSITY_VERBOSE: INFO
VERBOSITY_NORMAL: ERROR
VERBOSITY_VERBOSE: NOTICE
VERBOSITY_VERY_VERBOSE: INFO
VERBOSITY_DEBUG: DEBUG
18 changes: 8 additions & 10 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -213,26 +213,24 @@ services:
phpDocumentor\Parser\Middleware\StopwatchMiddleware:
tags: [ { name: 'phpdoc.parser.middleware', priority: 5000 } ]

# Increments the progress bar; by placing it in front of the cache you see the progress bar
# increment, even while we only consume the cache
phpDocumentor\Parser\Middleware\EmittingMiddleware:
tags: [ { name: 'phpdoc.parser.middleware', priority: 4500 } ]

phpDocumentor\Parser\Middleware\CacheMiddleware:
arguments:
$dataStore: '@Stash\Pool'
tags: [ { name: 'phpdoc.parser.middleware', priority: 4000 } ]

phpDocumentor\Parser\Middleware\ErrorHandlingMiddleware:
tags: [ { name: 'phpdoc.parser.middleware', priority: 3000 } ]

phpDocumentor\Parser\Middleware\EmittingMiddleware:
tags: [ { name: 'phpdoc.parser.middleware', priority: 0 } ]

###################################################################################
## Autoloading definitions for external services ##################################
###################################################################################

Psr\Cache\CacheItemPoolInterface: '@Stash\Pool'

Stash\Interfaces\PoolInterface: '@Stash\Pool'

Stash\Pool: ~
phpDocumentor\Descriptor\Cache\ProjectDescriptorMapper:
arguments:
$descriptors: '@descriptors'

League\Flysystem\MountManager: ~

Expand Down
4 changes: 2 additions & 2 deletions config/stages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ services:
## Autoloading definitions for cache stages #################################
###################################################################################

phpDocumentor\Application\Stage\Cache\ConfigureCache:
phpDocumentor\Application\Stage\Cache\PurgeCachesWhenForced:
tags:
- { name: 'phpdoc.pipeline.application.cache', priority: 10000 }
- { name: 'phpdoc.pipeline.application.configuration', priority: 7000 }

phpDocumentor\Application\Stage\Cache\LoadProjectDescriptorFromCache:
tags:
Expand Down
9 changes: 1 addition & 8 deletions src/phpDocumentor/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,9 @@ public static function templateDirectory(): string
/**
* Initializes all components used by phpDocumentor.
*/
public function __construct(LoggerInterface $logger)
public function __construct()
{
$this->defineIniSettings();

Dispatcher::getInstance()->addListener(
'parser.file.pre',
function (PreFileEvent $event) use ($logger) {
$logger->log(LogLevel::NOTICE, 'Parsing ' . $event->getFile());
}
);
}

/**
Expand Down
45 changes: 0 additions & 45 deletions src/phpDocumentor/Application/Stage/Cache/ConfigureCache.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Mike van Riel <[email protected]>
* @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/

namespace phpDocumentor\Application\Stage\Cache;

use phpDocumentor\Application\Stage\Payload;
use Psr\Log\LoggerInterface;
use Symfony\Component\Cache\Adapter\AdapterInterface;

final class PurgeCachesWhenForced
{
private $filesCache;
private $descriptorsCache;
private $logger;

public function __construct(
AdapterInterface $filesCache,
AdapterInterface $descriptorsCache,
LoggerInterface $logger
) {
$this->filesCache = $filesCache;
$this->descriptorsCache = $descriptorsCache;
$this->logger = $logger;
}

public function __invoke(Payload $payload)
{
$this->logger->info('Checking whether to purge cache');
if (! $payload->getConfig()['phpdocumentor']['use-cache']) {
$this->logger->info('Purging cache');
$this->filesCache->clear();
$this->descriptorsCache->clear();
}

return $payload;
}
}
28 changes: 20 additions & 8 deletions src/phpDocumentor/Application/Stage/Parser/ParseFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use phpDocumentor\Parser\Parser;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Symfony\Component\Cache\Adapter\AdapterInterface;

final class ParseFiles
{
Expand All @@ -30,17 +31,29 @@ final class ParseFiles
* @var LoggerInterface
*/
private $logger;
/**
* @var AdapterInterface
*/
private $filesCache;
/**
* @var AdapterInterface
*/
private $descriptorsCache;

public function __construct(Parser $parser, LoggerInterface $logger)
{
public function __construct(
Parser $parser,
LoggerInterface $logger,
AdapterInterface $filesCache,
AdapterInterface $descriptorsCache
) {
$this->parser = $parser;
$this->logger = $logger;
$this->filesCache = $filesCache;
$this->descriptorsCache = $descriptorsCache;
}

public function __invoke(Payload $payload)
{
$configuration = $payload->getConfig();

/*
* For now settings of the first api are used.
* We need to change this later, when we accept more different things
Expand All @@ -52,10 +65,9 @@ public function __invoke(Payload $payload)
$builder->setMarkers($apiConfig['markers']);
$builder->setIncludeSource($apiConfig['include-source']);

$this->parser->setForced(!$configuration['phpdocumentor']['use-cache']);

if ($builder->getProjectDescriptor()->getSettings()->isModified()) {
$this->parser->setForced(true);
$this->filesCache->clear();
$this->descriptorsCache->clear();
$this->log(
'One of the project\'s settings have changed, forcing a complete rebuild',
LogLevel::NOTICE
Expand All @@ -78,7 +90,7 @@ public function __invoke(Payload $payload)
/**
* Dispatches a logging request.
*
* @param string $priority The logging priority as declared in the LogLevel PSR-3 class.
* @param string $priority The logging priority as declared in the LogLevel PSR-3 class.
* @param string[] $parameters
*/
private function log(string $message, string $priority = LogLevel::INFO, array $parameters = []): void
Expand Down
Loading

0 comments on commit c38c1e1

Please sign in to comment.