Skip to content

Commit

Permalink
Merge pull request ZF-Commons#20 from SpiffyJr/pr/refactor
Browse files Browse the repository at this point in the history
Refactored for latest ZfcBase/ZfcUser changes.
  • Loading branch information
EvanDotPro committed Jul 2, 2012
2 parents f011ae1 + cf5f4c8 commit c9bde88
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 213 deletions.
36 changes: 10 additions & 26 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

Expand All @@ -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')
);
},
),
Expand Down
13 changes: 3 additions & 10 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)
);
11 changes: 0 additions & 11 deletions config/xml/entity/ZfcUserDoctrineORM.Entity.UserMeta.dcm.xml

This file was deleted.

23 changes: 0 additions & 23 deletions config/xml/model/ZfcUser.Model.User.dcm.xml

This file was deleted.

18 changes: 0 additions & 18 deletions config/xml/model/ZfcUser.Model.UserMeta.dcm.xml

This file was deleted.

20 changes: 20 additions & 0 deletions config/xml/zfcuser/ZfcUser.Entity.User.dcm.xml
Original file line number Diff line number Diff line change
@@ -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>
4 changes: 2 additions & 2 deletions src/ZfcUserDoctrineORM/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{}
8 changes: 0 additions & 8 deletions src/ZfcUserDoctrineORM/Entity/UserMeta.php

This file was deleted.

75 changes: 0 additions & 75 deletions src/ZfcUserDoctrineORM/Event/ResolveTargetEntityListener.php

This file was deleted.

48 changes: 37 additions & 11 deletions src/ZfcUserDoctrineORM/Mapper/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}
}
10 changes: 0 additions & 10 deletions src/ZfcUserDoctrineORM/Mapper/UserMeta.php

This file was deleted.

35 changes: 35 additions & 0 deletions src/ZfcUserDoctrineORM/Options/ModuleOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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 $this;
}

/**
* @return boolean
*/
public function getEnableDefaultEntities()
{
return $this->enableDefaultEntities;
}
}
19 changes: 0 additions & 19 deletions src/ZfcUserDoctrineORM/Repository/User.php

This file was deleted.

0 comments on commit c9bde88

Please sign in to comment.