|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Httpful; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Bootstrap class that facilitates autoloading. A naive |
|
7
|
|
|
* PSR-0 autoloader. |
|
8
|
|
|
* |
|
9
|
|
|
* @author Nate Good <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
class Bootstrap |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
const DIR_GLUE = DIRECTORY_SEPARATOR; |
|
15
|
|
|
const NS_GLUE = '\\'; |
|
16
|
|
|
|
|
17
|
|
|
public static $registered = false; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Register the autoloader and any other setup needed |
|
21
|
|
|
*/ |
|
22
|
|
|
public static function init() |
|
23
|
|
|
{ |
|
24
|
|
|
spl_autoload_register(array('\Httpful\Bootstrap', 'autoload')); |
|
25
|
|
|
self::registerHandlers(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The autoload magic (PSR-0 style) |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $classname |
|
32
|
|
|
*/ |
|
33
|
|
|
public static function autoload($classname) |
|
34
|
|
|
{ |
|
35
|
|
|
self::_autoload(dirname(dirname(__FILE__)), $classname); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Register the autoloader and any other setup needed |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function pharInit() |
|
42
|
|
|
{ |
|
43
|
|
|
spl_autoload_register(array('\Httpful\Bootstrap', 'pharAutoload')); |
|
44
|
|
|
self::registerHandlers(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Phar specific autoloader |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $classname |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function pharAutoload($classname) |
|
53
|
|
|
{ |
|
54
|
|
|
self::_autoload('phar://httpful.phar', $classname); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string base |
|
59
|
|
|
* @param string classname |
|
60
|
|
|
*/ |
|
61
|
|
|
private static function _autoload($base, $classname) |
|
62
|
|
|
{ |
|
63
|
|
|
$parts = explode(self::NS_GLUE, $classname); |
|
64
|
|
|
$path = $base . self::DIR_GLUE . implode(self::DIR_GLUE, $parts) . '.php'; |
|
65
|
|
|
|
|
66
|
|
|
if (file_exists($path)) { |
|
67
|
|
|
require_once($path); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
/** |
|
71
|
|
|
* Register default mime handlers. Is idempotent. |
|
72
|
|
|
*/ |
|
73
|
|
|
public static function registerHandlers() |
|
74
|
|
|
{ |
|
75
|
|
|
if (self::$registered === true) { |
|
76
|
|
|
return; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// @todo check a conf file to load from that instead of |
|
80
|
|
|
// hardcoding into the library? |
|
81
|
|
|
$handlers = array( |
|
82
|
|
|
\Httpful\Mime::JSON => new \Httpful\Handlers\JsonHandler(), |
|
83
|
|
|
\Httpful\Mime::XML => new \Httpful\Handlers\XmlHandler(), |
|
84
|
|
|
\Httpful\Mime::FORM => new \Httpful\Handlers\FormHandler(), |
|
85
|
|
|
\Httpful\Mime::CSV => new \Httpful\Handlers\CsvHandler(), |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
foreach ($handlers as $mime => $handler) { |
|
89
|
|
|
// Don't overwrite if the handler has already been registered |
|
90
|
|
|
if (Httpful::hasParserRegistered($mime)) |
|
91
|
|
|
continue; |
|
92
|
|
|
Httpful::register($mime, $handler); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
self::$registered = true; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|