Skip to content
Browse files

Refactored for latest ZfcBase/ZfcUser changes.

  • Loading branch information...
1 parent f011ae1 commit 6494223dd5154ab2a2bab8cf8272ec6a603e1bc4 @spiffyjr spiffyjr committed Jul 1, 2012
View
36 Module.php
@@ -3,31 +3,20 @@
namespace ZfcUserDoctrineORM;
use Doctrine\ORM\Mapping\Driver\XmlDriver;
-use Zend\EventManager\StaticEventManager;
-use ZfcUserDoctrineORM\Event\ResolveTargetEntityListener;
use ZfcUser\Module as ZfcUser;
class Module
{
public function onBootstrap($e)
{
- $app = $e->getParam('application');
- $sm = $app->getServiceManager();
- $em = $sm->get('zfcuser_doctrine_em');
- $evm = $em->getEventManager();
-
- $listener = new ResolveTargetEntityListener;
- $listener->addResolveTargetEntity(
- 'ZfcUser\Model\UserInterface',
- ZfcUser::getOption('user_model_class'),
- array()
- );
- $evm->addEventListener(\Doctrine\ORM\Events::loadClassMetadata, $listener);
+ $app = $e->getParam('application');
+ $sm = $app->getServiceManager();
+ $options = $sm->get('zfcuser_module_options');
// Add the default entity driver only if specified in configuration
- if (ZfcUser::getOption('enable_default_entities')) {
+ if ($options->getEnableDefaultEntities()) {
$chain = $sm->get('doctrine.driver.orm_default');
- $chain->addDriver(new XmlDriver(__DIR__ . '/config/xml/entity'), 'ZfcUserDoctrineORM\Entity');
+ $chain->addDriver(new XmlDriver(__DIR__ . '/config/xml/zfcuserdoctrineorm'), 'ZfcUserDoctrineORM\Entity');
}
}
@@ -53,19 +42,14 @@ public function getServiceConfiguration()
),
'factories' => array(
- 'zfcuser_user_repository' => function ($sm) {
- $mapper = $sm->get('zfcuser_user_mapper');
- return new Repository\User($mapper);
+ 'zfcuser_module_options' => function ($sm) {
+ $config = $sm->get('Configuration');
+ return new Options\ModuleOptions(isset($config['zfcuser']) ? $config['zfcuser'] : array());
},
-
'zfcuser_user_mapper' => function ($sm) {
return new \ZfcUserDoctrineORM\Mapper\User(
- $sm->get('zfcuser_doctrine_em')
- );
- },
- 'zfcuser_usermeta_mapper' => function ($sm) {
- return new \ZfcUserDoctrineORM\Mapper\UserMeta(
- $sm->get('zfcuser_doctrine_em')
+ $sm->get('zfcuser_doctrine_em'),
+ $sm->get('zfcuser_module_options')
);
},
),
View
13 config/module.config.php
@@ -2,23 +2,16 @@
return array(
'doctrine' => array(
'driver' => array(
- 'zfcuser_model' => array(
+ 'zfcuser_entity' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\XmlDriver',
- 'paths' => __DIR__ . '/xml/model'
+ 'paths' => __DIR__ . '/xml/zfcuser'
),
'orm_default' => array(
'drivers' => array(
- 'ZfcUser\Model' => 'zfcuser_model'
+ 'ZfcUser\Entity' => 'zfcuser_entity'
)
)
)
),
-
- 'zfcuser' => array(
- 'enable_default_entities' => true,
-
- 'user_model_class' => 'ZfcUserDoctrineORM\Entity\User',
- 'usermeta_model_class' => 'ZfcUserDoctrineORM\Entity\UserMeta',
- )
);
View
11 config/xml/entity/ZfcUserDoctrineORM.Entity.UserMeta.dcm.xml
@@ -1,11 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
- http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
-
- <entity name="ZfcUserDoctrineORM\Entity\UserMeta" table="user_meta">
-
- </entity>
-
-</doctrine-mapping>
View
23 config/xml/model/ZfcUser.Model.User.dcm.xml
@@ -1,23 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
- http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
-
- <mapped-superclass name="ZfcUser\Model\User" table="user">
-
- <id name="user_id" type="integer" column="user_id"><generator strategy="AUTO" /></id>
- <field name="username" column="username" type="string" unique="true" nullable="true" />
- <field name="email" column="email" type="string" unique="true" length="100" />
- <field name="display_name" column="display_name" type="string" length="50" nullable="true" />
- <field name="password" column="password" type="string" length="128" />
- <field name="last_login" column="last_login" type="datetime" nullable="true" />
- <field name="last_ip" column="last_ip" type="integer" nullable="true" />
- <field name="register_time" column="register_time" type="datetime" />
- <field name="register_ip" column="register_ip" type="integer" />
- <field name="active" column="active" type="boolean" />
- <field name="enabled" column="enabled" type="boolean" />
-
- </mapped-superclass>
-
-</doctrine-mapping>
View
18 config/xml/model/ZfcUser.Model.UserMeta.dcm.xml
@@ -1,18 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
- http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
-
- <mapped-superclass name="ZfcUser\Model\UserMeta" table="user_meta">
-
- <id name="user" association-key="true" />
- <many-to-one field="user" column="user_id" target-entity="ZfcUser\Model\User" >
- <join-column name="user_id" referenced-column-name="user_id" />
- </many-to-one>
- <id name="meta_key" type="string" column="meta_key" />
- <field name="meta" column="meta" type="text" />
-
- </mapped-superclass>
-
-</doctrine-mapping>
View
20 config/xml/zfcuser/ZfcUser.Entity.User.dcm.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
+ http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
+
+ <mapped-superclass name="ZfcUser\Entity\User" table="user">
+
+ <id name="id" type="integer" column="id">
+ <generator strategy="AUTO" />
+ </id>
+
+ <field name="username" type="string" length="255" unique="true" nullable="true" />
+ <field name="email" type="string" unique="true" length="255" />
+ <field name="displayName" column="display_name" type="string" length="50" nullable="true" />
+ <field name="password" type="string" length="128" />
+
+ </mapped-superclass>
+
+</doctrine-mapping>
View
0 ...ty/ZfcUserDoctrineORM.Entity.User.dcm.xml → ...rm/ZfcUserDoctrineORM.Entity.User.dcm.xml
File renamed without changes.
View
4 src/ZfcUserDoctrineORM/Entity/User.php
@@ -2,7 +2,7 @@
namespace ZfcUserDoctrineORM\Entity;
-use ZfcUser\Model\User as UserModel;
+use ZfcUser\Entity\User as ZfcUserEntity;
-class User extends UserModel
+class User extends ZfcUserEntity
{}
View
8 src/ZfcUserDoctrineORM/Entity/UserMeta.php
@@ -1,8 +0,0 @@
-<?php
-
-namespace ZfcUserDoctrineORM\Entity;
-
-use ZfcUser\Model\UserMeta as UserMetaModel;
-
-class UserMeta extends UserMetaModel
-{}
View
75 src/ZfcUserDoctrineORM/Event/ResolveTargetEntityListener.php
@@ -1,75 +0,0 @@
-<?php
-namespace ZfcUserDoctrineORM\Event;
-
-use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
-use Doctrine\ORM\Mapping\ClassMetadata;
-
-/**
- * ResolveTargetEntityListener
- *
- * Mechanism to overwrite interfaces or classes specified as association
- * targets.
- *
- * @author Benjamin Eberlei <kontakt@beberlei.de>
- * @since 2.2
- */
-class ResolveTargetEntityListener
-{
- /**
- * @var array
- */
- private $resolveTargetEntities = array();
-
- /**
- * Add a target-entity class name to resolve to a new class name.
- *
- * @param string $originalEntity
- * @param string $newEntity
- * @param array $mapping
- * @return void
- */
- public function addResolveTargetEntity($originalEntity, $newEntity, array $mapping)
- {
- $mapping['targetEntity'] = ltrim($newEntity, "\\");
- $this->resolveTargetEntities[ltrim($originalEntity, "\\")] = $mapping;
- }
-
- /**
- * Process event and resolve new target entity names.
- *
- * @param LoadClassMetadataEventArgs $args
- * @return void
- */
- public function loadClassMetadata(LoadClassMetadataEventArgs $args)
- {
- $cm = $args->getClassMetadata();
- foreach ($cm->associationMappings as $assocName => $mapping) {
- if (isset($this->resolveTargetEntities[$mapping['targetEntity']])) {
- $this->remapAssociation($cm, $mapping);
- }
- }
- }
-
- private function remapAssociation($classMetadata, $mapping)
- {
- $newMapping = $this->resolveTargetEntities[$mapping['targetEntity']];
- $newMapping = array_replace_recursive($mapping, $newMapping);
- $newMapping['fieldName'] = $mapping['fieldName'];
- unset($classMetadata->associationMappings[$mapping['fieldName']]);
-
- switch ($mapping['type']) {
- case ClassMetadata::MANY_TO_MANY:
- $classMetadata->mapManyToMany($newMapping);
- break;
- case ClassMetadata::MANY_TO_ONE:
- $classMetadata->mapManyToOne($newMapping);
- break;
- case ClassMetadata::ONE_TO_MANY:
- $classMetadata->mapOneToMany($newMapping);
- break;
- case ClassMetadata::ONE_TO_ONE:
- $classMetadata->mapOneToOne($newMapping);
- break;
- }
- }
-}
View
48 src/ZfcUserDoctrineORM/Mapper/User.php
@@ -4,7 +4,8 @@
use Doctrine\ORM\EntityManager;
use ZfcUser\Mapper\User as ZfcUserMapper;
-use ZfcUser\Module as ZfcUser;
+use ZfcUserDoctrineORM\Options\ModuleOptions;
+use Zend\Stdlib\Hydrator\HydratorInterface;
class User extends ZfcUserMapper
{
@@ -13,25 +14,50 @@ class User extends ZfcUserMapper
*/
protected $em;
- public function __construct(EntityManager $em)
+ /**
+ * @var \ZfcUserDoctrineORM\Options\ModuleOptions
+ */
+ protected $options;
+
+ public function __construct(EntityManager $em, ModuleOptions $options)
{
- $this->em = $em;
+ $this->em = $em;
+ $this->options = $options;
}
- public function em()
+ public function findByEmail($email)
{
- return $this->em;
+ $er = $this->em->getRepository($this->options->getUserEntityClass());
+ return $er->findOneBy(array('email' => $email));
}
- public function find($id)
+ public function findByUsername($username)
{
- $class = ZfcUser::getOption('user_model_class');
- return $this->em()->getRepository($class)->find($id);
+ $er = $this->em->getRepository($this->options->getUserEntityClass());
+ return $er->findOneBy(array('username' => $username));
}
- public function persist($model)
+ public function findById($id)
{
- $this->em()->persist($model);
- $this->em()->flush();
+ $er = $this->em->getRepository($this->options->getUserEntityClass());
+ return $er->find($id);
+ }
+
+ public function insert($entity, $tableName = null, HydratorInterface $hydrator = null)
+ {
+ return $this->persist($entity);
+ }
+
+ public function update($entity, $where = null, $tableName = null, HydratorInterface $hydrator = null)
+ {
+ return $this->persist($entity);
+ }
+
+ protected function persist($entity)
+ {
+ $this->em->persist($entity);
+ $this->em->flush();
+
+ return $entity;
}
}
View
10 src/ZfcUserDoctrineORM/Mapper/UserMeta.php
@@ -1,10 +0,0 @@
-<?php
-
-namespace ZfcUserDoctrineORM\Mapper;
-
-use ZfcUser\Mapper\UserMeta as ZfcUserMetaMapper;
-
-class UserMeta extends ZfcUserMetaMapper
-{
-
-}
View
34 src/ZfcUserDoctrineORM/Options/ModuleOptions.php
@@ -0,0 +1,34 @@
+<?php
+
+namespace ZfcUserDoctrineORM\Options;
+
+use ZfcUser\Options\ModuleOptions as BaseModuleOptions;
+
+class ModuleOptions extends BaseModuleOptions
+{
+ /**
+ * @var string
+ */
+ protected $userEntityClass = 'ZfcUserDoctrineORM\Entity\User';
+
+ /**
+ * @var bool
+ */
+ protected $enableDefaultEntities = true;
+
+ /**
+ * @param boolean $enableDefaultEntities
+ */
+ public function setEnableDefaultEntities($enableDefaultEntities)
+ {
+ $this->enableDefaultEntities = $enableDefaultEntities;
+ }
+
+ /**
+ * @return boolean
+ */
+ public function getEnableDefaultEntities()
+ {
+ return $this->enableDefaultEntities;
+ }
+}
View
19 src/ZfcUserDoctrineORM/Repository/User.php
@@ -1,19 +0,0 @@
-<?php
-
-namespace ZfcUserDoctrineORM\Repository;
-
-use ZfcUser\Module as ZfcUser;
-use ZfcUser\Repository\User as ZfcUserRepository;
-
-class User extends ZfCUserRepository
-{
- public function findByEmail($email)
- {
- $class = ZfcUser::getOption('user_model_class');
- return $this->getMapper()->em()->getRepository($class)->findOneBy(array(
- 'email' => $email
- ));
- $this->events()->trigger(__FUNCTION__ . '.post', $this, array('user' => $user));
- return $user;
- }
-}

0 comments on commit 6494223

Please sign in to comment.
Something went wrong with that request. Please try again.