1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
<?php
namespace oroboros\core\traits\patterns\structural;
Trait ControlApi {
private $_control_methods = array();
private $_api = array();
public function api($index = null, $action = null) {
if (!isset($index) && !isset($action)) {
$api = array();
foreach ($this->_control_methods as $control => $params) {
$methods = preg_grep('/^_' . $control . '/', get_class_methods($this));
foreach ($methods as $method) {
$id = $this->_reverseParseControlMethodName($control, $method);
$api[$id] = $this->_getControlMethodApi($method);
}
}
} elseif (!array_key_exists($index, $this->_control_methods)) {
return false;
} elseif (isset($index) && !isset($action)) {
$api = array();
$methods = preg_grep('/^_' . $index . '/', get_class_methods($this));
foreach ($methods as $method) {
$id = $this->_reverseParseControlMethodName($index, $method);
$api[$id] = $this->_getControlMethodApi($method);
}
} else {
$api = $this->_getControlMethodApi(
$this->_constructControlWorkerMethodName(
$index, $action
)
);
}
return $api;
}
public function __call($name, $args = array()) {
if (!array_key_exists($name, $this->_control_methods)) {
throw new \oroboros\lib\exception\LibraryException(
'unknown method: [' . $name . ']. Call api() to retrieve '
. 'valid control methods and their subset of actions. '
. 'Thrown at: ' . __LINE__ . ' of ' . __TRAIT__, \oroboros\interfaces\utility\ExceptionCodeInterface::EXCEPTION_CODE_BAD_PARAMETERS);
}
$method = $this->_constructControlWorkerMethodName($name, ((is_null($args) || ( is_array($args) && empty($args))) ? null : ((is_string($args)) ? $args : ((is_array($args)) ? array_shift($args) : null) ))
);
if (!method_exists($this, $method)) {
throw new \oroboros\lib\exception\LibraryException(
'unknown method: [' . $method . ']. in instance of [' . get_class($this) . ']. '
. 'Thrown at: ' . __LINE__ . ' of ' . __TRAIT__, \oroboros\interfaces\utility\ExceptionCodeInterface::EXCEPTION_CODE_NOT_IMPLEMENTED
);
}
$command = (is_array($args) ? array_shift($args) : null);
$arguments = (is_array($args) ? array_shift($args) : null);
$flags = (is_array($args) ? array_shift($args) : null);
return $this->_runControlMethod($method, $command, $arguments, $flags, $args, ((is_array($args)) ? count($args) : 0));
}
protected function _registerControlMethodSet($method) {
if (!is_string($method) || !preg_match('/[a-z]/', $method)) {
throw new \oroboros\lib\exception\LibraryException(
'Invalid control method identifier. '
. 'Thrown at: ' . __LINE__ . ' of ' . __TRAIT__, \oroboros\interfaces\utility\ExceptionCodeInterface::EXCEPTION_CODE_BAD_PARAMETERS);
}
if (!method_exists($this, $this->_constructControlWorkerMethodName($method))) {
throw new \oroboros\lib\exception\LibraryException(
'Cannot register [' . $method . '] because the default method [_'
. $method . '()] does not exist in the scope of class: ['
. get_class($this) . ']. '
. 'Thrown at: ' . __LINE__ . ' of ' . __TRAIT__, \oroboros\interfaces\utility\ExceptionCodeInterface::EXCEPTION_CODE_NOT_IMPLEMENTED);
}
echo 'Registering [' . $method . ']';
if (!in_array($method, $this->_control_methods)) {
$this->_control_methods[$method] = array();
}
return true;
}
private function _loadMethod($name, $method = null, $args = null, $flags = null) {
$worker = '_' . strtolower($name)
. str_replace(' ', null, ucwords(
str_replace('-', ' ', strtolower($method))));
return call_user_func_array($this->_control_methods[$worker], $args);
}
private function _constructControlWorkerMethodName($control, $method = null) {
if (!isset($method)) {
return '_' . strtolower($control);
} else {
return '_' . strtolower($control)
. str_replace(' ', null, ucwords(
str_replace('-', ' ', strtolower($method))));
}
}
private function _runControlMethod($method, $command, $arguments, $flags, $args, $arg_count) {
if (is_null($command) && is_null($arguments) && is_null($flags)) {
return $this->$method();
} elseif (!is_null($command) && is_null($arguments) && is_null($flags)) {
return $this->$method($command);
} elseif (!is_null($command) && !is_null($arguments) && is_null($flags)) {
return $this->$method($command, $arguments);
} elseif (!is_null($command) && !is_null($arguments) && !is_null($flags) && $arg_count === 0) {
return $this->$method($command, $arguments, $flags);
} else {
return $this->$method($command, $arguments, $flags, $args);
}
}
private function _getControlMethodApi($method) {
if (!method_exists($this, $method)) {
return 'Method does not exist in this scope.';
}
$reflector = new \ReflectionMethod($this, $method);
$docblock = $reflector->getDocComment();
if (!$docblock) {
return 'No api provided.';
}
$explode = explode(PHP_EOL, $docblock);
foreach ($explode as $key => $line) {
$explode[$key] = $line = ltrim($line, '/* ');
if ($line == '') {
unset($explode[$key]);
}
}
return implode(PHP_EOL, $explode);
}
private function _reverseParseControlMethodName($index, $method = null) {
$raw = str_replace($this->_constructControlWorkerMethodName($index), null, $method);
if ($raw === '') {
d('__default');
return '__default';
}
return strtolower(implode('-', $this->_splitAtUpperCase($raw)));
}
private function _splitAtUpperCase($s) {
return preg_split('/(?=[A-Z])/', $s, -1, PREG_SPLIT_NO_EMPTY);
}
}