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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
<?php
/*
* The MIT License
*
* @author Brian Dayhoff <mopsyd@me.com>
* @copyright (c) 2017, Brian Dayhoff <mopsyd@me.com> all rights reserved.
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
namespace oroboros\core\abstracts\libraries\hooks;
/**
* Abstract Hook
* Defines a set of methods for identifying
* and replacing hooks within supplied data.
* @author Brian Dayhoff <mopsyd@me.com>
*/
abstract class AbstractHookParser
extends \oroboros\core\abstracts\libraries\AbstractLibrary
{
//This is what we are using to store our valid set of known hooks.
use \oroboros\core\traits\patterns\behavioral\Registry;
const CLASS_SCOPE = \oroboros\core\interfaces\api\ClassScopeApi::CLASS_SCOPE_ABSTRACT_PARSER;
const API = '\\oroboros\\core\\interfaces\\api\\HookApi';
/**
* <Hook Prefix>
* Overrideable way of designating the prefix of a valid hook
*/
const HOOK_PREFIX = '::';
/**
* <Hook Suffix>
* Overrideable way of designating the suffix of a valid hook
*/
const HOOK_SUFFIX = '::';
/**
* <Hook Debug Prefix>
* If the debug flag is passed,
* parsed output will prefix the hooked content with this value,
* applying sprintf to it with the parameters $hook and $current_class
*/
const HOOK_DEBUG_PREFIX = '\r\n[HOOK_DEBUG: start_hook[hook: _%s_][class: _%s_ ]\r\n';
/**
* <Hook Debug Suffix>
* If the debug flag is passed,
* parsed output will suffix the hooked content with this value,
* applying sprintf to it with the parameters $hook and $current_class
*/
const HOOK_DEBUG_SUFFIX = '\r\n[/hook: _%s_][class: _%s_] /HOOK_DEBUG]\r\n';
/**
* <Content Implode Delimiter>
* Overrideable way of designating the implode delimiter
* for content that is an array.
*
* By default, uses line breaks.
* This only applies if the content is an array or an iterable object.
*/
const CONTENCATION_DELIMITER = PHP_EOL;
private $_content = null;
private $_isParsed = false;
private $_debug_enabled = false;
private $_use_hook_debug_override_methods = false;
/**
* <Public Api Methods>
*/
public function __construct(array $params = array(), array $flags = array()) {
parent::__construct($params, $flags);
}
/**
* <Hook Parser Initialization Method>
* @param array $params
* @param array $flags
*/
public function initialize(array $params = array(), array $flags = array()) {
parent::initialize($params, $flags);
}
public function __toString() {
return $this->getContent();
}
public function registerHook($hook, $result = null) {
}
public function unregisterHook($hook) {
}
public function getHookValue($hook) {
}
public function getIndices($hook) {
}
public function setContent($content) {
}
public function getRawContent() {
}
/**
* Publicly accessible parse method.
* Prevents child classes from tampering with the baseline parse operation.
* @return void
*/
public function parse() {
return $this->_parse();
}
/**
* Publicly accessible reset method.
* Prevents child classes from tampering with the baseline reset operation.
* @return void
*/
public function reset() {
return $this->_reset();
}
public function resetContent() {
}
public function resetHooks() {
}
public function getContent() {
}
/**
* <Overrideable protected methods>
*/
/**
* <Hook Debug Prefix Override>
* If you need a more granular means of generating a debug prefix
* than a simple string, override this method.
*
* If a simple string is acceptable,
* override the class constant: HOOK_DEBUG_PREFIX
* instead.
*
* @return string
*/
protected function _hookOverridePrefix() {
$class = get_called_class();
return (string) $class::HOOK_DEBUG_PREFIX;
}
/**
* <Hook Debug Suffix Override>
* If you need a more granular means of generating a debug suffix
* than a simple string, override this method.
*
* If a simple string is acceptable,
* override the class constant: HOOK_DEBUG_SUFFIX
* instead.
*
* @return string
*/
protected function _hookDebugOverrideSuffix() {
$class = get_called_class();
return (string) $class::HOOK_DEBUG_SUFFIX;
}
/**
* <Internal Methods>
* Under the hood stuff.
*/
private function _getHookIndices($content, $hook) {
}
private function _getHookDebugInformation($hook) {
}
private function _validateHook($hook) {
}
private function _replaceHook($content, $value = null) {
}
private function _getHookCallbackContent($callable) {
}
/**
* Returns the baseline raw content.
* @return string;
*/
private function _getRawContent() {
$content = $this->_content;
//null content returns an empty string.
if (is_null($content)) {
return '';
} elseif (is_array($content) || (is_object($content) && $this->_isIterableObject($content))) {
}
}
/**
* Performs the baseline parse operation.
* @return void
*/
private function _parse() {
}
/**
* Performs the baseline reset operation.
* @return void
*/
private function _reset() {
$this->_resetContent();
$this->_resetHooks();
}
private function _resetContent() {
}
private function _resetHooks() {
}
/**
* Safely generates content recursively without allowing errors to arise.
* Will throw an exception if it cannot proceed without errors,
* and does not make any assumption that partial content is acceptable.
*
* If supplied content is multidimensional,
* all leaf nodes must be able to be cast to string
* or an exception will arise.
*
* This method uses recursion if given multidimensional content.
* Large content complexity may cause performance to hang.
* If this becomes an issue, chunk out your request more granularly
* in the referencing class, and ideally populate a stream with the
* partially processed content so it is not bloating runtime memory.
*
* @param mixed $content any of the following: [string, int, float, double, bool, null, an object that is iterable, an object with the __toString method]
* @return string
* @throws \oroboros\core\utilities\exception\InvalidArgumentException if it encounters un-stringable content.
*/
private function _contencateContentSafely($content) {
$content = '';
$current_class = get_called_class();
foreach ($content as $key => $value) {
if (
//scalar values safely cast to string
is_scalar($value)
//null is not scalar, but can cast to string
|| is_null($value)
//objects can be cast to string if they have the __toString magic method
|| is_object($value) && method_exists($value, '__toString')
) {
//append the contencated content.
$content .= (string) $value . $current_class::CONTENCATION_DELIMITER;
continue;
} elseif (is_array($value) || (is_object($value) && $this->_isIterableObject($value))) {
//bust up nested arrays
$content .= $this->_contencateContentSafely($value);
} elseif (
(is_object($value) && method_exists($value, '__toString'))
|| is_bool($value)
|| is_numeric($value)
|| is_string($value)
) {
//contencate normally
$content .= (string) $value . $current_class::CONTENT_IMPLODE_DELIMITER;
} else {
$valid = '[string, int, float, double, bool, null, an object that is iterable, an object with the __toString method]';
$received = gettype($value);
if (is_object($value)) {
$received = 'instance of: ' . get_class($value);
}
$message = sprintf('Invalid nested content passed in hook parse method. Accepts [%s], but received incompatible: [%s]. Operation cannot continue.', $valid, $received);
\oroboros\Oroboros::log(LogLevel::WARNING, $message);
throw new _Exception\InvalidArgumentException($message, self::ERROR_PHP_BAD_PARAMETERS);
}
return $content;
}
}
/**
* Returns whether or not the object passed is iterable,
* and can be handled similarly to an array.
*
* Will only accept an actual object (all php classes extend \stdClass).
* Type checking needs to be done earlier on if it applies.
* Will produce a fatal error if you give it anything else.
*
* Checks against native php valid iterable interfaces,
* and returns true if the object or it's parent implements either of them.
*
* @see http://php.net/manual/en/class.iterator.php
* @see http://php.net/manual/en/class.iteratoraggregate.php
* @see http://php.net/manual/en/class.traversable.php
*
* @note when only PHP 7.0+ is supported, this will be replaced with the native is_iterable function.
*
* @param \stdClass $object
* @return bool
*/
private function _isIterableObject(\stdClass $object) {
return (($object instanceof \Iterator)
|| ($object instanceof \IteratorAggregate)
|| ($object instanceof \Traversable)
);
}
/**
* Checks if debug is enabled for the current object instance.
* @return bool
*/
private function _isDebugEnabled() {
return ($this->_debug_enabled ? true : false);
}
/**
* Returns the debug hook replacement prefix if debug
* is enabled, and null if it isn't.
* @see \oroboros\core\abstracts\libraries\hooks\AbstractHook::_isDebugEnabled
* @param string $hook
* @return string|null
*/
private function _getDebugPrefix($hook) {
if (!$this->_isDebugEnabled()) {
return;
}
$current_class = get_called_class();
return sprintf($current_class::HOOK_DEBUG_PREFIX, $hook, $current_class);
}
/**
* Returns the debug hook replacement suffix if debug
* is enabled, and null if it isn't.
* @see \oroboros\core\abstracts\libraries\hooks\AbstractHook::_isDebugEnabled
* @param string $hook
* @return string|null
*/
private function _getDebugSuffix($hook) {
if (!$this->_isDebugEnabled()) {
return;
}
return
$current_class = get_called_class();
return sprintf($current_class::HOOK_DEBUG_SUFFIX, $hook, $current_class);
}
private function _getHookValue($hook) {
$prefix = $this->_getDebugPrefix($hook);
$suffix = $this->_getDebugSuffix($hook);
$content = $this->_getHookValueByKey($hook);
if ($this->_isDebugEnabled() && is_null($content)) {
$content = '[null]';
}
return $prefix . $content . $suffix;
}
private function _setHook($hook, $value) {
}
private function _unsetHook($hook) {
}
private function _listHooks() {
}
private function _getValidHookNames() {
}
private function _getHookValueByKey($hook) {
return null;
}
}