Skip to content

Commit

Permalink
feat: revert: don't delete old classes but just deprecate them
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Rieschl <thomas@trinet.at>
  • Loading branch information
rieschl committed Oct 23, 2020
1 parent c0200b1 commit 89ec686
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/Validator/AbstractValidatorChainEM2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* @see https://github.com/laminas/laminas-session for the canonical source repository
* @copyright https://github.com/laminas/laminas-session/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-session/blob/master/LICENSE.md New BSD License
*/

namespace Laminas\Session\Validator;

use Laminas\EventManager\EventManager;
use Laminas\Session\Storage\StorageInterface;
use Laminas\Stdlib\CallbackHandler;

/**
* Abstract validator chain for validating sessions (for use with laminas-eventmanager v2).
*
* @deprecated laminas-eventmanager v2 is not supported anymore. Use {@see \Laminas\Session\ValidatorChain} directly
*/
abstract class AbstractValidatorChainEM2 extends EventManager
{
use ValidatorChainTrait;

/**
* Construct the validation chain
*
* Retrieves validators from session storage and attaches them.
*
* Duplicated in ValidatorChainEM3 to prevent trait collision with parent.
*
* @param StorageInterface $storage
*/
public function __construct(StorageInterface $storage)
{
parent::__construct();

$this->storage = $storage;
$validators = $storage->getMetadata('_VALID');
if ($validators) {
foreach ($validators as $validator => $data) {
$this->attachValidator('session.validate', [new $validator($data), 'isValid'], 1);
}
}
}

/**
* Attach a listener to the session validator chain.
*
* @param string $event
* @param null|callable $callback
* @param int $priority
* @return CallbackHandler
*/
public function attach($event, $callback = null, $priority = 1)
{
return $this->attachValidator($event, $callback, $priority);
}
}
58 changes: 58 additions & 0 deletions src/Validator/AbstractValidatorChainEM3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* @see https://github.com/laminas/laminas-session for the canonical source repository
* @copyright https://github.com/laminas/laminas-session/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-session/blob/master/LICENSE.md New BSD License
*/

namespace Laminas\Session\Validator;

use Laminas\EventManager\EventManager;
use Laminas\Session\Storage\StorageInterface;
use Laminas\Stdlib\CallbackHandler;

/**
* Abstract validator chain for validating sessions (for use with laminas-eventmanager v3)
*
* @deprecated Use {@see \Laminas\Session\ValidatorChain} directly
*/
abstract class AbstractValidatorChainEM3 extends EventManager
{
use ValidatorChainTrait;

/**
* Construct the validation chain
*
* Retrieves validators from session storage and attaches them.
*
* Duplicated in ValidatorChainEM2 to prevent trait collision with parent.
*
* @param StorageInterface $storage
*/
public function __construct(StorageInterface $storage)
{
parent::__construct();

$this->storage = $storage;
$validators = $storage->getMetadata('_VALID');
if ($validators) {
foreach ($validators as $validator => $data) {
$this->attachValidator('session.validate', [new $validator($data), 'isValid'], 1);
}
}
}

/**
* Attach a listener to the session validator chain.
*
* @param string $eventName
* @param callable $callback
* @param int $priority
* @return CallbackHandler
*/
public function attach($eventName, callable $callback, $priority = 1)
{
return $this->attachValidator($eventName, $callback, $priority);
}
}
66 changes: 66 additions & 0 deletions src/Validator/ValidatorChainTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/**
* @see https://github.com/laminas/laminas-session for the canonical source repository
* @copyright https://github.com/laminas/laminas-session/blob/master/COPYRIGHT.md
* @license https://github.com/laminas/laminas-session/blob/master/LICENSE.md New BSD License
*/

namespace Laminas\Session\Validator;

use Laminas\Session\Storage\StorageInterface;
use Laminas\Stdlib\CallbackHandler;

/**
* Base trait for validator chain implementations
*
* @deprecated Use {@see \Laminas\Session\ValidatorChain} directly
*/
trait ValidatorChainTrait
{
/**
* @var StorageInterface
*/
protected $storage;

/**
* Retrieve session storage object
*
* @return StorageInterface
*/
public function getStorage()
{
return $this->storage;
}

/**
* Internal implementation for attaching a listener to the
* session validator chain.
*
* @param string $event
* @param callable $callback
* @param int $priority
* @return CallbackHandler|callable
*/
private function attachValidator($event, $callback, $priority)
{
$context = null;
if ($callback instanceof ValidatorInterface) {
$context = $callback;
} elseif (is_array($callback)) {
$test = array_shift($callback);
if ($test instanceof ValidatorInterface) {
$context = $test;
}
array_unshift($callback, $test);
}
if ($context instanceof ValidatorInterface) {
$data = $context->getData();
$name = $context->getName();
$this->getStorage()->setMetadata('_VALID', [$name => $data]);
}

$listener = parent::attach($event, $callback, $priority);
return $listener;
}
}

0 comments on commit 89ec686

Please sign in to comment.