|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* See class comment |
|
4
|
|
|
* |
|
5
|
|
|
* PHP Version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Netresearch |
|
8
|
|
|
* @package Netresearch\Kite |
|
9
|
|
|
* @subpackage Task |
|
10
|
|
|
* @author Christian Opitz <[email protected]> |
|
11
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
|
12
|
|
|
* @link http://www.netresearch.de |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Netresearch\Kite\Task; |
|
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Execute a callback |
|
20
|
|
|
* |
|
21
|
|
|
* @category Netresearch |
|
22
|
|
|
* @package Netresearch\Kite |
|
23
|
|
|
* @subpackage Task |
|
24
|
|
|
* @author Christian Opitz <[email protected]> |
|
25
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
|
26
|
|
|
* @link http://www.netresearch.de |
|
27
|
|
|
*/ |
|
28
|
|
|
class CallbackTask extends \Netresearch\Kite\Task |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* Configures the options |
|
32
|
|
|
* |
|
33
|
|
|
* @return array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function configureVariables() |
|
36
|
|
|
{ |
|
37
|
|
|
return array( |
|
|
|
|
|
|
38
|
|
|
'callback' => array( |
|
39
|
|
|
'type' => 'callable|string', |
|
40
|
|
|
'required' => true, |
|
41
|
|
|
'label' => 'The callback or user function to run (@see GeneralUtility::callUserFunction())' |
|
42
|
|
|
), |
|
43
|
|
|
'--' |
|
44
|
|
|
) + parent::configureVariables(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Execute the task |
|
49
|
|
|
* |
|
50
|
|
|
* @return mixed |
|
51
|
|
|
*/ |
|
52
|
|
|
public function execute() |
|
53
|
|
|
{ |
|
54
|
|
|
$callback = $this->get('callback'); |
|
55
|
|
|
if (!is_array($callback) && !$callback instanceof \Closure) { |
|
56
|
|
|
if (strpos($callback, '::')) { |
|
57
|
|
|
$callback = explode('::', $callback); |
|
58
|
|
|
} elseif (strpos($callback, '->')) { |
|
59
|
|
|
$parts = explode('->', $callback); |
|
60
|
|
|
$instance = new $parts[0]; |
|
61
|
|
|
$callback = array($instance, $parts[1]); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
return call_user_func($callback, $this->getParent()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Show an information of what will happen |
|
69
|
|
|
* |
|
70
|
|
|
* @return void |
|
71
|
|
|
*/ |
|
72
|
|
|
public function preview() |
|
73
|
|
|
{ |
|
74
|
|
|
parent::preview(); |
|
75
|
|
|
$fn = $this->get('callback'); |
|
76
|
|
|
if (is_string($fn)) { |
|
77
|
|
|
$name = $fn; |
|
78
|
|
|
} elseif (is_array($fn)) { |
|
79
|
|
|
$name = (is_object($fn[0]) ? get_class($fn[0]) . '->' : $fn[0] . '::') . $fn[1]; |
|
80
|
|
|
} else { |
|
81
|
|
|
$name = 'anonymous function'; |
|
82
|
|
|
} |
|
83
|
|
|
$this->console->output('Calling ' . $name, OutputInterface::VERBOSITY_DEBUG); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
?> |
|
87
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.