|
1 | 1 | <?php |
2 | 2 | namespace GraphQL\Examples\Blog; |
3 | 3 |
|
| 4 | +use Exception; |
4 | 5 | use GraphQL\Examples\Blog\Type\CommentType; |
5 | 6 | use GraphQL\Examples\Blog\Type\Enum\ContentFormatEnum; |
6 | 7 | use GraphQL\Examples\Blog\Type\Enum\ImageSizeEnumType; |
7 | 8 | use GraphQL\Examples\Blog\Type\Field\HtmlField; |
8 | 9 | use GraphQL\Examples\Blog\Type\SearchResultType; |
9 | 10 | use GraphQL\Examples\Blog\Type\NodeType; |
10 | | -use GraphQL\Examples\Blog\Type\QueryType; |
11 | 11 | use GraphQL\Examples\Blog\Type\Scalar\EmailType; |
12 | 12 | use GraphQL\Examples\Blog\Type\StoryType; |
13 | 13 | use GraphQL\Examples\Blog\Type\Scalar\UrlType; |
|
25 | 25 | */ |
26 | 26 | class Types |
27 | 27 | { |
28 | | - // Object types: |
29 | | - private static $user; |
30 | | - private static $story; |
31 | | - private static $comment; |
32 | | - private static $image; |
33 | | - private static $query; |
34 | | - |
35 | | - /** |
36 | | - * @return UserType |
37 | | - */ |
38 | | - public static function user() |
39 | | - { |
40 | | - return self::$user ?: (self::$user = new UserType()); |
41 | | - } |
42 | | - |
43 | | - /** |
44 | | - * @return StoryType |
45 | | - */ |
46 | | - public static function story() |
47 | | - { |
48 | | - return self::$story ?: (self::$story = new StoryType()); |
49 | | - } |
50 | | - |
51 | | - /** |
52 | | - * @return CommentType |
53 | | - */ |
54 | | - public static function comment() |
55 | | - { |
56 | | - return self::$comment ?: (self::$comment = new CommentType()); |
57 | | - } |
58 | | - |
59 | | - /** |
60 | | - * @return ImageType |
61 | | - */ |
62 | | - public static function image() |
63 | | - { |
64 | | - return self::$image ?: (self::$image = new ImageType()); |
65 | | - } |
66 | | - |
67 | | - /** |
68 | | - * @return QueryType |
69 | | - */ |
70 | | - public static function query() |
71 | | - { |
72 | | - return self::$query ?: (self::$query = new QueryType()); |
73 | | - } |
| 28 | + private static $types = []; |
| 29 | + const LAZY_LOAD_GRAPHQL_TYPES = true; |
74 | 30 |
|
| 31 | + public static function user() : callable { return static::get(UserType::class); } |
| 32 | + public static function story() : callable { return static::get(StoryType::class); } |
| 33 | + public static function comment() : callable { return static::get(CommentType::class); } |
| 34 | + public static function image() : callable { return static::get(ImageType::class); } |
| 35 | + public static function node() : callable { return static::get(NodeType::class); } |
| 36 | + public static function mention() : callable { return static::get(SearchResultType::class); } |
| 37 | + public static function imageSizeEnum() : callable { return static::get(ImageSizeEnumType::class); } |
| 38 | + public static function contentFormatEnum() : callable { return static::get(ContentFormatEnum::class); } |
| 39 | + public static function email() : callable { return static::get(EmailType::class); } |
| 40 | + public static function url() : callable { return static::get(UrlType::class); } |
75 | 41 |
|
76 | | - // Interface types |
77 | | - private static $node; |
78 | | - |
79 | | - /** |
80 | | - * @return NodeType |
81 | | - */ |
82 | | - public static function node() |
| 42 | + public static function get($classname) |
83 | 43 | { |
84 | | - return self::$node ?: (self::$node = new NodeType()); |
| 44 | + return static::LAZY_LOAD_GRAPHQL_TYPES ? function() use ($classname) { |
| 45 | + return static::byClassName($classname); |
| 46 | + } : static::byClassName($classname); |
85 | 47 | } |
86 | 48 |
|
| 49 | + protected static function byClassName($classname) { |
| 50 | + $parts = explode("\\", $classname); |
| 51 | + $cacheName = strtolower(preg_replace('~Type$~', '', $parts[count($parts) - 1])); |
| 52 | + $type = null; |
87 | 53 |
|
88 | | - // Unions types: |
89 | | - private static $mention; |
90 | | - |
91 | | - /** |
92 | | - * @return SearchResultType |
93 | | - */ |
94 | | - public static function mention() |
95 | | - { |
96 | | - return self::$mention ?: (self::$mention = new SearchResultType()); |
97 | | - } |
| 54 | + if (!isset(self::$types[$cacheName])) { |
| 55 | + if (class_exists($classname)) { |
| 56 | + $type = new $classname(); |
| 57 | + } |
98 | 58 |
|
| 59 | + self::$types[$cacheName] = $type; |
| 60 | + } |
99 | 61 |
|
100 | | - // Enum types |
101 | | - private static $imageSizeEnum; |
102 | | - private static $contentFormatEnum; |
| 62 | + $type = self::$types[$cacheName]; |
103 | 63 |
|
104 | | - /** |
105 | | - * @return ImageSizeEnumType |
106 | | - */ |
107 | | - public static function imageSizeEnum() |
108 | | - { |
109 | | - return self::$imageSizeEnum ?: (self::$imageSizeEnum = new ImageSizeEnumType()); |
| 64 | + if (!$type) { |
| 65 | + throw new Exception("Unknown graphql type: " . $classname); |
| 66 | + } |
| 67 | + return $type; |
110 | 68 | } |
111 | 69 |
|
112 | | - /** |
113 | | - * @return ContentFormatEnum |
114 | | - */ |
115 | | - public static function contentFormatEnum() |
| 70 | + public static function byTypeName($shortName, $removeType=true) |
116 | 71 | { |
117 | | - return self::$contentFormatEnum ?: (self::$contentFormatEnum = new ContentFormatEnum()); |
118 | | - } |
| 72 | + $cacheName = strtolower($shortName); |
| 73 | + $type = null; |
119 | 74 |
|
120 | | - // Custom Scalar types: |
121 | | - private static $urlType; |
122 | | - private static $emailType; |
| 75 | + if (isset(self::$types[$cacheName])) { |
| 76 | + return self::$types[$cacheName]; |
| 77 | + } |
123 | 78 |
|
124 | | - public static function email() |
125 | | - { |
126 | | - return self::$emailType ?: (self::$emailType = EmailType::create()); |
127 | | - } |
| 79 | + $method = lcfirst($shortName); |
| 80 | + if(method_exists(get_called_class(), $method)) { |
| 81 | + $type = self::{$method}(); |
| 82 | + } |
128 | 83 |
|
129 | | - /** |
130 | | - * @return UrlType |
131 | | - */ |
132 | | - public static function url() |
133 | | - { |
134 | | - return self::$urlType ?: (self::$urlType = new UrlType()); |
| 84 | + if(!$type) { |
| 85 | + throw new Exception("Unknown graphql type: " . $shortName); |
| 86 | + } |
| 87 | + return $type; |
135 | 88 | } |
136 | 89 |
|
137 | 90 | /** |
|
0 commit comments