Skip to content

Commit f8302b1

Browse files
committed
adding user defined namespaces for ConcreteType
1 parent f4d03c3 commit f8302b1

8 files changed

Lines changed: 142 additions & 28 deletions

File tree

src/DynamicParser.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,9 @@ protected function sanitizeKey($key)
121121
{
122122
return $key;
123123
}
124+
125+
public function registerConcreteNamespaceLookup(array $namespaces)
126+
{
127+
}
128+
124129
}

src/Interfaces/ParserInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,11 @@ interface ParserInterface
1717
* @return array
1818
*/
1919
public function parse($docBlock);
20+
21+
/**
22+
* Register set of namespaces for ConcreteType to autolookup.
23+
*
24+
* @param array $namespaces
25+
*/
26+
public function registerConcreteNamespaceLookup(array $namespaces);
2027
}

src/Parser.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,8 @@ protected function buildTypesPattern()
119119
{
120120
$this->typesPattern = '/^('.implode('|', $this->types).')(\s+)/';
121121
}
122+
123+
public function registerConcreteNamespaceLookup(array $namespaces) {
124+
$this->typeContainer->concreteType->setNamespaces($namespaces);
125+
}
122126
}

src/Types/ConcreteType.php

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,76 @@ public static function getType()
2222
return self::$instance;
2323
}
2424

25+
/**
26+
* @var array
27+
*/
28+
private $namespaceLookup = [];
29+
30+
/**
31+
* Set of user defined namespaces to lookup for class autoloading.
32+
*
33+
* @param array $namespaces
34+
*/
35+
public function setNamespaces(array $namespaces)
36+
{
37+
$this->namespaceLookup = $namespaces;
38+
}
39+
40+
/**
41+
* @param string $class
42+
*
43+
* @return string
44+
*
45+
* @throws ParserException
46+
*/
47+
protected function checkClassExistence($class)
48+
{
49+
$found = class_exists($class);
50+
$classname = $class;
51+
$i = 0;
52+
53+
while (!$found && $i < count($this->namespaceLookup)) {
54+
$classname = $this->namespaceLookup[$i] . $class;
55+
$found = class_exists($classname);
56+
$i++;
57+
}
58+
59+
if (!$found) {
60+
throw new ParserException("Concrete annotation expects '{$class}' to exist.");
61+
}
62+
63+
return $classname;
64+
}
65+
2566
/**
2667
* Process a value to be a concrete annotation
2768
*
28-
* @param string $value json string
29-
* @param string $class name of concrete annotation type (class)
30-
* @throws \Minime\Annotations\ParserException
69+
* @param string $value json string
70+
* @param string $class name of concrete annotation type (class)
71+
*
72+
* @throws ParserException
73+
*
3174
* @return object
3275
*/
3376
public function parse($value, $class = null)
3477
{
35-
if (!class_exists($class)) {
36-
throw new ParserException("Concrete annotation expects '{$class}' to exist.");
37-
}
78+
$classname = $this->checkClassExistence($class);
3879

3980
$prototype = (new JsonType)->parse($value);
4081

4182
if ($prototype instanceof stdClass) {
42-
if (! $this->isPrototypeSchemaValid($prototype)) {
83+
if (!$this->isPrototypeSchemaValid($prototype)) {
4384
throw new ParserException("Only arrays should be used to configure concrete annotation method calls.");
4485
}
4586

46-
return $this->makeInstance($class, $prototype);
87+
return $this->makeInstance($classname, $prototype);
4788
}
4889

4990
if (is_array($prototype)) {
50-
return $this->makeConstructSugarInjectionInstance($class, $prototype);
91+
return $this->makeConstructSugarInjectionInstance($classname, $prototype);
5192
}
5293

53-
throw new ParserException("Json value for annotation({$class}) must be of type object or array.");
94+
throw new ParserException("Json value for annotation({$classname}) must be of type object or array.");
5495
}
5596

5697
protected function makeConstructSugarInjectionInstance($class, array $prototype) {

test/fixtures/AnnotationsFixture.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ class AnnotationsFixture
3636
private $implicit_boolean_fixture;
3737

3838
/**
39-
* @value abc
40-
* @value "abc"
41-
* @value "abc "
39+
* @value abc
40+
* @value "abc"
41+
* @value "abc "
4242
* @value "123"
4343
*/
4444
private $string_fixture;
@@ -82,6 +82,15 @@ class AnnotationsFixture
8282
*/
8383
private $concrete_fixture;
8484

85+
/**
86+
* @AnnotationConstructInjection -> { "__construct" : ["bar"] }
87+
*
88+
* @AnnotationConstructSugarInjection -> ["foo", "bar"]
89+
*
90+
* @AnnotationSetterInjection -> { "setFoo" : ["bar"] }
91+
*/
92+
private $short_concrete_fixture;
93+
8594
/**
8695
* @SomeUndefinedClass -> {}
8796
*/
@@ -121,17 +130,17 @@ class AnnotationsFixture
121130

122131
/**
123132
* @value string abc
124-
* @value string 45
133+
* @value string 45
125134
* @value integer 45
126-
* @value integer -45
135+
* @value integer -45
127136
* @value float .45
128137
* @value float 0.45
129138
* @value float 45.
130139
* @value float -4.5
131-
* @value float 4
140+
* @value float 4
132141
* @json_value json ["x", "y"]
133142
* @json_value json {"x": {"y": "z"}}
134-
* @json_value json {"x": {"y": ["z", "p"]}}
143+
* @json_value json {"x": {"y": ["z", "p"]}}
135144
*/
136145
private $strong_typed_fixture;
137146

@@ -145,14 +154,14 @@ class AnnotationsFixture
145154
* @multiline_indented_string
146155
* ------
147156
* < moo >
148-
* ------
157+
* ------
149158
* \ ^__^
150159
* \ (oo)\_______
151160
* (__)\ )\/\
152161
* ||----w |
153162
* || ||
154163
*
155-
*
164+
*
156165
* @multiline_json json {
157166
* "x": {
158167
* "y": [
@@ -168,10 +177,10 @@ class AnnotationsFixture
168177
* @value integer
169178
* @value float
170179
* @value json
171-
* @value_with_trailing_space string
172-
* @value_with_trailing_space integer
173-
* @value_with_trailing_space float
174-
* @value_with_trailing_space json
180+
* @value_with_trailing_space string
181+
* @value_with_trailing_space integer
182+
* @value_with_trailing_space float
183+
* @value_with_trailing_space json
175184
*/
176185
private $reserved_words_as_value_fixture;
177186

test/suite/BaseTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use \ReflectionProperty;
66
use Minime\Annotations\Fixtures\AnnotationsFixture;
7+
use Minime\Annotations\Interfaces\ParserInterface;
78

89
/**
910
* BaseTest
@@ -13,6 +14,9 @@ abstract class BaseTest extends \PHPUnit_Framework_TestCase
1314
{
1415
protected $fixture;
1516

17+
/**
18+
* @var ParserInterface
19+
*/
1620
protected $parser;
1721

1822
public function setUp()

test/suite/DynamicParserTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/**
66
* DynamicParserTest
7-
*
7+
*
88
* @group parser
99
*/
1010
class DynamicParserTest extends BaseTest
@@ -153,7 +153,7 @@ public function parseMultilineValueFixture()
153153
."Suspendisse egestas orci a felis imperdiet, non consectetur est suscipit.";
154154
$this->assertSame($string, $annotations['multiline_string']);
155155

156-
$cowsay = "------\n< moo >\n------ \n \ ^__^\n ".
156+
$cowsay = "------\n< moo >\n------\n \ ^__^\n ".
157157
"\ (oo)\_______\n (__)\ )\/\\\n ".
158158
"||----w |\n || ||";
159159
$this->assertSame($cowsay, $annotations['multiline_indented_string']);

test/suite/ParserTest.php

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
namespace Minime\Annotations;
44

5-
use \ReflectionProperty;
6-
use Minime\Annotations\Fixtures\AnnotationsFixture;
7-
85
/**
96
* ParserTest
107
*
@@ -40,6 +37,14 @@ public function parseConcreteFixture()
4037
'{"foo":"bar","bar":"baz"}',
4138
json_encode($annotations['Minime\Annotations\Fixtures\AnnotationConstructInjection'][1])
4239
);
40+
$this->assertInstanceOf(
41+
'Minime\Annotations\Fixtures\AnnotationConstructSugarInjection',
42+
$annotations['Minime\Annotations\Fixtures\AnnotationConstructSugarInjection'][0]
43+
);
44+
$this->assertInstanceOf(
45+
'Minime\Annotations\Fixtures\AnnotationConstructSugarInjection',
46+
$annotations['Minime\Annotations\Fixtures\AnnotationConstructSugarInjection'][1]
47+
);
4348
$this->assertSame(
4449
'{"foo":"foo","bar":"bar"}',
4550
json_encode($annotations['Minime\Annotations\Fixtures\AnnotationConstructSugarInjection'][0])
@@ -173,4 +178,43 @@ public function testTypeRegister()
173178
$this->parser->unregisterType('\Minime\Annotations\Fixtures\FooType');
174179
$this->assertSame(['value' => 'foo bar'], $this->parser->parse($docblock));
175180
}
181+
182+
/**
183+
* @test
184+
*/
185+
public function testConcreteTypeNamespaceLookup()
186+
{
187+
$this->parser->registerConcreteNamespaceLookup([
188+
'Minime\\Annotations\\Types\\Dummy\\',
189+
'Minime\\Annotations\\Fixtures\\'
190+
]);
191+
192+
$annotations = $this->getFixture('short_concrete_fixture');
193+
194+
$this->assertInstanceOf(
195+
'Minime\Annotations\Fixtures\AnnotationConstructInjection',
196+
$annotations['AnnotationConstructInjection']
197+
);
198+
$this->assertInstanceOf(
199+
'Minime\Annotations\Fixtures\AnnotationConstructSugarInjection',
200+
$annotations['AnnotationConstructSugarInjection']
201+
);
202+
$this->assertInstanceOf(
203+
'Minime\Annotations\Fixtures\AnnotationSetterInjection',
204+
$annotations['AnnotationSetterInjection']
205+
);
206+
207+
$this->assertSame(
208+
'{"foo":"bar","bar":"baz"}',
209+
json_encode($annotations['AnnotationConstructInjection'])
210+
);
211+
$this->assertSame(
212+
'{"foo":"foo","bar":"bar"}',
213+
json_encode($annotations['AnnotationConstructSugarInjection'])
214+
);
215+
$this->assertSame(
216+
'{"foo":"bar"}',
217+
json_encode($annotations['AnnotationSetterInjection'])
218+
);
219+
}
176220
}

0 commit comments

Comments
 (0)