Skip to content

Commit

Permalink
Annotations: skip protected/private methods, skip public methods with…
Browse files Browse the repository at this point in the history
…out annotations
  • Loading branch information
f3l1x committed Mar 13, 2017
1 parent dd35fbf commit 16b4daa
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Bridges/DI/Annotation/DoctrineAnnotationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,18 @@ protected function parseControllerMethodsAnnotations(SchemaController $controlle
{
// Iterate over all methods in class
foreach ($class->getMethods() as $method) {
// Append method to scheme
$schemaMethod = $controller->addMethod($method->getName());
// Skip protected/private methods
if (!$method->isPublic()) continue;

// Read method annotations
$annotations = $this->createReader()->getMethodAnnotations($method);

// Skip if method has no @Path/@Method annotations
if (count($annotations) <= 0) continue;

// Append method to scheme
$schemaMethod = $controller->addMethod($method->getName());

// Iterate over all method annotations
foreach ($annotations as $annotation) {
// Parse @Path =========================
Expand Down
8 changes: 8 additions & 0 deletions src/Bridges/DI/Annotation/NetteAnnotationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ protected function parseControllerMethodsAnnotations(SchemaController $controlle
{
// Iterate over all methods in class
foreach ($class->getMethods() as $method) {
// Skip protected/private methods
if (!$method->isPublic()) continue;

// Skip if method has no @Path/@Method annotation
if (!$method->hasAnnotation('Path')
|| !$method->hasAnnotation('Method')
) continue;

// Append method to scheme
$schemaMethod = $controller->addMethod($method->getName());

Expand Down
30 changes: 30 additions & 0 deletions tests/cases/unit/Bridges/DI/Annotation/NetteAnnotationLoader.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,33 @@ test(function () {
Assert::type(SchemaBuilder::class, $schemaBuilder);
Mockery::close();
});

// Parse annotations
test(function () {
$builder = new ContainerBuilder();
$builder->addDefinition('foobar')
->setClass(FoobarController::class);

$loader = new NetteAnnotationLoader($builder);
$schemaBuilder = $loader->load();

Assert::type(SchemaBuilder::class, $schemaBuilder);
Assert::count(1, $schemaBuilder->getControllers());

$controllers = $schemaBuilder->getControllers();
$controller = array_pop($controllers);

Assert::equal(FoobarController::class, $controller->getClass());
Assert::equal('/foobar', $controller->getRootPath());

Assert::count(2, $controller->getMethods());

Assert::equal('baz1', $controller->getMethods()['baz1']->getName());
Assert::equal('/baz1', $controller->getMethods()['baz1']->getPath());
Assert::equal(['GET'], $controller->getMethods()['baz1']->getMethods());

Assert::equal('baz2', $controller->getMethods()['baz2']->getName());
Assert::equal('/baz2', $controller->getMethods()['baz2']->getPath());

Mockery::close();
});
24 changes: 24 additions & 0 deletions tests/fixtures/Controllers/FoobarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,28 @@ public function baz2(ApiRequest $request, ApiResponse $response)
{
}

/**
* @return void
*/
public function getData1()
{
// Skip this method
}

/**
* @return void
*/
protected function getData2()
{
// Skip this method
}

/**
* @return void
*/
protected function getData3()
{
// Skip this method
}

}

0 comments on commit 16b4daa

Please sign in to comment.