public static function initWithLogger(Configuration $config, $loggerPrefix = '')
 {
     self::init($config);
     if (!self::$loggerInitialized) {
         // FIXME: Terminate logger crap with extreme prejudice
         Logger::init($config->val('logging/root-context'), $config->val('logging/log-level'), $config, $loggerPrefix);
         self::$loggerInitialized = true;
     }
 }
 /**
  * @return Response
  */
 public static function process()
 {
     // Can go away once we require PHP 5.6
     ini_set('default_charset', 'UTF-8');
     // --- Get the request and response objects
     $request = Request::createFromGlobals();
     $response = new Response();
     $response->setPrivate();
     // --- Break the request into parts ---
     $uri = $request->query->get('p', '');
     $parts = explode('/', $uri);
     $request->query->remove('p');
     if (count($parts) < 2) {
         $response->setStatusCode(403, 'Cannot process this request: bad URI format. A configuration node and an action is required');
         return $response;
     }
     $view = array_shift($parts);
     $action = array_shift($parts);
     // --- Initialize core services ---
     $config = Configuration::createForView($view);
     Context::init($config);
     Logger::init($config->val('logging/root-context'), $config->val('logging/log-level'), $config, Context::get()->getContextId());
     if ($config->nodeExists('disabled') && $config->val('disabled')) {
         Logger::debug('403 will be given for disabled view.', $uri);
         $response->setStatusCode(403, "View '{$view}' disabled. Cannot continue.");
         return $response;
     }
     if ($config->nodeExists('charset')) {
         // recreate the request with a different input encoding
         // FIXME: This is only converting the POST values.  Also,
         // is there really no better way to do this?
         $decoded = rawurldecode($request->getContent());
         $content = mb_convert_encoding($decoded, 'UTF-8', $config->val('charset'));
         parse_str($content, $data);
         $request->request = new ParameterBag($data);
     }
     set_error_handler('\\SmashPig\\Core\\Http\\RequestHandler::lastChanceErrorHandler');
     set_exception_handler('\\SmashPig\\Core\\Http\\RequestHandler::lastChanceExceptionHandler');
     register_shutdown_function('\\SmashPig\\Core\\Http\\RequestHandler::shutdownHandler');
     // Check to make sure there's even a point to continuing
     Logger::info("Starting processing for request, configuration view: '{$view}', action: '{$action}'");
     if (!$config->nodeExists("endpoints/{$action}")) {
         Logger::debug('403 will be given for unknown action on inbound URL.', $uri);
         $response->setStatusCode(403, "Action '{$action}' not configured. Cannot continue.");
         return $response;
     }
     // Inform the request object of our security environment
     $trustedHeader = $config->val('security/ip-header-name');
     if ($trustedHeader) {
         $request->setTrustedHeaderName(Request::HEADER_CLIENT_IP, $trustedHeader);
     }
     $trustedProxies = $config->val('security/ip-trusted-proxies');
     if ($trustedProxies) {
         $request->setTrustedProxies($trustedProxies);
     }
     // --- Actually get the endpoint object and start the request ---
     $endpointObj = $config->object("endpoints/{$action}");
     if ($endpointObj instanceof IHttpActionHandler) {
         $endpointObj->execute($request, $response);
     } else {
         $str = "Requested action '{$action}' does not implement a known handler. Cannot continue.";
         Logger::debug($str);
         $response->setStatusCode(500, $str);
     }
     $code = $response->getStatusCode();
     if ($code !== 200 && $code !== 302) {
         $response->setContent('');
     }
     return $response;
 }
 /**
  * Do some sanity checking and framework setup
  */
 public function setup()
 {
     global $maintClass;
     // Abort if called from a web server
     if (isset($_SERVER) && isset($_SERVER['REQUEST_METHOD'])) {
         $this->error('This script must be run from the command line', true);
     }
     if (version_compare(phpversion(), '5.2.4') >= 0) {
         // Send PHP warnings and errors to stderr instead of stdout.
         // This aids in diagnosing problems, while keeping messages
         // out of redirected output.
         if (ini_get('display_errors')) {
             ini_set('display_errors', 'stderr');
         }
         // Don't touch the setting on earlier versions of PHP,
         // as setting it would disable output if you'd wanted it.
         // Note that exceptions are also sent to stderr when
         // command-line mode is on, regardless of PHP version.
     }
     // Set max execution time to 0 (no limit). PHP.net says that
     // "When running PHP from the command line the default setting is 0."
     // But sometimes this doesn't seem to be the case.
     ini_set('max_execution_time', 0);
     $this->loadParamsAndArgs();
     $this->helpIfRequested();
     $this->adjustMemoryLimit();
     // --- Initialize core services ---
     $configNode = $this->getOption('config-node');
     $configFile = $this->getOption('config-file');
     $config = Configuration::createForViewWithOverrideFile($configNode, $configFile);
     Context::init($config);
     Logger::init($config->val('logging/root-context') . '-' . end(explode("\\", $maintClass)), $config->val('logging/log-level'), $config, Context::get()->getContextId());
     Logger::getContext()->addLogStream(new ConsoleLogStream());
     set_error_handler('\\SmashPig\\Maintenance\\MaintenanceBase::lastChanceErrorHandler');
     set_exception_handler('\\SmashPig\\Maintenance\\MaintenanceBase::lastChanceExceptionHandler');
 }