|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* AppserverIo\Resources\Interfaces\Resources |
|
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 2018 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/appserver-io/resources |
|
18
|
|
|
* @link http://www.appserver.io |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace AppserverIo\Resources; |
|
22
|
|
|
|
|
23
|
|
|
use AppserverIo\Lang\String; |
|
24
|
|
|
use AppserverIo\Collections\HashMap; |
|
25
|
|
|
use AppserverIo\Resources\Interfaces\ResourcesFactoryInterface; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* This class bundles several resource files to a so called resource bundle. |
|
29
|
|
|
* |
|
30
|
|
|
* It also provides several functions to handle resource files, like an import/export functionality. |
|
31
|
|
|
* |
|
32
|
|
|
* @author Tim Wagner <[email protected]> |
|
33
|
|
|
* @copyright 2018 TechDivision GmbH <[email protected]> |
|
34
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
35
|
|
|
* @link https://github.com/appserver-io/resources |
|
36
|
|
|
* @link http://www.appserver.io |
|
37
|
|
|
*/ |
|
38
|
|
|
abstract class AbstractResourcesFactory implements ResourcesFactoryInterface |
|
39
|
|
|
{ |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Return the returnNull property value that will be configured on Resources instances created by this factory. |
|
43
|
|
|
* |
|
44
|
|
|
* @var boolean |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $returnNull = true; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* This HashMap holds the initialized Resources instances. |
|
50
|
|
|
* |
|
51
|
|
|
* @var \AppserverIo\Collections\HashMap |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $resources = null; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* The constructor initializes the internal members necessary for handling the initialized resources. |
|
57
|
|
|
* |
|
58
|
|
|
* @return void |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct() |
|
61
|
|
|
{ |
|
62
|
|
|
$this->resources = new HashMap(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Create and return a new resources instance with the specified logical name, after calling its init() |
|
67
|
|
|
* method and delegating the relevant properties. Concrete subclasses MUST implement this method. |
|
68
|
|
|
* |
|
69
|
|
|
* @param \AppserverIo\Lang\String $name Holds the logical name of the Resources to create |
|
70
|
|
|
* @param \AppserverIo\Lang\String $config Holds the optional string to the configuration |
|
71
|
|
|
* |
|
72
|
|
|
* @return \AppserverIo\Resources\Interfaces\ResourcesInterface Holds the initialized resources |
|
73
|
|
|
*/ |
|
74
|
|
|
abstract protected function createResources(String $name, String $config = null); |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Create (if necessary) and return a Resources instance for the specified logical name, with a |
|
78
|
|
|
* configuration based on the specified configuration string. |
|
79
|
|
|
* |
|
80
|
|
|
* @param \AppserverIo\Lang\String $name The name of the resources |
|
81
|
|
|
* @param \AppserverIo\Lang\String $config The path to the configuration file |
|
82
|
|
|
* |
|
83
|
|
|
* @return \AppserverIo\Resources\Interfaces\ResourcesInterface The Resources instance |
|
84
|
|
|
* @throws \AppserverIo\Resources\Exceptions\ResourcesException Is thrown if an exception occurs when the Resources are initialized |
|
85
|
|
|
* @see \AppserverIo\Resources\Interfaces\ResourcesFactoryInterface::getResources() |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getResources(String $name, String $config = null) |
|
88
|
|
|
{ |
|
89
|
|
|
|
|
90
|
|
|
// check if the resources already exists |
|
91
|
|
|
if (!$this->resources->exists($name)) { |
|
92
|
|
|
// if not, initialize them |
|
93
|
|
|
$this->resources->add($name, $this->createResources($name, $config)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// return the requested resources |
|
97
|
|
|
return $this->resources->get($name); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Return the returnNull property value that will be configured on Resources instances created |
|
102
|
|
|
* by this factory. |
|
103
|
|
|
* |
|
104
|
|
|
* @return boolean TRUE if null is returned for invalid key values. |
|
105
|
|
|
* @see \AppserverIo\Resources\Interfaces\ResourcesFactoryInterface::isReturnNull() |
|
106
|
|
|
*/ |
|
107
|
|
|
public function isReturnNull() |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->returnNull; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Set the returnNull property value that will be configured on Resources instances created |
|
114
|
|
|
* by this factory. |
|
115
|
|
|
* |
|
116
|
|
|
* @param boolean $returnNull The new value to delegate |
|
117
|
|
|
* |
|
118
|
|
|
* @return void |
|
119
|
|
|
* @see \AppserverIo\Resources\Interfaces\ResourcesFactoryInterface::setReturnNull( $returnNull) |
|
120
|
|
|
*/ |
|
121
|
|
|
public function setReturnNull($returnNull) |
|
122
|
|
|
{ |
|
123
|
|
|
$this->returnNull = $returnNull; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Release any internal references to Resources instances that have been returned previously, |
|
128
|
|
|
* after calling the destroy() method on each such instance. |
|
129
|
|
|
* |
|
130
|
|
|
* @return void |
|
131
|
|
|
* @see \AppserverIo\Resources\Interfaces\ResourcesFactoryInterface::release() |
|
132
|
|
|
*/ |
|
133
|
|
|
public function release() |
|
134
|
|
|
{ |
|
135
|
|
|
|
|
136
|
|
|
// invoke the destroy method on each instace and delete it from the internal array |
|
137
|
|
|
foreach ($this->resources as $name => $resources) { |
|
138
|
|
|
// invoke the destroy method of the resources |
|
139
|
|
|
$resources->destroy(); |
|
140
|
|
|
// remove the resources from the internal HashMap |
|
141
|
|
|
$this->resources->remove($name); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|