Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a PHP 8 attribute to support configuration #39

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 4 additions & 27 deletions src/Caching/Annotation/Send304IfNotModified.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,16 @@

namespace Webfactory\Bundle\WfdMetaBundle\Caching\Annotation;

use Exception;
use Webfactory\Bundle\WfdMetaBundle\Helper\LastmodHelper;
use Webfactory\Bundle\WfdMetaBundle\MetaQueryFactory;

@trigger_error(
'The Send304IfNotModified annotation is deprecated. Use WebfactoryHttpCachingBundle and its LastModifiedDeterminators instead. If in a hurry, @see \Webfactory\Bundle\WfdMetaBundle\Caching\WfdMetaQueries for a quick conversion.',
\E_USER_DEPRECATED
);

/**
* @Annotation
*
* @deprecated Use WebfactoryHttpCachingBundle and its LastModifiedDeterminators instead. If in a hurry, @see \Webfactory\Bundle\WfdMetaBundle\Caching\WfdMetaQueries for a quick conversion.
* @deprecated Use the \Webfactory\Bundle\WfdMetaBundle\Caching\Attribute\Send304IfNotModified attribute instead
*/
class Send304IfNotModified
class Send304IfNotModified extends \Webfactory\Bundle\WfdMetaBundle\Caching\Attribute\Send304IfNotModified
{
protected $lastmodHelper;

public function __construct($values)
{
$this->lastmodHelper = new LastmodHelper();

foreach ($values as $key => $value) {
if (method_exists($this->lastmodHelper, $name = 'set'.ucfirst($key))) {
$this->lastmodHelper->$name($value);
} else {
throw new Exception('Die Annotation '.static::class.' kann die Eigentschaft "'.$key.'" nicht setzen.');
}
}
}

public function calculateLastModified(MetaQueryFactory $metaQueryFactory)
{
return $this->lastmodHelper->calculateLastModified($metaQueryFactory);
@trigger_error(sprintf('The %s annotation is deprecated, use the %s attribute instead', __CLASS__, \Webfactory\Bundle\WfdMetaBundle\Caching\Attribute\Send304IfNotModified::class), \E_USER_DEPRECATED);
parent::__construct(...$values);
}
}
10 changes: 3 additions & 7 deletions src/Caching/Annotation/ValidUntilLastTouched.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@
/**
* @Annotation
*
* @Deprecated
* @deprecated Use the \Webfactory\Bundle\WfdMetaBundle\Caching\Attribute\Send304IfNotModified attribute instead
*/
class ValidUntilLastTouched extends Send304IfNotModified
{
public function __construct($values)
{
@trigger_error(
'The ValidUntilLastTouched annotation is deprecated. Use Send304IfNotModified instead.',
\E_USER_DEPRECATED
);

parent::__construct($values);
@trigger_error(sprintf('The %s annotation is deprecated, use the %s attribute instead', __CLASS__, \Webfactory\Bundle\WfdMetaBundle\Caching\Attribute\Send304IfNotModified::class), \E_USER_DEPRECATED);
\Webfactory\Bundle\WfdMetaBundle\Caching\Attribute\Send304IfNotModified::__construct(...$values);
}
}
46 changes: 46 additions & 0 deletions src/Caching/Attribute/Send304IfNotModified.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/*
* (c) webfactory GmbH <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Webfactory\Bundle\WfdMetaBundle\Caching\Attribute;

use Attribute;
use Exception;
use Webfactory\Bundle\WfdMetaBundle\Helper\LastmodHelper;
use Webfactory\Bundle\WfdMetaBundle\MetaQueryFactory;

/**
* @deprecated Use WebfactoryHttpCachingBundle and its LastModifiedDeterminators instead. If in a hurry, @see \Webfactory\Bundle\WfdMetaBundle\Caching\WfdMetaQueries for a quick conversion.
*/
#[Attribute]
class Send304IfNotModified
{
protected $lastmodHelper;

public function __construct(...$values)
{
@trigger_error(
'The Send304IfNotModified attribute is deprecated. Use WebfactoryHttpCachingBundle and its LastModifiedDeterminators instead. If in a hurry, @see \Webfactory\Bundle\WfdMetaBundle\Caching\WfdMetaQueries for a quick conversion.',
\E_USER_DEPRECATED
);

$this->lastmodHelper = new LastmodHelper();

foreach ($values as $key => $value) {
if (method_exists($this->lastmodHelper, $name = 'set'.ucfirst($key))) {
$this->lastmodHelper->$name($value);
} else {
throw new Exception('Die Annotation '.static::class.' kann die Eigentschaft "'.$key.'" nicht setzen.');
}
}
}

public function calculateLastModified(MetaQueryFactory $metaQueryFactory)
{
return $this->lastmodHelper->calculateLastModified($metaQueryFactory);
}
}
38 changes: 25 additions & 13 deletions src/Caching/EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ public function onKernelController(ControllerEvent $event)
$controller = $event->getController();
$request = $event->getRequest();

$annotation = $this->findAnnotation($controller);
$attribute = $this->findAttribute($controller);

if (!$annotation) {
if (!$attribute) {
return;
}

$lastTouched = $annotation->calculateLastModified($this->metaQueryFactory);
$lastTouched = $attribute->calculateLastModified($this->metaQueryFactory);

if (!$lastTouched) {
return;
Expand Down Expand Up @@ -94,18 +94,30 @@ public function onKernelResponse(ResponseEvent $event)
/**
* @param $callback array A PHP callback (array) pointing to the method to reflect on.
*
* @return Send304IfNotModified|null The annotation, if found. Null otherwise.
* @return Send304IfNotModified|null The attribute, if found. Null otherwise.
*/
protected function findAnnotation($callback)
protected function findAttribute($callback): ?Attribute\Send304IfNotModified
{
if (\is_array($callback)) {
$object = new ReflectionObject($callback[0]);
$method = $object->getMethod($callback[1]);

foreach ($this->reader->getMethodAnnotations($method) as $configuration) {
if ($configuration instanceof Send304IfNotModified) {
return $configuration;
}
if (!\is_array($callback)) {
return null;
}

$object = new ReflectionObject($callback[0]);
$method = $object->getMethod($callback[1]);

if (\PHP_MAJOR_VERSION >= 8) {
$attributes = $method->getAttributes(\Webfactory\Bundle\WfdMetaBundle\Caching\Attribute\Send304IfNotModified::class);

if ($attributes) {
return $attributes[0];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FIX: new instance!

}
}

foreach ($this->reader->getMethodAnnotations($method) as $configuration) {
if ($configuration instanceof Send304IfNotModified) {
@trigger_error(sprintf('Using annotations to configure wfd_meta based caching on %s::%s is deprecated, use attribute-based configuration instead.', $method->class, $method->name), \E_USER_DEPRECATED);

return $configuration;
}
}

Expand Down