Created
November 6, 2012 14:17
-
-
Save rufinus/4024970 to your computer and use it in GitHub Desktop.
ZF2 Service Layout - request for Comments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Admin; | |
return array( | |
'invokables' => array( | |
'serviceFactory' => 'cwdAdmin\Service\ServiceFactory', | |
), | |
'factories' => array( | |
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory', | |
'navigation' => 'cwdAdmin\Service\AdminNavigation', | |
'standard_identity' => function ($sm) { | |
$auth = $sm->get('cwdAdmin.auth_service'); | |
if(!$auth->hasIdentity()) { | |
$roles = array('guest'); | |
$identity = new \ZfcRbac\Identity\StandardIdentity($roles); | |
}else{ | |
$em = $sm->get('Doctrine\ORM\EntityManager'); | |
$session = $auth->getIdentity(); | |
$identity = $em->find('Admin\Entity\User', $session['userId']); | |
} | |
return $identity; | |
}, | |
'Service_I18n' => function ($sm) { | |
$factory = $sm->get('serviceFactory'); | |
$object = $factory->setUp(new \Admin\Service\I18n()); | |
return $object; | |
} | |
), | |
'initializers' => array( | |
), | |
'shared' => array( | |
) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace cwdAdmin\Service; | |
use cwdCommon\Service; | |
use Zend\InputFilter\InputFilterAwareInterface; | |
use Zend\ServiceManager\ServiceLocatorAwareInterface; | |
/** | |
* ServiceFactory | |
* | |
* @category cwdAdmin | |
* @package CWD | |
* @subpackage Service | |
**/ | |
class ServiceFactory implements ServiceLocatorAwareInterface | |
{ | |
/** | |
* @var \Zend\ServiceManager\ServiceLocatorInterface | |
*/ | |
protected $serviceLocator; | |
public function setUp(Service $object) | |
{ | |
$object->setServiceLocator($this->getServiceLocator()); | |
$object->setLogger($this->getServiceLocator()->get('Logger')); | |
$form = $object->getFormClass(); | |
$entity = $object->getEntityClass(); | |
$form->setServiceLocator($this->getServiceLocator()); | |
if($form instanceof InputFilterAwareInterface) { | |
$form->setInputFilter($this->getEntity()->getInputFilter()); | |
} | |
$form->changeViewHelper(); | |
$form->setHydrator(new \Zend\Stdlib\Hydrator\ClassMethods()); | |
$object->setEntity($entity); | |
$object->setForm($form); | |
return $object; | |
} | |
/* (non-PHPdoc) | |
* @see \Zend\ServiceManager\ServiceLocatorAwareInterface::getServiceLocator() | |
*/ | |
public function getServiceLocator() | |
{ | |
return $this->serviceLocator; | |
} | |
/* (non-PHPdoc) | |
* @see \Zend\ServiceManager\ServiceLocatorAwareInterface::setServiceLocator() | |
*/ | |
public function setServiceLocator(\Zend\ServiceManager\ServiceLocatorInterface $serviceLocator) | |
{ | |
$this->serviceLocator = $serviceLocator; | |
return $this; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace cwdCommon; | |
use cwdAdmin\Service\ServiceInterface; | |
use Zend\ServiceManager\ServiceLocatorAwareInterface; | |
use Doctrine\ORM\EntityManager; | |
use Zend\Log\LoggerInterface; | |
use cwdCommon\Doctrine\EntityManagerAwareInterface; | |
use cwdCommon\ServiceManager\LoggerAwareInterface; | |
/** | |
* The Mother of all Services :-) | |
* | |
* @package CWD\Common | |
**/ | |
abstract class Service implements ServiceInterface, LoggerAwareInterface, EntityManagerAwareInterface, ServiceLocatorAwareInterface { | |
/** | |
* @var \Zend\ServiceManager\ServiceLocatorInterface | |
*/ | |
protected $serviceLocator = NULL; | |
/** | |
* | |
* @var \cwdAdmin\Form\Form | |
*/ | |
protected $form = NULL; | |
/** | |
* | |
* @var \cwdCommon\Doctrine\Entity | |
*/ | |
protected $entity = NULL; | |
/** | |
* @var Doctrine\ORM\EntityManager | |
*/ | |
protected $em; | |
/** | |
* @var \cwdComon\Log | |
*/ | |
protected $logger; | |
public function setForm($form) { | |
$this->form = $form; | |
return $this; | |
} | |
public function setEntity($entity) { | |
$this->entity = $entity; | |
return $this; | |
} | |
/** | |
* @return \cwdAdmin\Form\Form | |
*/ | |
public function getForm() | |
{ | |
return $this->form; | |
} | |
/** | |
* @return \cwdCommon\Doctrine\Entity | |
*/ | |
public function getEntity() | |
{ | |
return $this->entity; | |
} | |
/** | |
* Find a row by ID | |
* @param string $id | |
*/ | |
public function find($id = NULL) | |
{ | |
} | |
/** | |
* persist object to db | |
*/ | |
public function persist($object) | |
{ | |
$this->getEntityManager()->persist($object); | |
$this->getEntityManager()->flush(); | |
} | |
/** | |
* Updated existing row | |
* @param array $data | |
*/ | |
public function update($data = array()) | |
{ | |
} | |
/** | |
* Get List of elements | |
* @param array $options | |
*/ | |
public function fetchAll($options = array()) | |
{ | |
} | |
/** | |
* Set Logger | |
* @param LoggerInterface $logger | |
*/ | |
public function setLogger(LoggerInterface $logger) | |
{ | |
$this->logger = $logger; | |
} | |
/** | |
* Get Logger | |
* @return \cwdCommon\Log | |
*/ | |
public function getLogger() | |
{ | |
return $this->logger; | |
} | |
/** | |
* Set EntityManager | |
* @param EntityManager $em | |
*/ | |
public function setEntityManager(EntityManager $em) | |
{ | |
$this->em = $em; | |
} | |
/** | |
* get Doctrine EntityManager | |
* @return \Doctrine\ORM\EntityManager | |
*/ | |
public function getEntityManager() | |
{ | |
if (null === $this->em) { | |
$this->em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager'); | |
} | |
return $this->em; | |
} | |
/* Get Entity Class Instance | |
* @see \cwdAdmin\Service\ServiceInterface::getEntityClass() | |
*/ | |
abstract public function getEntityClass(); | |
/* Get the FormClass Instance | |
* @see \cwdAdmin\Service\ServiceInterface::getFormClass() | |
*/ | |
abstract public function getFormClass(); | |
/* (non-PHPdoc) | |
* @see \Zend\ServiceManager\ServiceLocatorAwareInterface::getServiceLocator() | |
*/ | |
public function getServiceLocator() { | |
return $this->serviceLocator; | |
} | |
/* (non-PHPdoc) | |
* @see \Zend\ServiceManager\ServiceLocatorAwareInterface::setServiceLocator() | |
*/ | |
public function setServiceLocator(\Zend\ServiceManager\ServiceLocatorInterface $serviceLocator) { | |
$this->serviceLocator = $serviceLocator; | |
return $this; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Admin\Service; | |
use Admin\Entity\I18n as Entity; | |
use Admin\Form\I18n as Form; | |
use cwdCommon\Service; | |
class I18n extends Service { | |
/** | |
* More Service Specifc Functions | |
**/ | |
public function getFormClass() | |
{ | |
return new Form(); | |
} | |
public function getEntityClass() | |
{ | |
return new Entity(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Admin\Controller; | |
use cwdAdmin\ServiceManager\NeedLoginAwareInterface; | |
use cwdAdmin\Grid\Column\Rewrite; | |
use cwdAdmin\Grid\Column\String; | |
use cwdCommon\Controller; | |
use Zend\View\Model\ViewModel; | |
use Zend\View\Model\JsonModel; | |
use cwdAdmin\Grid\Grid; | |
class I18nController extends Controller implements NeedLoginAwareInterface | |
{ | |
public function createAction() | |
{ | |
$service = $this->getServiceLocator()->get('Service_I18n'); | |
$form = $service->getForm(); | |
$entity = $service->getEntity(); | |
$form->bind($entity); | |
if($this->request->isPost()) { | |
$form->setData($this->request->getPost()); | |
$this->getLogger()->info('Form Post', $_POST); | |
if($form->isValid()) { | |
$service->persist($entity); | |
$this->getLogger()->info($entity->getLangkey().' Successfully saved'); | |
$this->getMessenger()->setNamespace('ok')->addMessage($entity->getLangkey().' successfully saved'); | |
$this->redirect()->toUrl('/i18n/list'); | |
} | |
} | |
return new ViewModel(array('form' => $form)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Admin\Form; | |
use Zend\Form\Element\Select; | |
use cwdAdmin\Form\DisplayGroup; | |
use Zend\Form\Fieldset; | |
use Zend\InputFilter\InputFilterProviderInterface; | |
use Zend\Form\Element; | |
use cwdAdmin\Form\Form; | |
class I18n extends Form { | |
public function __construct() | |
{ | |
parent::__construct('i18n'); | |
$this->setAttribute('method', 'post'); | |
$this->addHidden('i18n_id'); | |
$name = $this->addText('langkey', 'Key', true, NULL, array('description' => '')); | |
$value = $this->addTextarea('langvalue', 'Value', false); | |
$select1 = new Select('language'); | |
$select1->setLabel('Locale'); | |
$select1->setAttributes(array( | |
'type' => 'select', | |
)); | |
$select1->setValueOptions(array('de_AT' => 'Deutsch (Österreich)')); | |
$this->add($select1); | |
$i18n = new DisplayGroup('i18nfs', 'Internationalisation'); | |
$i18n->add('i18n_id') | |
->add('language') | |
->add('langkey') | |
->add('langvalue'); | |
$this->addDisplayGroup($i18n); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Admin\Entity; | |
use Zend\InputFilter\InputFilterInterface; | |
use Zend\InputFilter\InputFilter; | |
use Zend\InputFilter\Factory as InputFactory; | |
use Doctrine\ORM\Mapping as ORM; | |
/** | |
* Admin\Entity\I18n | |
* | |
* @ORM\Table(name="i18n") | |
* @ORM\Entity | |
*/ | |
class I18n extends \cwdCommon\Doctrine\Entity | |
{ | |
/** | |
* Input Filter | |
* @var InputFilter | |
*/ | |
protected $inputFilter; | |
/** | |
* @var integer $i18nId | |
* | |
* @ORM\Column(name="i18n_id", type="integer", nullable=false) | |
* @ORM\Id | |
* @ORM\GeneratedValue(strategy="IDENTITY") | |
*/ | |
private $i18nId; | |
/** | |
* @var string $key | |
* | |
* @ORM\Column(name="langkey", type="string", length=255, nullable=false) | |
*/ | |
private $langkey; | |
/** | |
* @var string $value | |
* | |
* @ORM\Column(name="langvalue", type="string", length=9999, nullable=true) | |
*/ | |
private $langvalue; | |
/** | |
* | |
* @var string $locale | |
* | |
* @ORM\Column(name="language", type="string", length=5, nullable=false) | |
*/ | |
private $language; | |
/** | |
* @return the $key | |
*/ | |
public function getLangkey() { | |
return $this->langkey; | |
} | |
/** | |
* @return the $value | |
*/ | |
public function getLangvalue() { | |
return $this->langvalue; | |
} | |
/** | |
* @param string $key | |
*/ | |
public function setLangkey($key) { | |
$this->langkey = $key; | |
} | |
/** | |
* @param string $value | |
*/ | |
public function setLangvalue($value) { | |
$this->langvalue = $value; | |
} | |
/** | |
* @return the $i18nId | |
*/ | |
public function getI18nId() { | |
return $this->i18nId; | |
} | |
/** | |
* @return the $locale | |
*/ | |
public function getLanguage() { | |
return $this->language; | |
} | |
/** | |
* @param number $i18nId | |
*/ | |
public function setI18nId($i18nId) { | |
$this->i18nId = $i18nId; | |
} | |
/** | |
* @param field_type $locale | |
*/ | |
public function setLanguage($locale) { | |
$this->language = $locale; | |
} | |
/** | |
* Set Input Filter | |
* @param InputFilterInterface $inputFilter | |
*/ | |
public function setInputFilter(InputFilterInterface $inputFilter) | |
{ | |
throw new \Exception("Not used"); | |
} | |
public function getInputFilter() | |
{ | |
if(!$this->inputFilter) { | |
$inputFilter = new InputFilter(); | |
$factory = new InputFactory(); | |
$inputFilter->add( | |
$factory->createInput(array( | |
'name' => 'langkey', | |
'required' => true, | |
'filters' => array( | |
array('name' => 'StripTags'), | |
array('name' => 'StringTrim') | |
), | |
'validators' => array( | |
array( | |
'name' => 'StringLength', | |
'options' => array( | |
'encoding' => 'UTF-8', | |
'min' => 1, | |
'max' => 255 | |
) | |
) | |
) | |
)) | |
); | |
$inputFilter->add( | |
$factory->createInput(array( | |
'name' => 'langvalue', | |
'required' => false, | |
'filters' => array( | |
array('name' => 'StringTrim') | |
) | |
)) | |
); | |
$this->inputFilter = $inputFilter; | |
} | |
return $this->inputFilter; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment