mirror of
https://we.phorge.it/source/phorge.git
synced 2024-12-01 19:22:42 +01:00
23786784ef
Summary: Adds the Balanced PHP API to externals/. Ref T2787. Test Plan: Used in next diff. Reviewers: btrahan, chad Reviewed By: chad CC: aran, aurelijus Maniphest Tasks: T2787 Differential Revision: https://secure.phabricator.com/D5764
97 lines
2.4 KiB
PHP
97 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Httpful;
|
|
|
|
/**
|
|
* Bootstrap class that facilitates autoloading. A naive
|
|
* PSR-0 autoloader.
|
|
*
|
|
* @author Nate Good <me@nategood.com>
|
|
*/
|
|
class Bootstrap
|
|
{
|
|
|
|
const DIR_GLUE = DIRECTORY_SEPARATOR;
|
|
const NS_GLUE = '\\';
|
|
|
|
public static $registered = false;
|
|
|
|
/**
|
|
* Register the autoloader and any other setup needed
|
|
*/
|
|
public static function init()
|
|
{
|
|
spl_autoload_register(array('\Httpful\Bootstrap', 'autoload'));
|
|
self::registerHandlers();
|
|
}
|
|
|
|
/**
|
|
* The autoload magic (PSR-0 style)
|
|
*
|
|
* @param string $classname
|
|
*/
|
|
public static function autoload($classname)
|
|
{
|
|
self::_autoload(dirname(dirname(__FILE__)), $classname);
|
|
}
|
|
|
|
/**
|
|
* Register the autoloader and any other setup needed
|
|
*/
|
|
public static function pharInit()
|
|
{
|
|
spl_autoload_register(array('\Httpful\Bootstrap', 'pharAutoload'));
|
|
self::registerHandlers();
|
|
}
|
|
|
|
/**
|
|
* Phar specific autoloader
|
|
*
|
|
* @param string $classname
|
|
*/
|
|
public static function pharAutoload($classname)
|
|
{
|
|
self::_autoload('phar://httpful.phar', $classname);
|
|
}
|
|
|
|
/**
|
|
* @param string base
|
|
* @param string classname
|
|
*/
|
|
private static function _autoload($base, $classname)
|
|
{
|
|
$parts = explode(self::NS_GLUE, $classname);
|
|
$path = $base . self::DIR_GLUE . implode(self::DIR_GLUE, $parts) . '.php';
|
|
|
|
if (file_exists($path)) {
|
|
require_once($path);
|
|
}
|
|
}
|
|
/**
|
|
* Register default mime handlers. Is idempotent.
|
|
*/
|
|
public static function registerHandlers()
|
|
{
|
|
if (self::$registered === true) {
|
|
return;
|
|
}
|
|
|
|
// @todo check a conf file to load from that instead of
|
|
// hardcoding into the library?
|
|
$handlers = array(
|
|
\Httpful\Mime::JSON => new \Httpful\Handlers\JsonHandler(),
|
|
\Httpful\Mime::XML => new \Httpful\Handlers\XmlHandler(),
|
|
\Httpful\Mime::FORM => new \Httpful\Handlers\FormHandler(),
|
|
\Httpful\Mime::CSV => new \Httpful\Handlers\CsvHandler(),
|
|
);
|
|
|
|
foreach ($handlers as $mime => $handler) {
|
|
// Don't overwrite if the handler has already been registered
|
|
if (Httpful::hasParserRegistered($mime))
|
|
continue;
|
|
Httpful::register($mime, $handler);
|
|
}
|
|
|
|
self::$registered = true;
|
|
}
|
|
}
|