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
<?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\bootstrap;
/**
* <BootstrapTrait Trait>
* Satisfies the BootstrapContract requirements.
* Using this trait and implementing that contract interface should give
* you a fully functional bootstrap object that honors it's api on any class
* that does not collide with it's declared methods, with no additional work.
*
* --------
* Traits provide extended method support to classes without requiring a direct,
* linear chain of inheritance. This allows functions to inherit subsets of
* related methods without declaring a parent class.
*
* In Oroboros core, ALL methods are granted to classes via traits,
* and the classes themselves are just containers that correlate their methods
* to an interface they are expected to honor. This approach maximizes
* interoperability, by entirely removing class inheritance as a requirement
* for extension of any class in this system.
*
* 3rd parties using this package are not expected to follow this approach,
* but ALL of our internal class and logic structure does.
* --------
*
* @author Brian Dayhoff <mopsyd@me.com>
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
* @link bitbucket.org/oroborosframework/oroboros-core/wiki/development/api/contract_interface.md
* @category traits
* @package oroboros/core
* @subpackage bootstrap
* @requires \oroboros\core\interfaces\api\EnvironmentApi
* @satisfies \oroboros\core\interfaces\contract\bootstrap\BootstrapContract
* @satisfies \oroboros\core\interfaces\contract\utilities\UtilityContract
* @satisfies \oroboros\core\interfaces\contract\BaseContract
* @default \oroboros\core\bootstrap\Bootstrap
* @version 0.2.4-alpha
* @since 0.2.4-alpha
*/
trait BootstrapTrait
{
/**
* Here we are extending the BaseTrait,
* to inherit it's system recognition methods.
*/
use \oroboros\core\traits\BaseTrait;
/**
* This will be our local value store.
* This will also allow us to separate which internals
* we wish to expose from those we do not want to be
* outwardly affected.
*/
use \oroboros\core\traits\patterns\behavioral\Registry;
/**
* This will allow us to obtain the factories to automatically construct
* the appropriate router and controllers we need in the current scope,
* with respect to any overrides that have been defined.
*/
use \oroboros\core\traits\patterns\creational\factory\FactoryFactory;
/**
* -------------------------------------------------------------------------
* Contract Methods
*
* These methods satisfy the public api defined in the bootstrap contract
*
* @satisfies \oroboros\core\interfaces\contract\bootstrap\BootstrapContract
*
* @execution Default Execution Plan (minimal)
* //this represents generic usage in a bootload routine
* $bootstrap = new \path\to\your\class\with\this\trait\YourBootstrapClass();
* $bootstrap->initialize();
* try {
* $bootstrap->execute();
* }
* catch (\oroboros\core\interfaces\contract\bootstrap\BootstrapExceptionContract $e) {
* $bootstrap->error($e);
* }
* $bootstrap->shutdown();
* exit();
*
* @execution Default Execution Plan (commented)
* //this represents generic usage in a bootload routine
*
* //Create an instance
* //The default concrete for this is \oroboros\core\bootstrap\Bootstrap
* $bootstrap = new \path\to\your\class\with\this\trait\YourBootstrapClass();
*
* //Initialize the instance
* $bootstrap->initialize();
*
* //this object will throw a BootstrapException if it fails to execute.
* //We should catch this, and call the error method if it fires.
* try {
* $bootstrap->execute();
* }
*
* //We will catch the error using the
* //contract interface for bootstrap exceptions
* //This will allow us to also handle any overrides,
* //or integration with any existing exception that implements it.
* catch (\oroboros\core\interfaces\contract\bootstrap\BootstrapExceptionContract $e) {
*
* //BootstrapExceptions return meaningful results that can be used to
* //accurately create a valid error execution plan.
* //All you need to do here is pass it into the bootstrap error method.
* //It already contains any relevant error codes and message output
* //appropriate to the defined scope.
* $bootstrap->error($e);
* }
*
* //Our execution has completed.
* //We may optionally run the clean shutdown process.
* //You may also let PHP's native garbage collector
* //do the rest if you aren't doing anything else
* //after this point.
*
* //Tell the bootstrap object to release any objects it has
* //stored in memory, which will fire the destructors for all
* //objects anywhere in it's stack.
* $bootstrap->shutdown();
*
* //We don't need this object any longer.
* unset($bootstrap);
*
* //And we're done.
* exit();
*
* -------------------------------------------------------------------------
*/
/**
* <Bootstrap Constructor>
* Instantiates the bootstrap object.
* Can be called with no parameters, but accepts substitution parameters
* for unit testing specific requests, or requesting specific functionality
* that is normally out of scope for the current environment. Also accepts
* flags, which it will propogate to any controllers that it references
* automatically through dependency injection.
*
* If you intend to do full initialization within the constructor,
* you should call the initialize method manually within your constructor,
* and still place your initialization logic in that method.
* This allows for re-initialization without the weight of creating
* a new object.
*
* @param mixed $params (optional) An array of parameters to pass into the bootstrap, to replace it's normally defined values. This may be used for unit-testing the bootstrap process with specific overrides of local dependencies before deploying them into the normal codebase, or to create a localized bootstrap that works as a subroutine, with custom parameters it would not otherwise use.
* @param mixed $flags (optional) An array of flags to pass into the bootstrap, which will be propogated to controllers and routers it controls to modify their behavior. This presents a simple way for setting up debug, testing, profiling, or benchmarking paradigms, and allows for an easy way to pass environmental considerations (such as dev vs production), that will automatically propogate through all subsequent objects in the stack on execution.
* @return $this;
*/
public function __construct( $params = null, $flags = null )
{
}
/**
* <Bootstrap Destructor>
* The Bootstrap destructor MUST release all dependent objects and
* their children, and manually release it's memory footprint also.
* It should be configured to tell the webserver not to reserve it's
* memory and release it back into the pool for the actual machine
* hardware as well (apache does not do this by default, which can
* be problematic on servers with low memory or a lot of shared threads),
* though this action SHALL NOT be executed unless explicitly requested
* via a flag passed at either instantiation, initialization, or reset.
* @return void
*/
public function __destruct()
{
}
/**
* <Bootstrap Initialization>
* Initializes the bootstrap object.
* This method separates instantiation from initialization, and provides a
* reset point to restore to in the event that instantiation did not pass
* all of the values required, or if you needed multiple instances of the
* bootstrap object with different parameters.
*
* This method should contain ALL logic that needs to be performed prior
* to execution of the primary task of the Bootstrap object.
*
* @param array $params
* @param array $flags
* @return void
*/
public function initialize( $params = null, $flags = null )
{
}
/**
* <Bootstrap Reset>
* Resets the Bootstrap object to its state immediately after execution
* fires successfully, so it is initialized, but does not have any
* remnant data that was obtained through any action that occurred
* after initialization. The purpose of this method is to allow a
* degree of external control over the execution plan, and allow it
* to be called repeatedly if needed, without the weight of
* reinitialization.
*
* @param array $params (optional) If params are passed, they will replace the values known under the same key that were obtained during initialization. If not passed, they will be ignored.
* @param array $flags (optional) If params are passed, they will replace the flags that were passed or obtained during initialization. If not passed, they will be ignored.
* @return void
*/
public function reset( $params = null, $flags = null )
{
}
/**
* <Bootstrap Launch>
* Launches the application.
* If the bootstrap has not already initialized fully, it will do so at
* this point using it's default values (bootstraps should be capable of
* autonomous operation, but not assume it).
*
* At this point it will obtain it's routes, check for a match,
* load the appropriate controller if a match is found,
* load the default (or supplied) error controller if no match is found,
* initialize it's selected controller and pass any flags into it that
* were given to the bootstrap, and execute the controller route.
*/
public function launch()
{
}
/**
* <Bootstrap Shutdown>
* This method terminates the bootstrap object, and causes it to release
* all objects that it has instantiated along with their own chid objects.
* This should clear it's entire stack and memory footprint, and leave the
* bootstrap in an empty, uninitialized state. This SHALL NOT terminate
* the object, but will leave it to be either reinitialized or unset as
* determined by it's controlling logic.
*/
public function shutdown()
{
}
/**
* -------------------------------------------------------------------------
* Extension Methods (protected)
*
* These methods may be extended by inheriting constructs as needed.
* They represent the interal api.
* -------------------------------------------------------------------------
*/
/**
* This method provides an overrideable way to
* filter params to any construct that extends this logic.
*
* The actual param work is done privately for stability,
* but this method allows for overrides that are still
* not publicly accessible as needed.
*
* Child classes and traits can perform any operations
* needed prior to calling parent::_handleParams
*
* @param array $params
* @return void
*/
protected function _handleParams( $params = null )
{
//the default action is simply to pass this
//along to the internal mechanic that handles
//it without opinion.
$this->_parseInitializationParams( $params );
}
/**
* This method provides an overrideable way to
* filter flags to any construct that extends this logic.
*
* The actual param work is done privately for stability,
* but this method allows for overrides that are still
* not publicly accessible as needed.
*
* Child classes and traits can perform any operations
* needed prior to calling parent::_handleFlags
*
* @param array $params
* @return void
*/
protected function _handleFlags( $flags = null )
{
//the default action is simply to pass this
//along to the internal mechanic that handles
//it without opinion.
$this->_parseInitializationFlags( $flags );
}
/**
* -------------------------------------------------------------------------
* Logic Methods (private)
*
* These methods are not externally exposed.
* They represent the actual work.
* -------------------------------------------------------------------------
*/
/**
* This method parses the initialization parameters,
* which may occur during instantiation,
* during initialization,
* or during reset.
* @param array $params
* @return type
*/
private function _parseInitializationParams( $params = null )
{
if ( is_null( $flags ) )
{
//no operation by default
return;
}
//parse the initialization parameters
return;
}
/**
* Checks if the params are anything useable.
* @param mixed $params
* @return bool
*/
private function _validateInitializationParams( $params )
{
}
/**
* This method parses the initialization flags,
* which may occur during instantiation,
* during initialization,
* or during reset.
* @param array $flags
* @return void
*/
private function _parseInitializationFlags( $flags = null )
{
if ( is_null( $flags ) )
{
//no operation by default
return;
}
//parse the flags
return;
}
/**
* Checks if the flags are anything useable.
* @param mixed $flags
* @return bool
*/
private function _validateInitializationParams( $flags )
{
}
}