Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

エンティティ継承機能を追加 #1817

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions src/Eccube/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public function initConfig()
->parseConfig('nav', $configAll, true)
->parseConfig('doctrine_cache', $configAll)
->parseConfig('http_cache', $configAll)
->parseConfig('extended_entities', $configAll)
->parseConfig('session_handler', $configAll);

return $configAll;
Expand Down Expand Up @@ -416,15 +417,13 @@ public function initDoctrine()
->directories()
->depth(0);

$ormMappings = array();
$ormMappings[] = array(
'type' => 'yml',
'namespace' => 'Eccube\Entity',
'path' => array(
__DIR__.'/Resource/doctrine',
__DIR__.'/Resource/doctrine/master',
),
$basePaths = array(
__DIR__.'/Resource/doctrine',
__DIR__.'/Resource/doctrine/master',
);
$ormMappings = array();
$pluginMappings = array();
$allPaths = array();

foreach ($finder as $dir) {

Expand All @@ -444,14 +443,27 @@ public function initDoctrine()
foreach ($config['orm.path'] as $path) {
$paths[] = $pluginBasePath.'/'.$config['code'].$path;
}
$ormMappings[] = array(
$pluginMappings[] = array(
'type' => 'yml',
'namespace' => 'Plugin\\'.$config['code'].'\\Entity',
'path' => $paths,
);
$allPaths = array_merge($allPaths, $paths);
}
}

$ormMappings[] = array(
'type' => 'yml',
'namespace' => 'Eccube\Entity',
'path' => array_merge($basePaths, $allPaths),
);

foreach ($pluginMappings as &$pluginMapping) {
$pluginMapping['path'] = array_unique(array_merge($pluginMapping['path'], $allPaths, $basePaths));
}

$ormMappings = array_merge($ormMappings, $pluginMappings);

$options = array(
'mappings' => $ormMappings
);
Expand Down Expand Up @@ -926,6 +938,17 @@ public function loadPlugin()
$this->register(new $class($this));
}
}
if (isset($config['extended_entities'])) {
foreach ($config['extended_entities'] as $entity) {
$driverChain = $this['orm.em']->getConfiguration()->getMetadataDriverImpl();
$drivers = $driverChain->getDrivers();
foreach ($drivers as $namespace => $driver) {
if ($driver instanceof \Eccube\Doctrine\ORM\Mapping\Driver\YamlDriver) {
$driver->addExtendedEntity($entity);
}
}
}
}
}
}

Expand Down
90 changes: 90 additions & 0 deletions src/Eccube/Doctrine/ORM/Mapping/Driver/YamlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,94 @@ protected function loadMappingFile($file)
{
return Yaml::parse(file_get_contents($file));
}

/*
* 以下、エンティティ拡張機構
*
* Copyright (c) by Paulius Jarmalavicius
* Released under the MIT license
* https://opensource.org/licenses/mit-license.php
*/

/**
* @var array
*/
protected $extendedEntities = array();

/**
* 継承元のエンティティを追加する
*
* @param string $extendedEntity
* @return $this
*/
public function addExtendedEntity($extendedEntity)
{
if (!in_array($extendedEntity, $this->getExtendedEntities(), true)) {
$this->extendedEntities[] = $extendedEntity;
}
return $this;
}

/**
* 継承元エンティティの配列を取得する
*
* @return array
*/
public function getExtendedEntities()
{
return $this->extendedEntities;
}

/**
* {@inheritdoc}
*/
public function getAllClassNames()
{
$driver = $this;
$classNames = parent::getAllClassNames();
$filter = function ($className) use ($driver) {
return !in_array($className, $driver->getExtendedEntities(), true);
};
return array_filter($classNames, $filter);
}

/**
* {@inheritdoc}
*/
public function isTransient($className)
{
return parent::isTransient($className) || in_array($className, $this->getExtendedEntities(), true);
}

/**
* {@inheritdoc}
*/
public function getElement($className)
{
$result = parent::getElement($className);
if (isset($result['extended_entity'])) {
$extendedElement = $this->getElement($result['extended_entity']);
unset($result['extended_entity']);
$result = $this->mergeMappings($extendedElement, $result);
}
return $result;
}

/**
* @param array $mapping1
* @param array $mapping2
* @return array
*/
protected function mergeMappings(array &$mapping1, array &$mapping2)
{
$merged = $mapping1;
foreach ($mapping2 as $key => &$value) {
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
$merged[$key] = $this->mergeMappings($merged[$key], $value);
} else {
$merged[$key] = $value;
}
}
return $merged;
}
}
12 changes: 6 additions & 6 deletions src/Eccube/Entity/AuthorityRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,32 @@ class AuthorityRole extends \Eccube\Entity\AbstractEntity
/**
* @var integer
*/
private $id;
protected $id;

/**
* @var string
*/
private $deny_url;
protected $deny_url;

/**
* @var \DateTime
*/
private $create_date;
protected $create_date;

/**
* @var \DateTime
*/
private $update_date;
protected $update_date;

/**
* @var \Eccube\Entity\Master\Authority
*/
private $Authority;
protected $Authority;

/**
* @var \Eccube\Entity\Member
*/
private $Creator;
protected $Creator;


/**
Expand Down
Loading