Advertisement
Tommy2002

Zugriff von IndexFactory auf ein Service �ber ServiceFactory

Oct 27th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.49 KB | None | 0 0
  1. /****************************************
  2. *Type: Config
  3. *Name: module.config.php
  4. *erstellt am: 15.10.2012
  5. *****************************************/
  6.  
  7. <?php
  8. return array(
  9.     'di' => array(
  10.         'allowed_controllers' => array(
  11.             'helloworld-index-controller'
  12.         ),
  13.         'definition' => array(
  14.             'class' => array(
  15.                 'Helloworld\Service\GreetingService' => array(
  16.                     'setLoggingService' => array(
  17.                         'required' => true
  18.                     )
  19.                 ),
  20.                 'Helloworld\Controller\IndexController' => array(
  21.                     'setGreetingService' => array(
  22.                         'required' => true
  23.                     )
  24.                 )
  25.             )
  26.         ),
  27.         'instance' => array(
  28.             'preferences' => array(
  29.                 'Helloworld\Service\LoggingServiceInterface' => 'Helloworld\Service\LoggingService'
  30.             ),
  31.             'Helloworld\Service\LoggingService' => array(
  32.                 'parameters' => array(
  33.                     'logfile' => __DIR__ . '/../../../data/log.txt'
  34.                 )
  35.             ),
  36.             'alias' => array(
  37.                 'helloworld-index-controller' => 'Helloworld\Controller\IndexController',
  38.             ),
  39.         )
  40.     ),
  41.     'view_manager' => array(
  42.         'template_path_stack' => array(
  43.             __DIR__ . '/../view'
  44.         ),
  45.     ),
  46.     'router' => array(
  47.         'routes' => array(
  48.             'sayhello' => array(
  49.                 'type'      => 'Zend\Mvc\Router\Http\Literal',
  50.                 'options'   => array(
  51.                     'route'     => '/sayhello',
  52.                     'defaults'  => array(
  53.                         'controller'    => 'Helloworld\Controller\Index',
  54.                         'action'        => 'index',
  55.                     )
  56.                 )
  57.             ),
  58.             'testForm' => array(
  59.                 'type'      => 'Zend\Mvc\Router\Http\Literal',
  60.                 'options'   => array(
  61.                     'route'     => '/testForm',
  62.                     'defaults'  => array(
  63.                         'controller'    => 'Helloworld\Controller\TestForm',
  64.                         'action'        => 'test',
  65.                     )
  66.                 )
  67.             ),
  68.             'testFormSignUp' => array(
  69.                 'type'      => 'Zend\Mvc\Router\Http\Literal',
  70.                 'options'   => array(
  71.                     'route'     => '/testForm2/signup',
  72.                     'defaults'  => array(
  73.                         'controller'    => 'Helloworld\Controller\TestForm',
  74.                         'action'        => 'signup',
  75.                     )
  76.                 )
  77.             ),
  78.         )
  79.     ),
  80.     'controllers' => array(
  81.         'invokables' => array(
  82.             'Helloworld\Controller\TestForm' => 'Helloworld\Controller\TestFormController'
  83.         ),
  84.         'factories' => array(
  85.             'Helloworld\Controller\Index' => 'Helloworld\Controller\IndexControllerFactory'
  86.         )
  87.        
  88.     ),
  89.     'view_helpers' => array(
  90.         'invokables' => array(
  91.             'displayCurrentDate' => 'Helloworld\View\Helper\DisplayCurrentDate'
  92.         )
  93.     )
  94. );
  95. ?>
  96.  
  97.  
  98. /****************************************
  99. *Type: ControllerFactory
  100. *Name: IndexControllerFactory.php
  101. *erstellt am: 15.10.2012
  102. *****************************************/
  103.  
  104. <?PHP
  105. namespace Helloworld\Controller;
  106.  
  107. use Zend\ServiceManager\FactoryInterface;
  108. use Zend\ServiceManager\ServiceLocatorInterface;
  109.  
  110. class IndexControllerFactory implements FactoryInterface
  111. {
  112.     public function createService(ServiceLocatorInterface $serviceLocator)
  113.     {
  114.         $ctr = new IndexController();
  115.         $serviceLocator = $serviceLocator->getServiceLocator();
  116.         $greetingSrv = $serviceLocator->get('Helloworld\Service\GreetingService');
  117.         $ctr->setGreetingService($greetingSrv);
  118.         return $ctr;
  119.     }
  120. }
  121.  
  122. ?>
  123.  
  124.  
  125. /****************************************
  126. *Type: Controller
  127. *Name: IndexController.php
  128. *erstellt am: 15.10.2012
  129. *****************************************/
  130.  
  131.  
  132. <?php
  133. namespace Helloworld\Controller;
  134.  
  135. use Zend\Mvc\Controller\AbstractActionController;
  136. use Zend\View\Model\ViewModel;
  137.  
  138. class IndexController extends AbstractActionController
  139. {
  140.     private $greetingService;
  141.    
  142.     public function indexAction()
  143.     {
  144.         $greetingLine = $this->greetingService->getGreeting();
  145.         return new ViewModel(
  146.                 array('greetingLine' => $greetingLine)
  147.         );
  148.     }
  149.    
  150.     public function setGreetingService($greetingService)
  151.     {
  152.         $this->greetingService = $greetingService;
  153.     }
  154.    
  155.     public function getGreetingService()
  156.     {
  157.         return $this->greetingService;
  158.     }
  159.    
  160.    
  161. }
  162. ?>
  163.  
  164.  
  165.  
  166. /****************************************
  167. *Type: ServiceFactory
  168. *Name: GreetingServiceFactory.php
  169. *erstellt am: 15.10.2012
  170. *****************************************/
  171.  
  172. <?PHP
  173. namespace Helloworld\Service;
  174.  
  175. use Zend\ServiceManager\FactoryInterface;
  176. use Zend\ServiceManager\ServiceLocatorInterface;
  177.  
  178. class GreetingServiceFactory implements FactoryInterface
  179. {
  180.     public function createService(ServiceLocatorInterface $serviceLocator)
  181.     {
  182.         $greetingService = new \Helloworld\Service\GreetingService();
  183.         $hourProvider = new \Helloworld\Service\GreetingService\HourProviderDateFunction();
  184.         $greetingService->setHourProvider($hourProvider);
  185.        
  186.         return $greetingService;
  187.     }
  188. }
  189.  
  190. ?>
  191.  
  192.  
  193.  
  194. /****************************************
  195. *Type: Service
  196. *Name: GreetingService.php
  197. *erstellt am: 15.10.2012
  198. *****************************************/
  199.  
  200. <?php
  201. namespace Helloworld\Service;
  202.  
  203. use Zend\EventManager\EventManagerInterface;
  204. use Helloworld\Service\GreetingService\HourProviderInterface;
  205.  
  206. class GreetingService
  207. {
  208.     private $eventManager;
  209.     private $loggingService;
  210.     private $hourProvider;
  211.    
  212.    
  213.     public function getGreeting()
  214.     {
  215.         if (!$this->hourProvider)
  216.             throw new \BadFunctionCallException('HourProvider not yet set.');
  217.        
  218.         $hour = $this->hourProvider->getHour();
  219.        
  220.         if($hour <= 11)
  221.             return "Good morning, world!";
  222.         else if($hour > 11 && $hour < 17)
  223.             return "Hello, world!";
  224.         else
  225.             return "Good evending, world!";
  226.     }
  227.    
  228.     public function setHourProvider(HourProviderInterface $hourProvider)
  229.     {
  230.         $this->hourProvider = $hourProvider;
  231.     }
  232.    
  233.     public function getHourProvider()
  234.     {
  235.         return $this->hourProvider;
  236.     }
  237.    
  238.     public function getEventManager()
  239.     {
  240.         return $this->eventManager;
  241.     }
  242.    
  243.     public function setEventManager(EventManagerInterface $em)
  244.     {
  245.         $this->eventManager = $em;
  246.     }
  247.    
  248.     public function setLoggingService(LoggingServiceInterface $loggingService)
  249.     {
  250.         return $this->loggingService = $loggingService;
  251.     }
  252.    
  253.     public function getLoggingService()
  254.     {
  255.         return $this->loggingService;
  256.     }
  257. }
  258. ?>
  259.  
  260.  
  261. /****************************************
  262. *Type: Interface
  263. *Name: HourProviderInterface.php
  264. *erstellt am: 15.10.2012
  265. *****************************************/
  266.  
  267. <?php
  268. namespace Helloworld\Service\GreetingService;
  269.  
  270. interface HourProviderInterface
  271. {
  272.     public function getHour();
  273. }
  274.  
  275.  
  276.  
  277. /****************************************
  278. *Type: Function
  279. *Name: HourProviderDateFunction.php
  280. *erstellt am: 15.10.2012
  281. *****************************************/
  282.  
  283. <?php
  284. namespace Helloworld\Service\GreetingService;
  285.  
  286. use Helloworld\Service\GreetingService\HourProviderInterface;
  287.  
  288. class HourProviderDateFunction implements HourProviderInterface
  289. {
  290.     public function getHour()
  291.     {
  292.         return date("H");
  293.     }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement