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 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
<?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\traits\utilities;
/**
* <Utility Api Trait>
* Provides compatibility with oroboros core for 3rd party class children.
* @requires \oroboros\core\interfaces\api\UtilityApi
* @author Brian Dayhoff <mopsyd@me.com>
*/
trait UtilityTrait
{
/**
* <Instance Fingerprint Registry>
* This is a list of all currently registered class fingerprints.
* It is only accessible to this class. This is used to insure
* that there are no collisions in instance keys for any reason
* (without needing to implement a heavier randomization engine),
* and otherwise has no effect.
* @var array $_fingerprint_instances
*/
private static $_fingerprint_instances = array();
/**
* <Object Instance Fingerprint>
* This is generated at the end of the base class constructor sequence.
* It is a unique key that can only exist for that specific instance of
* a constructor firing.
* @var string $_fingerprint
*/
private $_fingerprint;
private $_initialized = false;
private $_parameters = array();
private $_default_parameters = array();
private $_parameters_initialized = false;
private $_flags = array();
private $_default_flags = array();
private $_flags_initialized = false;
private $_locks = array(
"read" => false, //disables reading public variables
"write" => false, //disables setting public variables
"execute" => false, //disables any methods that use $this->_checkLockStatus("execute");
);
/**
* <Default Magic Getter>
* Defers magic method getter to the standard getter method
* @param string $name
* @return type
* @since 0.0.1a
*/
public function __get( $name )
{
return $this->get( $name );
}
/**
* <Default Magic Setter>
* Defers magic method setter to the standard setter method
* @param type $name
* @param type $value
* @return type
* @since 0.0.1a
*/
public function __set( $name, $value )
{
return $this->set( $name, $value );
}
/**
* <Default Setter>
* Default standard setter. Performs the same
* as the native php getter, except that it honors
* locks if they are defined.
* @param type $key
* @param mixed $value
* @return boolean
* @since 0.0.1a
*/
public function set( $key, $value )
{
if ( $this->_checkLock( 'write' ) )
{
throw new \oroboros\core\utilities\exception\Exception( "Cannot set value [$key] when object write access is disabled.",
\oroboros\core\interfaces\api\utilities\exception\ExceptionCodeApi::ERROR_SECURITY_LOCKED_RESOURCE );
}
return $this->{$key} = $value;
}
/**
* <Default Getter>
* Default standard getter. Throws an oroboros
* exception if resource not found instead of an
* error. Otherwise behaves the same as the
* native php getter.
*
* @param string $key
* @return mixed
* @throws \oroboros\core\utilities\exception\Exception
* @since 0.0.1a
*/
public function get( $id )
{
if ( $this->_checkLock( 'read' ) )
{
throw new \oroboros\core\utilities\exception\Exception( "Cannot set value [$id] when object read access is disabled.",
\oroboros\core\interfaces\api\utilities\exception\ExceptionCodeApi::ERROR_SECURITY_LOCKED_RESOURCE );
}
return ((isset( $this->{$id} ))
? $this->{$id}
: NULL);
}
/**
* <Default Initialization Method>
* Provides an unopinionated way to initialize parameters and flags.
* This class does nothing with the parameters or flags aside from
* storing them so they can be referenced and checked against. Child
* classes should implement their own functionality around this, but
* it can be safely ignored entirely.
* @note Parameter and flag initialization can also happen directly in the constructor if they need to be exposed before your class initialization method runs, or may be exposed at both if you need to separate pre-initialization parameters from initialization parameters.
* @param type $params
* @param type $flags
* @since 0.0.1a
*/
public function initialize( array $params = array(), array $flags = array() )
{
$this->_handleParameters( $params );
$this->_handleFlags( $flags );
$this->_initialized = true;
}
/**
* <Default Initialization Check Method>
* Provides a simple way of checking if initialization has occurred.
* If initialization method has been called, this will return true.
* @return boolean
* @since 0.0.2a
*/
public function isInitialized()
{
return $this->_initialized;
}
/**
* <Fingerprint Getter Method>
* This returns the unique fingerprint of the object.
* All instantiated Oroboros objects have this method.
* This method may not be overruled.
* @return type
* @final
* @since 0.0.2a
*/
final public function fingerprint()
{
return $this->_fingerprint;
}
/**
* <Is Base Class Constructor Complete Check Method>
* Returns [true] if the base class has been constructed.
* Useful for sanity checks and control blocks that suppress triggers from
* firing before construction is complete.
* @return boolean
* @since 0.0.2a
*/
protected function _isConstructed()
{
return $this->_constructed;
}
/**
* <Locking Functionality>
* Oroboros provides a basic asset locking mechanism to
* add additional security to your class. This allows you
* to designate the class as read-only, write-only, or to
* customize which functions are allowed to run based on
* state. All of this is disabled by default, but is
* provided for convenience.
*/
/**
* <Default Master Lock Check Method>
* Provides a simple way of checking all current lock types at once.
* @param array $status
* @return type
* @throws \Exception
* @since 0.0.1a
*/
protected function _checkLocks( array $status )
{
if ( !in_array( $status, array_keys( $this->_locks ) ) )
{
throw new \Exception();
}
return array(
"read" => $this->_checkLock( 'read' ),
"write" => $this->_checkLock( 'write' ),
"execute" => $this->_checkLock( 'execute' )
);
}
/**
* <Default Lock Checking Method>
* Checks whether the provided type is enabled
* or disabled. If the type is not recognized,
* throws an exception.
* @param type $type
* @return type
* @throws \oroboros\core\utilities\exception\Exception
* @since 0.0.1a
*/
protected function _checkLock( $type )
{
//acceptible $type = ["read", "write", "execute"]
if ( !in_array( $type, array_keys( $this->_locks ) ) )
{
throw new \oroboros\core\utilities\exception\Exception( "Invalid lock type. Acceptable lock types are [" . implode( array_keys( $this->_locks ) ) . "]",
\oroboros\core\interfaces\api\utilities\exception\ExceptionCodeApi::ERROR_LOGIC_BAD_PARAMETERS );
}
return $this->_locks[$type];
}
/**
* <Default Lock Setter Method>
* Sets a lock to the specified state (true is on, false is off).
* @param string $type ["read", "write", "execute"]
* @param boolean $value [true, false, 0, 1]
* @return type
* @throws \oroboros\core\utilities\exception\Exception
* @since 0.0.1a
*/
protected function _setLock( $type, $value )
{
//acceptible $type = ["read", "write", "execute"]
//acceptible $value = [true, false, 0, 1]
if ( !in_array( $type, array_keys( $this->_locks ) ) )
{
throw new \oroboros\core\utilities\exception\Exception( "Invalid lock type. Acceptable lock types are [" . implode( array_keys( $this->_locks ) ) . "]",
\oroboros\core\interfaces\api\utilities\exception\ExceptionCodeApi::ERROR_LOGIC_BAD_PARAMETERS );
}
if ( !in_array( $value,
array(
true,
false,
0,
1 ) ) )
{
throw new \oroboros\core\utilities\exception\Exception( "Invalid lock value. Acceptable lock types are [true, false, 0, 1]",
\oroboros\core\interfaces\api\utilities\exception\ExceptionCodeApi::ERROR_LOGIC_BAD_PARAMETERS );
}
return $this->_locks[$type];
}
/**
* <Parameter Handling Null Template>
* Overrule this method with how your parameters passed
* into the constructor or initialization method should
* be handled. It will be called whenever those parameters
* are updated.
* @return void
* @since 0.0.1a
*/
protected function _updateParameterConditions( $parameter_key )
{
}
/**
* <Flag Handling Null Template>
* Overrule this method with how your flags passed
* into the constructor or initialization method should
* be handled. It will be called whenever those parameters
* are updated.
* @return void
* @since 0.0.1a
*/
protected function _updateFlagConditions()
{
}
/**
* <Default Flag Setter Method>
* Sets a flag.
* @param type $key
* @throws \oroboros\core\utilities\exception\Exception
* @since 0.0.1a
*/
protected function _setFlag( $key )
{
if ( !is_string( $flag ) )
{
throw new \oroboros\core\utilities\exception\Exception( "Invalid object key: [$key] requested from [" . get_class( $this ) . '], thrown at ' . __LINE__ . ' of ' . __METHOD__,
\oroboros\core\interfaces\api\utilities\exception\ExceptionCodeApi::ERROR_LOGIC_BAD_PARAMETERS );
}
if ( array_search( $key, $this->_flags ) !== false )
{
$this->_flags[] = $key;
}
}
/**
* <Default Flag Check Method>
* Checks if a specific flag has been set.
* @param string $flag
* @return boolean
* @since 0.0.1a
*/
protected function _checkFlag( $flag )
{
return (in_array( $flag, $this->_flags ));
}
/**
* <Default Parameter Check Method>
* Returns [true] if a parameter is defined, [false] if undefined.
* This can be used to circumvent exceptions if the presence of a
* specific parameter is not certain.
* @param string $key
* @return boolean
* @since 0.0.2a
*/
protected function _checkParam( $key )
{
return (array_key_exists( $key, $this->_parameters ));
}
/**
* <Default Parameter Getter Method>
* Gets a specific parameter by key.
* @param string $key
* @return mixed
* @throws \oroboros\core\utilities\exception\Exception
*/
protected function _getParam( $key )
{
return ((isset( $this->_parameters[$key] ))
? $this->_parameters[$key]
: NULL);
}
/**
* <Default Parameter Setter Method>
* Sets a specific parameter.
* @param string $key
* @param mixed $value
* @return void
* @since 0.0.1a
*/
protected function _setParam( $key, $value )
{
$this->_parameters[$key] = $value;
}
/**
* <Default Parameter List Setter Method>
* Sets a list of parameters.
* @param array $params
* @return void
* @since 0.0.2a
*/
protected function _setParams( array $params )
{
array_map( function($param) use ($params)
{
return $this->_setParam( array_search( $param, $params ), $param );
}, $params );
}
/**
* <Internal Methods>
* Under the hood stuff
*/
/**
* If [$parameters] is provided, sets the object
* flags to the provided values, and calls
* the _updateParameterConditions method.
* If no parameters provided, Sets the parameters
* back to their default state, and
* calls the _updateParametersConditions method
* (which fires any time parameters are updated).
* @param array $parameters
*/
private function _handleParameters( array $parameters )
{
if ( empty( $parameters ) )
{
$parameters = $this->_default_parameters;
}
foreach ( $parameters as
$key =>
$value )
{
$this->_parameters[$key] = $value;
$this->_updateParameterConditions( $key );
}
$this->_parameters_initialized = true;
}
/**
* If [$flag] is provided, sets the object
* flags to the provided values, and calls
* the _updateFlagConditions method.
* If no parameters provided, Sets the flags
* back to their default state, and
* calls the _updateFlagConditions method
* (which fires any time flags are updated).
*/
private function _handleFlags( array $flags )
{
if ( empty( $flags ) )
{
$parameters = $this->_default_flags;
}
$this->_flags = $flags;
$this->_flags_initialized = true;
$this->_updateFlagConditions();
}
/**
* Resets the defined class parameters to their default state.
* @return void
* @since 0.0.1a
*/
private function _resetParameters()
{
$this->_parameters_initialized = false;
$this->_handleParameters( $this->_default_parameters );
}
/**
* Resets flags in the object back to their predefined defaults.
* @return void
* @since 0.0.1a
*/
private function _resetFlags()
{
$this->_flags_initialized = false;
$this->_handleFlags( $this->_default_flags );
}
/**
* Sets a group of flags.
* @param array $flags
* @since 0.0.2a
*/
protected function _setFlags( array $flags )
{
$this->_handleFlags( $flags );
}
/**
* Places a lock on this object instance for
* one of the specified lock types [read, write, execute].
* @param type $type
* @throws \oroboros\core\utilities\exception\Exception
* @since 0.0.1a
*/
private function _lock( $type )
{
//acceptible $type = ["read", "write", "execute"]
if ( !in_array( $type, array_keys( $this->_locks ) ) )
{
throw new \oroboros\core\utilities\exception\Exception( "Invalid lock type. Acceptable lock types are [" . implode( array_keys( $this->_locks ) ) . "]",
\oroboros\core\interfaces\api\utilities\exception\ExceptionCodeApi::ERROR_LOGIC_BAD_PARAMETERS );
}
$this->_locks[$type] = true;
}
/**
* Unlocks access in the class for a specific
* lock type [read, write, execute]
* @param type $type
* @throws \oroboros\core\utilities\exception\Exception
* @since 0.0.1a
*/
private function _unlock( $type )
{
//acceptible $type = ["read", "write", "execute"]
if ( !in_array( $type, array_keys( $this->_locks ) ) )
{
throw new \oroboros\core\utilities\exception\Exception( "Invalid lock type. Acceptable lock types are [" . implode( array_keys( $this->_locks ) ) . "]",
\oroboros\core\interfaces\api\utilities\exception\ExceptionCodeApi::ERROR_LOGIC_BAD_PARAMETERS );
}
$this->_locks[$type] = false;
}
/**
* Creates a unique fingerprint string.
* @return string
* @since 0.0.2a
*/
private function _generateObjectFingerprint()
{
$time = \time();
$class = \get_class( $this );
$rand = (string) \mt_rand( 0, \mt_getrandmax() );
$fingerprint = \sha1( $class . $time . $rand );
//If for some reason a collision occurs,
//new fingerprints will be attempted until
//a unique one is found. The likelihood of
//collision is low enough that this recursion
//should not cause an issue.
return array_key_exists( $fingerprint, self::$_fingerprint_instances )
? $this->_generateObjectFingerprint()
: $fingerprint;
}
/**
* Sets a unique fingerprint for each
* instantiated instance of a class.
* @return void
* @since 0.0.2a
*/
private function _setFingerprint()
{
$fingerprint = $this->_generateObjectFingerprint();
$this->_fingerprint = $fingerprint;
}
}