This repository was archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathPatternPluginManagerTrait.php
More file actions
75 lines (69 loc) · 2.15 KB
/
PatternPluginManagerTrait.php
File metadata and controls
75 lines (69 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/**
* @see https://github.com/zendframework/zend-cache for the canonical source repository
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-cache/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Cache\PatternPluginManager;
use Zend\Cache\Exception;
use Zend\Cache\Pattern;
use Zend\ServiceManager\Exception\InvalidServiceException;
/**
* Trait providing common logic between PatternPluginManager implementations.
*
* Trait does not define properties, as the properties common between the
* two versions are originally defined in their parent class, causing a
* resolution conflict.
*/
trait PatternPluginManagerTrait
{
/**
* Override build to inject options as PatternOptions instance.
*
* {@inheritDoc}
*/
public function build($plugin, array $options = null)
{
if (empty($options)) {
return parent::build($plugin);
}
$plugin = parent::build($plugin);
$plugin->setOptions(new Pattern\PatternOptions($options));
return $plugin;
}
/**
* Validate the plugin is of the expected type (v3).
*
* Validates against `$instanceOf`.
*
* @param mixed $instance
* @throws InvalidServiceException
*/
public function validate($instance)
{
if (! $instance instanceof $this->instanceOf) {
throw new InvalidServiceException(sprintf(
'%s can only create instances of %s; %s is invalid',
get_class($this),
$this->instanceOf,
(is_object($instance) ? get_class($instance) : gettype($instance))
));
}
}
/**
* Validate the plugin is of the expected type (v2).
*
* Proxies to `validate()`.
*
* @param mixed $plugin
* @throws Exception\RuntimeException if invalid
*/
public function validatePlugin($plugin)
{
try {
$this->validate($plugin);
} catch (InvalidServiceException $e) {
throw new Exception\RuntimeException($e->getMessage(), $e->getCode(), $e);
}
}
}