Lexinek

BaseControl with injects example

Sep 11th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.07 KB | None | 0 0
  1. <?php
  2.  
  3. abstract class BaseControl extends Nette\Application\UI\Control {
  4.  
  5.     protected $translator;
  6.  
  7.     public function injectTranslator(Nette\Localization\ITranslator $translator) {
  8.         $this->translator = $translator;
  9.     }
  10.  
  11.     protected function createTemplate($class = NULL) {
  12.         $template = parent::createTemplate($class);
  13.         $template->registerHelperLoader('Helpers::loader');
  14.         $template->setTranslator($this->translator);
  15.         // ...
  16.         return $template;
  17.     }
  18.  }
  19.  
  20.  
  21. final class RandomControlFactory extends ControlFactory {
  22.  
  23.     protected $dependency;
  24.  
  25.     public function __construct(RandomDependency $dependency) {
  26.         $this->dependency = $dependency;
  27.     }
  28.  
  29.     public function create() {
  30.         return new RandomControl($this->dependency);
  31.     }
  32.  
  33. }
  34.  
  35. final class RandomControl extends BaseControl {
  36.  
  37.     private $dependency;
  38.  
  39.     public function __construct(RandomDependency $dependency) {
  40.         $this->dependency = $dependency;
  41.     }
  42.  
  43.     public function attached($parent) {
  44.         // Logic ..
  45.         // I have $this->translator already
  46.     }
  47.  
  48.     public function render() {
  49.         // Render stuff
  50.     }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment