-
-
Save bazo/358977a9c27d7ed92ef8 to your computer and use it in GitHub Desktop.
<?php | |
use Nette\Application\UI\Presenter; | |
use Nette\DI\CompilerExtension; | |
/** | |
* Auto register presenters as services | |
* @author Martin Bažík <[email protected]> | |
*/ | |
class PresenterExtension extends CompilerExtension | |
{ | |
private $defaults = [ | |
'robotLoaderVarName' => 'robotLoader' | |
]; | |
public function loadConfiguration() | |
{ | |
$config = $this->getConfig($this->defaults); | |
$builder = $this->getContainerBuilder(); | |
$robotLoaderVarName = $config['robotLoaderVarName']; | |
global $$robotLoaderVarName; | |
$classes = $$robotLoaderVarName->getIndexedClasses(); | |
foreach ($classes as $class => $file) { | |
$r = new ReflectionClass($class); | |
if (!$r->isAbstract() and $r->isSubclassOf(Presenter::class)) { | |
$defName = str_replace('\\', '_', $r->getName()); | |
$builder | |
->addDefinition($defName) | |
->setClass($class) | |
->setAutowired(FALSE) | |
; | |
} | |
} | |
} | |
} |
oh boys you're so demanding:) it was 5min work together with ide start. global was the fastest way to reuse existing robot loader. filips approach is better ofc.
btw you need to set ->autowired to FALSE otherwise it will fail on components without overrideen constructors. and when inject is on, nette complains about injectPrimary
Fuck, in 2.1 (which I'm using on damejidlo), this line is in the else condition branch.
The whole point of this is to not have dynamic injects, but have them compiled in the container. So I guess that in 2.2, the PresenterFactory must be hacked.
@bazo looks promising nette/application@a0333bd
@bazo Just pushing the boundaries :) Thanks for starting discussion with some specific code.
The component issue can be also solved via own BaseControl with empty __construct.
Thanks @fprochazka for extension, gonna test it soon.
@TomasVotruba in 2.2, there is addExcludedClasses
http://phpfashion.com/pet-duvodu-upgradovat-na-nette-2-2-3
Thanks both. With addExcludedClasses
and setAutowired(FALSE)
everything seems to work fine.
I've got more time for testing now and had problem:
- with inject as you wrote Filip
- and autowired property in trait. Had to made it public to make it work.
I really don't understand the output of your discussion with David. Could you sum up pls?
I've made this for myself.
Because I'm not using the robot loader in production, I have a composer classmap generated for the
app/
dir.