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
<?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\libraries\container;
/**
* <Container Trait>
* An implementation of \Psr\Container\ContainerInterface as a trait,
* which can be bound to any class to fulfill the requirements of
* the container interface.
* @see http://www.php-fig.org/psr/psr-11/
* @satisfies \Psr\Container\ContainerInterface
* @satisfies \oroboros\core\interfaces\contract\libraries\container\ContainerContract
* @author Brian Dayhoff <mopsyd@me.com>
*/
trait ContainerTrait
{
use \oroboros\core\traits\patterns\behavioral\RegistryTrait;
/**
* -------------------------------------------------------------------------
* Contract Methods
*
* These methods satisfy the public api defined in the bootstrap contract
*
* @satisfies \Psr\Container\ContainerInterface
* @satisfies \oroboros\core\interfaces\contract\libraries\container\ContainerContract
*
* -------------------------------------------------------------------------
*/
/**
* Finds an entry of the container by its identifier and returns it.
*
* @param string $id Identifier of the entry to look for.
*
* @throws NotFoundExceptionInterface No entry was found for **this** identifier.
* @throws ContainerExceptionInterface Error while retrieving the entry.
*
* @return mixed Entry.
*/
public function get( $id )
{
return $this->_get( $id );
}
/**
* Returns true if the container can return an entry for the given identifier.
* Returns false otherwise.
*
* `has($id)` returning true does not mean that `get($id)` will not throw an exception.
* It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
*
* @param string $id Identifier of the entry to look for.
*
* @return bool
*/
public function has( $id )
{
return $this->_check( $id );
}
/**
* -------------------------------------------------------------------------
* Extension Methods (protected)
*
* These methods may be extended by inheriting constructs as needed.
* They represent the interal api.
* -------------------------------------------------------------------------
*/
/**
* <Container Setter>
* @param scalar $key The key to identify the value by.
* @param mixed $value The container value to set.
* @param bool $safe If true, will not overwrite existing values. Default false.
* @return bool
* @throws \oroboros\core\utilities\exception\container\ContainerException if a non-scalar value is passed.
*/
protected function _set( $key, $value, $safe = false )
{
$this->_initializeRegistry();
if ( !is_scalar( $key ) )
{
throw new \oroboros\core\utilities\exception\container\ContainerException(
sprintf( \oroboros\core\interfaces\api\ExceptionMessageApi::ERROR_LOGIC_BAD_PARAMETERS_MESSAGE,
__METHOD__, 'scalar value', gettype( $key ) ),
\oroboros\core\interfaces\api\ExceptionCodeApi::ERROR_LOGIC_BAD_PARAMETERS );
}
return $this->_setRegistryValue($key, $value, $safe);
}
/**
* <Container Getter>
* Returns the requested value if it exists in the container.
* @param scalar $key
* @return mixed
* @throws \oroboros\core\utilities\exception\container\ContainerException If a non-scalar value is passed, or any other problem occurs with retrieval.
* @throws \oroboros\core\utilities\exception\container\NotFoundException If the value does not exist in the container.
*/
protected function _get( $key )
{
$this->_initializeRegistry();
if ( !is_scalar( $key ) )
{
throw new \oroboros\core\utilities\exception\container\ContainerException(
sprintf( \oroboros\core\interfaces\api\ExceptionMessageApi::ERROR_LOGIC_BAD_PARAMETERS_MESSAGE,
__METHOD__, 'scalar value', gettype( $key ) ),
\oroboros\core\interfaces\api\ExceptionCodeApi::ERROR_LOGIC_BAD_PARAMETERS );
}
if ( !$this->_checkRegistryValue( $key ) )
{
throw new \oroboros\core\utilities\exception\container\NotFoundException(
sprintf( 'No entry was found for identifier [%s].', $key ),
\oroboros\core\interfaces\api\ExceptionCodeApi::ERROR_CORE_DATA_OBJECT_FAILURE
);
}
try
{
return $this->_getRegistryValue( $key );
} catch ( \oroboros\core\interfaces\contract\utilities\exception\ExceptionContract $e )
{
throw new \oroboros\core\utilities\exception\container\ContainerException( $e->getMessage(),
$e->getCode(), $e );
}
}
/**
* <Container Check Method>
* Returns a boolean determination as to whether a specific value exists.
* This method is safe and does not throw exceptions.
* @param scalar $key
* @return bool
*/
protected function _check( $key )
{
$this->_initializeRegistry();
if ( !is_scalar( $key ) )
{
return false;
}
return $this->_checkRegistryValue( $key );
}
}