Skip to content

Commit

Permalink
Call checkRequirements for createComponent methods
Browse files Browse the repository at this point in the history
  • Loading branch information
enumag committed Sep 22, 2016
1 parent 21f129c commit 39dc436
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Application/UI/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,22 @@ public function checkRequirements($element)
}


/**
* Component factory. Delegates the creation of components to a createComponent<Name> method.
* @param string $name
* @return Nette\ComponentModel\IComponent the created component (optionally)
*/
protected function createComponent($name)
{
$method = 'createComponent' . ucfirst($name);
if (method_exists($this, $method)) {
$this->checkRequirements($this->getReflection()->getMethod($method));
}

return parent::createComponent($name);
}


/**
* Access to reflection.
* @return ComponentReflection
Expand Down
32 changes: 32 additions & 0 deletions tests/UI/Component.createComponent().phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Nette\Application;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


class TestSubComponent extends Application\UI\Component
{
}

class TestComponent extends Application\UI\Component
{
public $calledWith;

public function checkRequirements($element)
{
$this->calledWith = $element;
}

public function createComponentTest()
{
return new TestSubComponent;
}
}

$component = new TestComponent;
Assert::true($component->getComponent('test') instanceof TestSubComponent);
Assert::true($component->calledWith instanceof ReflectionMethod);
Assert::same('createComponentTest', $component->calledWith->getName());
Assert::same(TestComponent::class, $component->calledWith->getDeclaringClass()->getName());

0 comments on commit 39dc436

Please sign in to comment.