/** * Constructor * * @author Art <*****@*****.**> * * @param LoggerInterface $logger If provided, this will be used to log errors and exceptions. * @param AbstractConfig $cfg The configuration class. Required. * * @since 1.4 $cfg added. This will become the first parameter in the constructor in 2.0 */ public function __construct(LoggerInterface $logger = null, AbstractConfig $cfg = null) { if (!$logger) { $logger = new Log(); } $this->config = Alo::ifnull($cfg, new AbstractConfig()); $this->logger = $logger; $this->isCLI = !$this->config->forceHTML && Alo::isCliRequest(); $this->initSymfony(); }
/** * Formats the timeout * * @author Art <*****@*****.**> * * @param int|DateTimeInterface $timeout The timeout * * @return bool false if an error occurred * @since 1.0.2 Uses DateTimeInterface instead of DateTime */ private function formatTimeout(&$timeout = null) { if ($timeout instanceof DateTimeInterface) { $time = time(); $timeout = $timeout->getTimestamp(); if ($time > $timeout) { trigger_error('The timeout cannot be in the past', E_USER_WARNING); //@codeCoverageIgnoreStart return false; //@codeCoverageIgnoreEnd } else { $timeout = $timeout - $time; } } else { $timeout = Alo::ifnull($timeout, $this->config->timeout, true); } return true; }
/** * Returns the debug backtrace * * @author Art <*****@*****.**> * @return array * @since 1.3 */ protected function getBacktrace() { $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); if (empty($trace)) { // @codeCoverageIgnoreStart return []; // @codeCoverageIgnoreEnd } else { foreach ($trace as $k => $v) { foreach (self::$ignoredFiles as $i) { if (stripos(Alo::ifnull($v['file'], ''), $i) !== false) { unset($trace[$k]); break; } } } } return array_values($trace); }
/** * The error handler * * @author Art <*****@*****.**> * * @param int $errno The level of the error raised * @param string $errstr The error message * @param string $errfile The filename that the error was raised in * @param int $errline The line number the error was raised at * * @since 1.2 Tracks the last reported error * @codeCoverageIgnore */ public function handle($errno, $errstr, $errfile, $errline) { self::$lastReported = new Error($errno, $errstr, $errfile, $errline); $this->injectCSS(); $type = Alo::ifnull(Error::$map[$errno], $errno); $label = 'danger'; switch ($errno) { case E_NOTICE: case E_USER_NOTICE: $label = 'info'; break; case E_DEPRECATED: case E_USER_DEPRECATED: case E_WARNING: case E_USER_WARNING: case E_STRICT: case E_CORE_WARNING: $label = 'warning'; } $file = implode(DIRECTORY_SEPARATOR, array_slice(explode(DIRECTORY_SEPARATOR, $errfile), -2)); if (!$errline) { $errline = '<<unknown>>'; } if ($this->isCLI) { $label = $label == 'danger' ? 'e' : substr($label, 0, 1); $this->handleCLI($type, $label, $errno, $errstr, $file, $errline); } else { $this->handleHTML($type, $label, $errno, $errstr, $file, $errline); } $this->log($errno, $errstr, $errfile, $errline); }
/** * The shutdown handler * * @author Art <*****@*****.**> * @since 1.2.1 Should now report fatal errors if no errors had been raised beforehand. * @codeCoverageIgnore */ public function handle() { if (ErrorHandler::isRegistered()) { $e = new Error(error_get_last()); if (Error::shouldBeReported($e->getType())) { $r = ErrorHandler::getLastReportedError(); $h = ErrorHandler::getLastRegisteredHandler(); if (!$e->isEmpty() && $h && ($r ? !$r->isEmpty() && !$r->equals($e) : true)) { $h->handle($e->getType(), Alo::ifnull($e->getMessage(), '<<unknown error>>'), Alo::ifnull($e->getFile(), '<<unknown file>>'), Alo::ifnull($e->getLine(), '<<unknown line>>')); } } } }
/** * Send our request * * @author Art <*****@*****.**> * * @param string $httpMethod The HTTP method to use (GET, POST etc) * @param string $url The URL to call. This is automatically prepended with https:// in this method * @param array $options Request options * * @return \GuzzleHttp\Promise\PromiseInterface|\Psr\Http\Message\ResponseInterface The promise interface if * async is set to true and the * request interface if it is * set to false * @throws \GuzzleHttp\Exception\ClientException */ protected final function sendAbstract($httpMethod, $url, array $options = []) { return call_user_func($this->sendCallable, new Request($httpMethod, 'https://' . $url, array_merge(['authorization' => 'Bearer ' . $this->token], Alo::ifnull($options['headers'], [], true)), Alo::nullget($options['body']))); }
/** * Constructor * * @author Art <*****@*****.**> * * @param Config $cfg Your custom configuration * @param LoggerInterface $logger A logger object. If omitted, AloFramework\Log will be used. */ public function __construct(Config $cfg = null, LoggerInterface $logger = null) { $this->config = Alo::ifnull($cfg, new Config()); $this->log = Alo::ifnull($logger, new Log()); $this->setID(); }
/** * Constructor * * @author Art <*****@*****.**> * * @param LoggerInterface $logger If provided, this will be used to log exceptions. * @param ExceptionConfig $cfg The configuration class * * @since 1.4 $cfg added. This will become the first parameter in the constructor in 2.0 */ public function __construct(LoggerInterface $logger = null, ExceptionConfig $cfg = null) { parent::__construct($logger, Alo::ifnull($cfg, new ExceptionConfig())); }