Skip to content

Commit

Permalink
feat: bump to PHP 7.2 and MVC v3 components
Browse files Browse the repository at this point in the history
This patch bumps the minimum supported PHP version to PHP 7.2, and the
various MVC components to the very latest stable versions (notably, v3
for laminas-mvc, laminas-eventmanager, and laminas-servicemanager).

As part of the bump, this also adopts the v2 version of
laminas-coding-standard.
  • Loading branch information
weierophinney committed Feb 4, 2020
1 parent cbf5078 commit 04bfd7f
Show file tree
Hide file tree
Showing 19 changed files with 249 additions and 304 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.phpcs-cache
.phpunit.result.cache
composer.lock
vendor/
12 changes: 0 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,6 @@ env:

matrix:
include:
- php: 7
env:
- DEPS=lowest
- php: 7
env:
- DEPS=latest
- php: 7.1
env:
- DEPS=lowest
- php: 7.1
env:
- DEPS=latest
- php: 7.2
env:
- DEPS=lowest
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ To enable the page cache factory, do the following:
return [
'service_manager' => [
'factories' => [
'PhlySimplePage\PageCache' => 'PhlySimplePage\PageCacheService',
'PhlySimplePage\PageCache' => \PhlySimplePage\PageCacheFactory::class,
],
],
];
Expand Down
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@
}
},
"require": {
"php": "^7.0",
"php": "^7.2",
"laminas/laminas-cache": "^2.9",
"laminas/laminas-eventmanager": "^2.6.4 || ^3.2.1",
"laminas/laminas-eventmanager": "^3.2.1",
"laminas/laminas-http": "^2.9.1",
"laminas/laminas-mvc": "^2.7.9 || ^3.1.1",
"laminas/laminas-stdlib": "^2.7.7 || ^3.2.1",
"laminas/laminas-mvc": "^3.1.1",
"laminas/laminas-stdlib": "^3.2.1",
"laminas/laminas-view": "^2.9.1",
"symfony/console": "^3.4 || ^4.0 || ^5.0",
"symfony/console": "^4.0 || ^5.0",
"ocramius/package-versions": "^1.0"
},
"require-dev": {
"laminas/laminas-coding-standard": "~1.0.0",
"phpunit/phpunit": "^5.7.25 || ^6.4.4",
"laminas/laminas-servicemanager": "^2.7.11 || ^3.4.0"
"laminas/laminas-coding-standard": "~2.0.0@rc || ~2.0.0",
"phpunit/phpunit": "^8.5.2",
"laminas/laminas-servicemanager": "^3.4.0"
},
"extra": {
"laminas": {
Expand Down
5 changes: 1 addition & 4 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@
'invokables' => array(
'PhlySimplePage\Controller\Page' => PageController::class,
),
'factories' => array(
'PhlySimplePage\Controller\Cache' => CacheControllerService::class,
),
),
'service_manager' => array(
'factories' => array(
ClearCacheCommand::class => ClearCacheCommandFactory::class,
PageCacheListener::class => PageCacheListenerService::class,
PageCacheListener::class => PageCacheListenerFactory::class,
),
),
);
16 changes: 14 additions & 2 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
<?xml version="1.0"?>
<ruleset name="Zend Framework coding standard">
<rule ref="./vendor/laminas/laminas-coding-standard/ruleset.xml"/>
<ruleset
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">

<arg name="basepath" value="." />
<arg name="cache" value=".phpcs-cache" />
<arg name="colors" />
<arg name="extensions" value="php" />
<arg name="parallel" value="80" />

<!-- Show progress -->
<arg value="p" />

<!-- Paths to check -->
<file>src</file>
<file>test</file>

<rule ref="LaminasCodingStandard"/>
</ruleset>
20 changes: 13 additions & 7 deletions src/ClearCacheCommand.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
<?php

/**
* @link https://github.com/weierophinney/PhlySimplePage for the canonical source repository
* @copyright Copyright (c) 2020 Matthew Weier O'Phinney (http://mwop.net)
* @copyright Copyright (c) 2020 Matthew Weier O'Phinney (https://mwop.net)
* @license https://github.com/weierophinney/PhlySimplePage/blog/master/LICENSE.md New BSD License
*/

declare(strict_types=1);

namespace PhlySimplePage;

use Laminas\Cache\Storage\Adapter\AbstractAdapter;
use Laminas\Cache\Storage\FlushableInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
use Laminas\Cache\Storage\FlushableInterface;

use function sprintf;

class ClearCacheCommand extends Command
{
/** @var AbstractAdapter */
private $cache;

public function __construct(AbstractAdapter $cache)
Expand All @@ -24,7 +30,7 @@ public function __construct(AbstractAdapter $cache)
parent::__construct();
}

protected function configure()
protected function configure(): void
{
$this->setName('cache:clear');
$this->addOption(
Expand All @@ -35,7 +41,7 @@ protected function configure()
);
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$page = $input->getOption('page');
if (! $page) {
Expand All @@ -45,7 +51,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $this->clearPage($page, $output);
}

private function clearAllPages(OutputInterface $output)
private function clearAllPages(OutputInterface $output): int
{
$output->writeln('<info>Clearing caches for all static pages</info>');

Expand All @@ -61,7 +67,7 @@ private function clearAllPages(OutputInterface $output)
return 0;
}

private function clearPage($page, OutputInterface $output)
private function clearPage(string $page, OutputInterface $output): int
{
$output->writeln(sprintf('<info>Clearing cache for page "%s"</info>', $page));

Expand Down
7 changes: 5 additions & 2 deletions src/ClearCacheCommandFactory.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
<?php

/**
* @link https://github.com/weierophinney/PhlySimplePage for the canonical source repository
* @copyright Copyright (c) 2020 Matthew Weier O'Phinney (http://mwop.net)
* @copyright Copyright (c) 2020 Matthew Weier O'Phinney (https://mwop.net)
* @license https://github.com/weierophinney/PhlySimplePage/blog/master/LICENSE.md New BSD License
*/

declare(strict_types=1);

namespace PhlySimplePage;

use Psr\Container\ContainerInterface;

class ClearCacheCommandFactory
{
public function __invoke(ContainerInterface $container)
public function __invoke(ContainerInterface $container): ClearCacheCommand
{
return new ClearCacheCommand(
$container->get('PhlySimplePage\PageCache')
Expand Down
67 changes: 23 additions & 44 deletions src/Module.php
Original file line number Diff line number Diff line change
@@ -1,43 +1,37 @@
<?php

/**
* @link https://github.com/weierophinney/PhlySimplePage for the canonical source repository
* @copyright Copyright (c) 2012 Matthew Weier O'Phinney (http://mwop.net)
* @copyright Copyright (c) 2012-2020 Matthew Weier O'Phinney (https://mwop.net)
* @license https://github.com/weierophinney/PhlySimplePage/blog/master/LICENSE.md New BSD License
*/

declare(strict_types=1);

namespace PhlySimplePage;

use Laminas\Mvc\Application;
use Laminas\Mvc\MvcEvent;
use Laminas\Stdlib\ResponseInterface;

/**
* Module class for use with ZF2
*/
use function str_replace;

class Module
{
/**
* Retrieve application configuration for this module
*
* @return array
* Normalize a cache key
*/
public function getConfig()
public static function normalizeCacheKey(string $key): string
{
return include __DIR__ . '/config/module.config.php';
return str_replace(['/', '\\', '.'], '_', $key);
}

/**
* Provide console usage messages for console endpoints
*
* @deprecated Since 1.1.0. To be removed in 2.0.0.
* @return array
* Retrieve application configuration for this module
*/
public function getConsoleUsage()
public function getConfig(): array
{
return [
'phlysimplepage cache clear all' => 'Clear caches for all static pages',
'phlysimplepage cache clear --page=' => 'Clear caches for a single static page',
['--page', 'Page name as matched via routing'],
];
return include __DIR__ . '/config/module.config.php';
}

/**
Expand All @@ -47,18 +41,16 @@ public function getConsoleUsage()
* "PhlySimplePage\PageCache" service is registered, it will pull the
* "PhlySimplePage\PageCacheListener" service and attach it to the
* event manager.
*
* @param \Laminas\Mvc\MvcEvent $e
*/
public function onBootstrap($e)
public function onBootstrap(MvcEvent $e): void
{
$app = $e->getTarget();
$events = $app->getEventManager();
$events->attach('route', [$this, 'onRoutePost'], -100);

$services = $app->getServiceManager();
if ($services->has('PhlySimplePage\PageCache')) {
$listener = $services->get('PhlySimplePage\PageCacheListener');
$listener = $services->get(PageCacheListener::class);
$listener->attach($events);
}
}
Expand All @@ -68,45 +60,41 @@ public function onBootstrap($e)
*
* Registers a post-dispatch listener on the controller if the matched
* controller is the PageController from this module.
*
* @param \Laminas\Mvc\MvcEvent $e
*/
public function onRoutePost($e)
public function onRoutePost(MvcEvent $e): void
{
$matches = $e->getRouteMatch();
if (! $matches) {
return;
}

$controller = $matches->getParam('controller');
if ($controller != 'PhlySimplePage\Controller\Page') {
if ($controller !== 'PhlySimplePage\Controller\Page') {
return;
}

$app = $e->getTarget();
$events = $app->getEventManager();
$shared = $events->getSharedManager();
$shared->attach('PhlySimplePage\PageController', 'dispatch', [$this, 'onDispatchPost'], -1);
$shared->attach(PageController::class, 'dispatch', [$this, 'onDispatchPost'], -1);
}

/**
* Listen to the dispatch event from the PageController
*
* If the controller result is a 404 status, triggers the application
* dispatch.error event.
*
* @param \Laminas\Mvc\MvcEvent $e
*/
public function onDispatchPost($e)
public function onDispatchPost(MvcEvent $e): ?ResponseInterface
{
$target = $e->getTarget();
if (! $target instanceof PageController) {
return;
return null;
}

$error = $e->getError();
if ($error != Application::ERROR_CONTROLLER_INVALID) {
return;
if ($error !== Application::ERROR_CONTROLLER_INVALID) {
return null;
}

$app = $e->getApplication();
Expand All @@ -120,16 +108,7 @@ public function onDispatchPost($e)
if ($return) {
$e->setResult($return);
}
}

/**
* Normalize a cache key
*
* @param string $key
* @return string
*/
public static function normalizeCacheKey($key)
{
return str_replace(['/', '\\', '.'], '_', $key);
return null;
}
}
40 changes: 40 additions & 0 deletions src/PageCacheFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* @link https://github.com/weierophinney/PhlySimplePage for the canonical source repository
* @copyright Copyright (c) 2012-2020 Matthew Weier O'Phinney (https://mwop.net)
* @license https://github.com/weierophinney/PhlySimplePage/blog/master/LICENSE.md New BSD License
*/

declare(strict_types=1);

namespace PhlySimplePage;

use Laminas\Cache\Storage\Adapter\AbstractAdapter;
use Laminas\Cache\StorageFactory;
use Laminas\ServiceManager\Exception;
use Psr\Container\ContainerInterface;

use function sprintf;

class PageCacheFactory
{
/**
* Create and return cache storage adapter
*
* @throws Exception\ServiceNotCreatedException
*/
public function __invoke(ContainerInterface $container): AbstractAdapter
{
$config = $container->get('config')['phly-simple-page'] ?? [];
if (! isset($config['cache'])) {
throw new Exception\ServiceNotCreatedException(sprintf(
'%s could not create a cache storage adapter, as the ["phly-simple-page"]["cache"]'
. ' key is missing',
self::class
));
}

return StorageFactory::factory($config['cache']);
}
}
Loading

0 comments on commit 04bfd7f

Please sign in to comment.