1 <?php
2 3 4 5 6 7
8
9 namespace Cross\Core;
10
11 use Cross\Auth\CookieAuth;
12 use Cross\Auth\SessionAuth;
13 use Cross\Exception\CoreException;
14 use Cross\I\HttpAuthInterface;
15 use ReflectionClass;
16
17 18 19 20 21
22 class HttpAuth
23 {
24 25 26
27 static $obj;
28
29 30 31 32 33 34 35 36 37 38 39 40 41
42 public static function factory($type = 'COOKIE', $auth_key = '')
43 {
44 if (!self::$obj) {
45 if (is_string($type)) {
46 if (strcasecmp($type, 'cookie') == 0) {
47 self::$obj = new CookieAuth($auth_key);
48 } elseif (strcasecmp($type, 'session') == 0) {
49 self::$obj = new SessionAuth($auth_key);
50 } else {
51 try {
52 $object = new ReflectionClass($type);
53 if ($object->implementsInterface('Cross\I\HttpAuthInterface')) {
54 self::$obj = $object->newInstance();
55 } else {
56 throw new CoreException('会话管理类必须实现HttpAuthInterface接口');
57 }
58 } catch (\Exception $e) {
59 throw new CoreException('Reflection ' . $e->getMessage());
60 }
61 }
62 } elseif (is_object($type)) {
63 if ($type instanceof HttpAuthInterface) {
64 self::$obj = $type;
65 } else {
66 throw new CoreException('会话管理类必须实现HttpAuthInterface接口');
67 }
68 } else {
69 throw new CoreException('无法识别的会话管理类');
70 }
71 }
72 return self::$obj;
73 }
74 }
75