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
<?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;
/**
* <Core Trait>
* Provides functionality for the global static accessor contract.
* @requires \oroboros\core\interfaces\api\UtilityApi
* @author Brian Dayhoff <mopsyd@me.com>
*/
trait CoreTrait
{
/**
* @todo clean up this deprecated mess, and use the static control api pattern to overload extension method decorators.
*/
private static $_log_modes = array(
"default" => self::LOGGER_DEFAULT,
"file" => self::LOGGER_FILE,
"database" => self::LOGGER_DATABASE,
"null" => self::LOGGER_NULL,
"screen" => self::LOGGER_SCREEN,
"cli" => self::LOGGER_CLI,
"css" => self::LOGGER_CSS,
"js" => self::LOGGER_JS,
"ajax" => self::LOGGER_AJAX,
);
private static $_valid_log_levels = [
\Psr\Log\LogLevel::EMERGENCY,
\Psr\Log\LogLevel::ALERT,
\Psr\Log\LogLevel::CRITICAL,
\Psr\Log\LogLevel::ERROR,
\Psr\Log\LogLevel::WARNING,
\Psr\Log\LogLevel::NOTICE,
\Psr\Log\LogLevel::INFO,
\Psr\Log\LogLevel::DEBUG,
];
private static $_env;
private static $_initialized = false;
private static $_logger;
private static $_autoloader;
private static $_settings = array();
private static $_codex;
private static $_mode;
private static $_valid_modes = array();
public function __construct(array $options = array(), array $flags = array()) {
if (!self::$_initialized) {
$this->initialize($options, $flags);
}
}
/**
* Aliases the static setLoggerObject method, for Psr-3 compliance.
* This method MAY be used to set the logger, but this class does not
* otherwise need to be instantiated.
* @param \Psr\Log\LoggerInterface $logger
*/
public function setLogger(\Psr\Log\LoggerInterface $logger) {
self::setLoggerObject($logger);
}
/**
* Proxy for static init
* @param array $options
* @param array $flags
*/
public function initialize(array $options = array(), array $flags = array()) {
self::init($options, $flags);
}
public static function init(array $options = array(), array $flags = array()) {
if (!empty($options)) {
//parse custom load options
$options['core'] = ((isset($options['core'])) ? $options['core'] : self::_getDefaultSettings()['core']);
} else {
//bootstrap normally
$options = self::_getDefaultSettings();
}
self::$_settings = $options;
self::_setLogger();
self::$_initialized = TRUE;
}
public static function isInitialized() {
return self::$_initialized;
}
/**
* Returns a system-compliant absolute file path
* from a supplied relative path, or alternately
* from the location it was called.
* NOTE: DO NOT USE WITHOUT SUPPLYING A PATH IN CLOSURES.
* If used in a closure, pass __DIR__ as the path,
* or it will throw an exception.
* @throws \oroboros\core\utilities\exception\Exception If called within a closure without supplying a path parameter, which will break execution otherwise.
* @param string|\Directory|\File $path (optional) The specified relative path. Also supports native PHP file and directory objects. If not supplied, will attempt to return the path to the file where it was called.
* @return string|boolean Returns [false] if path is not valid. Otherwise returns the fully qualified absolute path.
*/
public static function filepath($path = NULL) {
$result = false;
if ($path) {
echo 'foooo';
var_dump(self::OROBOROS_BASEPATH . DIRECTORY_SEPARATOR);
} else {
//perform a backtrace to determine where the method was called, find the file from the last valid entry.
};
return $result;
}
/**
* Returns a new instance of the logger,
* already initialized to the current core settings.
* You may use this to quickly grab a logger instance from anywhere.
* Individual instances are prototyped against the CURRENT GLOBAL LOGGER.
* This is not a singleton, so you will need to check that you have the
* one you need, otherwise use the factory to build one.
* @param \Psr\Log\LoggerInterface $logger
*/
public static function getLoggerObject() {
if (!self::$_initialized) {
self::init();
}
return clone(self::$_logger);
}
/**
* <Pre-Initialization Methods>
* You should probably just call the bootload routine instead of these
* unless you have a really good reason for doing so.
*/
public static function setAutoloader(\oroboros\core\libraries\psr4\Autoloader $autoloader) {
self::$_autoloader = $autoloader;
}
/**
* Sets a logger object. Must implement the Psr-3 logger interface
* @param \Psr\Log\LoggerInterface $logger
*/
public static function setLoggerObject(\Psr\Log\LoggerInterface $logger) {
self::$_logger = $logger;
}
/**
* Logs an entry using whatever Psr3 Compliant
* logger the system currently has.
* @return type
*/
public static function log($level, $message, array $context = array()) {
if (!self::$_initialized) {
self::init();
}
return self::$_logger->log($level, $message, $context);
}
/**
* <Configuration Deployer>
* @since 0.0.1a
* This method fetches parts of the initialization settings.
* If the optional key is supplied, it will attempt to return
* that section, or false if it does not exist. Otherwise it
* returns the entire settings index.
* @param type $key
* @return type
*/
public static function config($key = NULL) {
if (!self::$_initialized) {
self::init();
}
return (isset($key) ? ((array_key_exists($key, self::$_settings)) ? self::$_settings[$key] : FALSE) : self::$_settings);
}
/**
* Determines the current environment, and sets basic runtime parameters to
* help avoid common errors that result from environmental assumptions that
* are missing or not defined as expected.
*/
private static function _setEnv() {
}
private static function _setLogger(array $settings = array(), array $flags = array()) {
$settings = ((!empty($settings)) ? : self::_getDefaultSettings()['core']['log']);
//use the default server logfile if there isn't an override value provided
if (!array_key_exists('log', $settings)) {
$settings['log'] = OROBOROS_ERROR_LOG;
}
$logger = constant('self::LOGGER_' . strtoupper($settings['logmode']));
try {
self::$_logger = new $logger($settings, $flags);
} catch (\oroboros\core\utilities\exception\Exception $e) {
//if our log directory is not writeable, we will instead use a null logger
$logger = self::LOGGER_NULL;
self::$_logger = new $logger($settings, $flags);
}
}
private static function _getDefaultSettings() {
$config = realpath(OROBOROS_ROOT_DIRECTORY . self::SETTINGS_OVERRIDE);
if (!$config) {
//use the default config if no user provided override exists
$config = realpath(OROBOROS_ROOT_DIRECTORY . self::SETTINGS_DEFAULT);
}
if ($config) {
$config = json_decode(file_get_contents($config), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_OBJECT_AS_ARRAY);
}
return $config;
}
}