forked from christiankerl/WebServiceBundle
-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathAnnotationClassLoader.php
More file actions
186 lines (158 loc) · 6.42 KB
/
AnnotationClassLoader.php
File metadata and controls
186 lines (158 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/*
* This file is part of the BeSimpleSoap.
*
* (c) Christian Kerl <christian-kerl@web.de>
* (c) Francis Besset <francis.besset@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapBundle\ServiceDefinition\Loader;
use BeSimple\SoapBundle\ServiceDefinition as Definition;
use BeSimple\SoapBundle\ServiceDefinition\Annotation;
use BeSimple\SoapCommon\Definition\Type\ComplexType;
use BeSimple\SoapCommon\Definition\Type\TypeRepository;
use Doctrine\Common\Annotations\Reader;
use Symfony\Component\Config\Loader\Loader;
/**
* AnnotationClassLoader loads ServiceDefinition from a PHP class and its methods.
*
* Based on \Symfony\Component\Routing\Loader\AnnotationClassLoader
*
* @author Christian Kerl <christian-kerl@web.de>
* @author Francis Besset <francis.besset@gmail.com>
*/
class AnnotationClassLoader extends Loader
{
protected $reader;
protected $typeRepository;
/**
* Constructor.
*
* @param \Doctrine\Common\Annotations\Reader $reader
*/
public function __construct(Reader $reader, TypeRepository $typeRepository)
{
$this->reader = $reader;
$this->typeRepository = $typeRepository;
}
/**
* Loads a ServiceDefinition from annotations from a class.
*
* @param string $class A class name
* @param string $type The resource type
*
* @return \BeSimple\SoapBundle\ServiceDefinition\ServiceDefinition A ServiceDefinition instance
*
* @throws \InvalidArgumentException When route can't be parsed
*/
public function load($class, $type = null)
{
if (!class_exists($class)) {
throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
}
$class = new \ReflectionClass($class);
$definition = new Definition\Definition($this->typeRepository);
$sharedHeaders = array();
foreach ($this->reader->getClassAnnotations($class) as $annotation) {
if ($annotation instanceof Annotation\Header) {
$sharedHeaders[$annotation->getValue()] = $this->loadType($annotation->getPhpType());
}
}
foreach ($class->getMethods() as $method) {
$serviceHeaders = $sharedHeaders;
$serviceArguments = array();
$serviceMethod =
$serviceReturn = null;
foreach ($this->reader->getMethodAnnotations($method) as $annotation) {
if ($annotation instanceof Annotation\Header) {
$serviceHeaders[$annotation->getValue()] = $this->loadType($annotation->getPhpType());
} elseif ($annotation instanceof Annotation\Param) {
$serviceArguments[$annotation->getValue()] = $this->loadType($annotation->getPhpType());
} elseif ($annotation instanceof Annotation\Method) {
if ($serviceMethod) {
throw new \LogicException(sprintf('@Soap\Method defined twice for "%s".', $method->getName()));
}
$serviceMethod = new Definition\Method(
$annotation->getValue(),
$this->getController($class, $method, $annotation)
);
} elseif ($annotation instanceof Annotation\Result) {
if ($serviceReturn) {
throw new \LogicException(sprintf('@Soap\Result defined twice for "%s".', $method->getName()));
}
$serviceReturn = $annotation->getPhpType();
}
}
if (!$serviceMethod && (!empty($serviceArguments) || $serviceReturn)) {
throw new \LogicException(sprintf('@Soap\Method non-existent for "%s".', $method->getName()));
}
if ($serviceMethod) {
foreach ($serviceHeaders as $name => $type) {
$serviceMethod->addHeader($name, $type);
}
foreach ($serviceArguments as $name => $type) {
$serviceMethod->addInput($name, $type);
}
if (!$serviceReturn) {
throw new \LogicException(sprintf('@Soap\Result non-existent for "%s".', $method->getName()));
}
$serviceMethod->setOutput($this->loadType($serviceReturn));
$definition->addMethod($serviceMethod);
}
}
return $definition;
}
/**
* @param \ReflectionMethod $method
* @param \BeSimple\SoapBundle\ServiceDefinition\Annotation\Method $annotation
*
* @return string
*/
private function getController(\ReflectionClass $class, \ReflectionMethod $method, Annotation\Method $annotation)
{
if(null !== $annotation->getService()) {
return $annotation->getService() . ':' . $method->name;
} else {
return $class->name . '::' . $method->name;
}
}
private function loadType($phpType)
{
if (false !== $arrayOf = $this->typeRepository->getArrayOf($phpType)) {
$this->loadType($arrayOf);
}
if (!$this->typeRepository->hasType($phpType)) {
$complexTypeResolver = $this->resolve($phpType, 'annotation_complextype');
if (!$complexTypeResolver) {
throw new \Exception();
}
$loaded = $complexTypeResolver->load($phpType);
$complexType = new ComplexType($phpType, isset($loaded['alias']) ? $loaded['alias'] : $phpType);
$this->typeRepository->addComplexType($complexType);
foreach ($loaded['properties'] as $name => $property) {
$complexType->add($name, $this->loadType($property->getValue()), $property->isNillable());
}
}
return $phpType;
}
/**
* Returns true if this class supports the given resource.
*
* @param mixed $resource A resource
* @param string $type The resource type
*
* @return Boolean True if this class supports the given resource, false otherwise
*/
public function supports($resource, $type = null)
{
return is_string($resource) && preg_match('/^(?:\\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+$/', $resource) && (!$type || 'annotation' === $type);
}
/**
* @return null
*/
public function getResolver()
{
}
}