1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2006 - 2013 Eric D. Hough (http://ehough.com) |
4
|
|
|
* |
5
|
|
|
* This file is part of chaingang (https://github.com/ehough/chaingang) |
6
|
|
|
* |
7
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public |
8
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this |
9
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* This class is heavily based on the Commons Chain project. |
14
|
|
|
* http://commons.apache.org/chain |
15
|
|
|
* |
16
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
17
|
|
|
* contributor license agreements. See the NOTICE file distributed with |
18
|
|
|
* this work for additional information regarding copyright ownership. |
19
|
|
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
20
|
|
|
* (the "License"); you may not use this file except in compliance with |
21
|
|
|
* the License. You may obtain a copy of the License at |
22
|
|
|
* |
23
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
24
|
|
|
* |
25
|
|
|
* Unless required by applicable law or agreed to in writing, software |
26
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
27
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
28
|
|
|
* See the License for the specific language governing permissions and |
29
|
|
|
* limitations under the License. |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* A Chain represents a configured list of Commands that will be executed in order to |
34
|
|
|
* perform processing on a specified Context. Each included Command will be executed |
35
|
|
|
* in turn, until either one of them returns true, one of the executed Commands throws |
36
|
|
|
* an exception, or the end of the chain has been reached. The Chain itself will return |
37
|
|
|
* the return value of the last Command that was executed (if no exception was thrown), |
38
|
|
|
* or rethrow the thrown exception. |
39
|
|
|
* |
40
|
|
|
* Note that Chain extends Command, so that the two can be used interchangeably when |
41
|
|
|
* a Command is expected. This makes it easy to assemble workflows in a hierarchical |
42
|
|
|
* manner by combining subchains into an overall processing chain. |
43
|
|
|
*/ |
44
|
|
|
class ehough_chaingang_impl_StandardChain implements ehough_chaingang_api_Chain |
45
|
|
|
{ |
46
|
|
|
private $_commands = array(); |
47
|
|
|
|
48
|
|
|
private $_frozen = false; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Add a command to this chain. |
52
|
|
|
* |
53
|
|
|
* Add a Command to the list of Commands that will be called in turn when this Chain's |
54
|
|
|
* execute() method is called. Once execute() has been called at least once, it is no |
55
|
|
|
* longer possible to add additional Commands; instead, an exception will be thrown. |
56
|
|
|
* |
57
|
|
|
* @param ehough_chaingang_api_Command $command The Command to be added. |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
* |
61
|
|
|
* @throws ehough_chaingang_api_exception_IllegalStateException If this Chain has already |
62
|
|
|
* been executed at least once, |
63
|
|
|
* so no further configuration is allowed. |
64
|
|
|
*/ |
65
|
1 |
|
public final function addCommand(ehough_chaingang_api_Command $command) |
66
|
|
|
{ |
67
|
1 |
|
if ($this->_frozen) { |
68
|
|
|
|
69
|
|
|
throw new ehough_chaingang_api_exception_IllegalStateException('Chain is frozen.'); |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
array_push($this->_commands, $command); |
73
|
1 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Execute a unit of processing work to be performed. |
77
|
|
|
* |
78
|
|
|
* This Command may either complete the required processing and return true, |
79
|
|
|
* or delegate remaining processing to the next Command in a Chain containing |
80
|
|
|
* this Command by returning false. |
81
|
|
|
* |
82
|
|
|
* @param ehough_chaingang_api_Context $context The Context to be processed by this Command. |
83
|
|
|
* |
84
|
|
|
* @return boolean True if the processing of this Context has been completed, or false if the |
85
|
|
|
* processing of this Context should be delegated to a subsequent Command |
86
|
|
|
* in an enclosing Chain. |
87
|
|
|
*/ |
88
|
1 |
|
public final function execute(ehough_chaingang_api_Context $context) |
89
|
|
|
{ |
90
|
1 |
|
$this->_frozen = true; |
91
|
|
|
|
92
|
1 |
|
$handled = false; |
93
|
|
|
|
94
|
1 |
|
foreach ($this->_commands as $command) { |
95
|
|
|
|
96
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
97
|
1 |
|
$handled = $command->execute($context); |
98
|
|
|
|
99
|
1 |
|
if ($handled) { |
100
|
|
|
|
101
|
1 |
|
break; |
102
|
|
|
} |
103
|
1 |
|
} |
104
|
|
|
|
105
|
1 |
|
return $handled; |
106
|
|
|
} |
107
|
|
|
} |