|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Fwk |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2011-2012, Julien Ballestracci <[email protected]>. |
|
6
|
|
|
* All rights reserved. |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
12
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
13
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|
14
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|
15
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
16
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
17
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
18
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
19
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
20
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|
21
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
22
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
|
23
|
|
|
* |
|
24
|
|
|
* PHP Version 5.3 |
|
25
|
|
|
* |
|
26
|
|
|
* @category Database |
|
27
|
|
|
* @package Fwk\Db |
|
28
|
|
|
* @subpackage Workers |
|
29
|
|
|
* @author Julien Ballestracci <[email protected]> |
|
30
|
|
|
* @copyright 2011-2012 Julien Ballestracci <[email protected]> |
|
31
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
32
|
|
|
* @link http://www.phpfwk.com |
|
33
|
|
|
*/ |
|
34
|
|
|
namespace Fwk\Db\Workers; |
|
35
|
|
|
|
|
36
|
|
|
use Fwk\Db\Registry\Registry; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Base utility class for Workers |
|
40
|
|
|
* |
|
41
|
|
|
* @category Workers |
|
42
|
|
|
* @package Fwk\Db |
|
43
|
|
|
* @author Julien Ballestracci <[email protected]> |
|
44
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
45
|
|
|
* @link http://www.phpfwk.com |
|
46
|
|
|
*/ |
|
47
|
|
|
abstract class AbstractWorker |
|
48
|
|
|
{ |
|
49
|
|
|
/** |
|
50
|
|
|
* Object to delete |
|
51
|
|
|
* |
|
52
|
|
|
* @var mixed |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $entity; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Defined Registry to work with |
|
58
|
|
|
* |
|
59
|
|
|
* @var Registry |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $registry; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Prevent many workers to work on the same entity |
|
65
|
|
|
* |
|
66
|
|
|
* @var array |
|
67
|
|
|
*/ |
|
68
|
|
|
protected static $working = array(); |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Constructor |
|
72
|
|
|
* |
|
73
|
|
|
* @param mixed $entity The object involved with this worker |
|
74
|
|
|
* |
|
75
|
|
|
* @return void |
|
|
|
|
|
|
76
|
|
|
*/ |
|
77
|
|
|
public function __construct($entity) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->entity = $entity; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Return the registry to be used by this worker |
|
84
|
|
|
* |
|
85
|
|
|
* @return Registry |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getRegistry() |
|
88
|
|
|
{ |
|
89
|
|
|
|
|
90
|
|
|
return $this->registry; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Defines the Registry to work with. |
|
95
|
|
|
* |
|
96
|
|
|
* @param Registry $registry The registry to be used by this worker |
|
97
|
|
|
* |
|
98
|
|
|
* @return void |
|
99
|
|
|
*/ |
|
100
|
|
|
public function setRegistry(Registry $registry) |
|
101
|
|
|
{ |
|
102
|
|
|
$this->registry = $registry; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Return the defined entity |
|
107
|
|
|
* |
|
108
|
|
|
* @return mixed |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getEntity() |
|
111
|
|
|
{ |
|
112
|
|
|
|
|
113
|
|
|
return $this->entity; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Unmark the entity from the working scope |
|
118
|
|
|
* |
|
119
|
|
|
* @param object $entity The entity |
|
120
|
|
|
* |
|
121
|
|
|
* @return void |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function removeFromWorking($entity) |
|
124
|
|
|
{ |
|
125
|
|
|
$id = array_search($entity, static::$working, true); |
|
126
|
|
|
if ($id !== false) { |
|
127
|
|
|
unset(static::$working[$id]); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
} |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.