Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 0.67 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Strategy pattern in Symfony2
  2. $somePageType = new PageType(...);
  3. $this->get('page.service')->render($somePagetype);
  4.        
  5. // This is the page.service class
  6. class MyPageService {
  7.  
  8.     public function render(PageTypeInterface $page_type) {
  9.         $page_type->setContainer($this->container);
  10.  
  11.         // do stuff
  12.     }
  13. }
  14.  
  15. // This is the type strategy
  16. class MyStrategyType extends ContainerAware implements PageTypeInterface {
  17.     // you can access the container after MyPageService has injected it.
  18. }
  19.        
  20. class MyPageService {
  21.  
  22.     public function render(PageTypeInterface $page_type) {
  23.         $page_type->setService($this->container->get('my_service'));
  24.  
  25.         // do stuff
  26.     }
  27. }