1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* AppserverIo\Storage\StorageTrait |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link http://github.com/appserver-io/storage |
18
|
|
|
* @link http://www.appserver.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\Storage; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* A trait implementation providing basic storage functionality. |
25
|
|
|
* |
26
|
|
|
* @author Tim Wagner <[email protected]> |
27
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
28
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
29
|
|
|
* @link http://github.com/appserver-io/storage |
30
|
|
|
* @link http://www.appserver.io |
31
|
|
|
*/ |
32
|
|
|
trait StorageTrait |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* (non-PHPdoc) |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
* @see \AppserverIo\Storage\StorageInterface::flush() |
40
|
|
|
*/ |
41
|
|
|
public function flush() |
42
|
|
|
{ |
43
|
|
|
if ($allKeys = $this->getAllKeys()) { |
|
|
|
|
44
|
|
|
foreach ($allKeys as $key) { |
45
|
|
|
$this->remove($key); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* (non-PHPdoc) |
52
|
|
|
* |
53
|
|
|
* @param string $tag The tag the entries must have |
54
|
|
|
* |
55
|
|
|
* @return void |
56
|
|
|
* @see \AppserverIo\Storage\StorageInterface::flushByTag() |
57
|
|
|
*/ |
58
|
|
|
public function flushByTag($tag) |
59
|
|
|
{ |
60
|
|
|
$tagData = $this->get($tag); |
|
|
|
|
61
|
|
|
if (is_array($tagData)) { |
62
|
|
|
foreach ($tagData as $cacheKey) { |
63
|
|
|
$this->remove($cacheKey); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
$this->remove($tag); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* (non-PHPdoc) |
71
|
|
|
* |
72
|
|
|
* @param string $tag A tag to be checked for validity |
73
|
|
|
* |
74
|
|
|
* @return boolean |
75
|
|
|
* @see \AppserverIo\Storage\StorageInterface::isValidTag() |
76
|
|
|
*/ |
77
|
|
|
public function isValidTag($tag) |
78
|
|
|
{ |
79
|
|
|
return $this->isValidEntryIdentifier($tag); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* (non-PHPdoc) |
84
|
|
|
* |
85
|
|
|
* @param string $identifier An identifier to be checked for validity |
86
|
|
|
* |
87
|
|
|
* @return boolean |
88
|
|
|
* @see \AppserverIo\Storage\StorageInterface::isValidEntryIdentifier() |
89
|
|
|
*/ |
90
|
|
|
public function isValidEntryIdentifier($identifier) |
91
|
|
|
{ |
92
|
|
|
if (preg_match('^[0-9A-Za-z_]+$', $identifier) === 1) { |
93
|
|
|
return true; |
94
|
|
|
} |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.