Skip to content

Metadata drivers #73

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
32 changes: 31 additions & 1 deletion config/doctrine.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,41 @@
<?php

return [
/** @deprecated use 'mappings.params' and set the type to 'annotations */
'simple_annotations' => false,

/** @deprecated use 'mapping.params' */
'metadata' => [
base_path('app/models')
],
/**
* Mapping configuration. Allows for any supported doctrine mapping, including your own!
*/
'mappings' => [
/**
* One of xml, yml, annotations, static_php or a custom key set below
*/
'type' => 'annotations',
/**
* Array of params passed to the driver's constructor
*/
'params' => [
/**
* All default doctine drivers expect first param to be an array of paths
*/
[base_path('app/models')]
/**
* Further params may be required (ie. annotations require the simple_annotations boolean here)
*/
],
/**
* Add custom drivers as $key => callable $factory here.
*/
'custom_drivers' => [
/**
* Note that $factory is a php callable, currently "object@method" is not supported
*/
]
],

'proxy' => [
'auto_generate' => false,
Expand Down
92 changes: 92 additions & 0 deletions src/Configuration/ConfigurationFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php namespace Mitch\LaravelDoctrine\Configuration;

use Doctrine\ORM\Cache;
use Doctrine\ORM\Configuration;
use Mitch\LaravelDoctrine\CacheManager;
use Illuminate\Contracts\Config\Repository;
use Mitch\LaravelDoctrine\Metadata\MetadataStrategyFactory;

class ConfigurationFactory
{
private $metadataFactory;
private $config;
private $cacheManager;

public function __construct(MetadataStrategyFactory $metadataFactory, Repository $config, CacheManager $cacheManager)
{
$this->metadataFactory = $metadataFactory;
$this->config = $config;
$this->cacheManager = $cacheManager;
}

/**
* Reads the config files and builds a doctrine Configuration object.
*
* @return \Doctrine\ORM\Configuration
*/
public function create()
{
$configuration = $this->createConfiguration(
$this->config->get('app.debug'),
$this->config->get('doctrine::proxy.directory')
);

if ($cacheProvider = $this->config->get('doctrine::cache_provider'))
{
$cache = $this->cacheManager->getCache($cacheProvider);

$configuration->setMetadataCacheImpl($cache);
$configuration->setQueryCacheImpl($cache);
$configuration->setResultCacheImpl($cache);
}

$this->applyMetadata($configuration);

return $configuration;
}

/**
* @param bool $isDevMode
* @param string|null $proxyDir
*
* @return \Doctrine\ORM\Configuration
*/
private function createConfiguration($isDevMode = false, $proxyDir = null)
{
$proxyDir = $proxyDir ?: sys_get_temp_dir();

$config = new Configuration();
$config->setProxyDir($proxyDir);
$config->setProxyNamespace('DoctrineProxies');
$config->setAutoGenerateProxyClasses($isDevMode);

return $config;
}

/**
* @param \Doctrine\ORM\Configuration $configuration
*
* @return void
*/
private function applyMetadata(Configuration $configuration)
{
foreach ($this->config->get('doctrine::doctrine.metadata.custom_drivers', []) as $type => $factory)
{
$this->metadataFactory->addCustomType($type, $factory);
}

$metadataStrategy = $this->metadataFactory->getStrategy(
// Defaults to annotations for BC
$this->config->get('doctrine::doctrine.mappings.driver', 'annotations'),
// Defaults to the previous setup of paths + simple_annotations
// The new params key allows the user to provide an array of arguments
// That will be passed on to the driver constructor
$this->config->get('doctrine::doctrine.mappings.params', [
$this->config->get('doctrine::doctrine.metadata'),
$this->config->get('doctrine::doctrine.simple_annotations')
])
);

$metadataStrategy->apply($configuration);
}
}
26 changes: 13 additions & 13 deletions src/LaravelDoctrineServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Auth\AuthManager;
use Illuminate\Support\ServiceProvider;
use Mitch\LaravelDoctrine\Cache;
use Mitch\LaravelDoctrine\Configuration\ConfigurationFactory;
use Mitch\LaravelDoctrine\Configuration\DriverMapper;
use Mitch\LaravelDoctrine\Configuration\SqlMapper;
use Mitch\LaravelDoctrine\Configuration\SqliteMapper;
Expand Down Expand Up @@ -96,24 +97,23 @@ private function registerEntityManager()
{
$this->app->singleton(EntityManager::class, function ($app) {
$config = $app['config']['doctrine::doctrine'];
$metadata = Setup::createAnnotationMetadataConfiguration(
$config['metadata'],
$app['config']['app.debug'],
$config['proxy']['directory'],
$app[CacheManager::class]->getCache($config['cache_provider']),
$config['simple_annotations']
);
$metadata->addFilter('trashed', TrashedFilter::class);
$metadata->setAutoGenerateProxyClasses($config['proxy']['auto_generate']);
$metadata->setDefaultRepositoryClassName($config['repository']);
$metadata->setSQLLogger($config['logger']);

/** @type ConfigurationFactory $configurationFactory */
$configurationFactory = $app->make(ConfigurationFactory::class);

$configuration = $configurationFactory->create();

$configuration->addFilter('trashed', TrashedFilter::class);
$configuration->setAutoGenerateProxyClasses($config['proxy']['auto_generate']);
$configuration->setDefaultRepositoryClassName($config['repository']);
$configuration->setSQLLogger($config['logger']);

if (isset($config['proxy']['namespace']))
$metadata->setProxyNamespace($config['proxy']['namespace']);
$configuration->setProxyNamespace($config['proxy']['namespace']);

$eventManager = new EventManager;
$eventManager->addEventListener(Events::onFlush, new SoftDeletableListener);
$entityManager = EntityManager::create($this->mapLaravelToDoctrineConfig($app['config']), $metadata, $eventManager);
$entityManager = EntityManager::create($this->mapLaravelToDoctrineConfig($app['config']), $configuration, $eventManager);
$entityManager->getFilters()->enable('trashed');
return $entityManager;
});
Expand Down
36 changes: 36 additions & 0 deletions src/Metadata/AnnotationsStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php namespace Mitch\LaravelDoctrine\Metadata;

use Doctrine\ORM\Configuration;

class AnnotationsStrategy implements MetadataStrategyInterface
{
/**
* @type array
*/
private $paths;

/**
* @type boolean
*/
private $useSimpleAnnotations;

public function __construct(array $paths, $useSimpleAnnotations)
{
$this->paths = $paths;
$this->useSimpleAnnotations = $useSimpleAnnotations;
}

/**
* @param \Doctrine\ORM\Configuration $configuration
*
* @return void
*/
public function apply(Configuration $configuration)
{
$configuration->setMetadataDriverImpl(
$configuration->newDefaultAnnotationDriver(
$this->paths, $this->useSimpleAnnotations
)
);
}
}
82 changes: 82 additions & 0 deletions src/Metadata/MetadataStrategyFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php namespace Mitch\LaravelDoctrine\Metadata;

class MetadataStrategyFactory
{
/**
* @type array
*/
private $types;

public function __construct()
{
$this->types = [
'xml' => [$this, 'xmlStrategy'],
'yml' => [$this, 'ymlStrategy'],
'annotations' => [$this, 'annotationsStrategy'],
'static_php' => [$this, 'staticPhpStrategy']
];
}

/**
* @param string $type
* @param callable $factory
*/
public function addCustomType($type, callable $factory)
{
$this->types[$type] = $factory;
}

/**
* @param string $type
* @param array $params
*
* @return \Mitch\LaravelDoctrine\Metadata\MetadataStrategyInterface
*/
public function getStrategy($type, array $params)
{
if (!array_key_exists($type, $this->types))
{
throw new \UnexpectedValueException("Driver $type is not a valid metadata driver. Please choose one of: " . implode(', ', array_keys($this->types)));
}

$factoryMethod = $this->types[$type];

return call_user_func_array($factoryMethod, $params);
}

/**
* @param array $paths
*
* @return \Mitch\LaravelDoctrine\Metadata\XmlStrategy
*/
public function xmlStrategy(array $paths)
{
return new XmlStrategy($paths);
}

/**
* @param array $paths
*
* @return \Mitch\LaravelDoctrine\Metadata\YmlStrategy
*/
public function ymlStrategy(array $paths)
{
return new YmlStrategy($paths);
}

/**
* @param array $paths
* @param $useSimpleAnnotations
*
* @return \Mitch\LaravelDoctrine\Metadata\AnnotationsStrategy
*/
public function annotationsStrategy(array $paths, $useSimpleAnnotations)
{
return new AnnotationsStrategy($paths, $useSimpleAnnotations);
}

public function staticPhpStrategy(array $paths)
{
return new StaticPhpStrategy($paths);
}
}
13 changes: 13 additions & 0 deletions src/Metadata/MetadataStrategyInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php namespace Mitch\LaravelDoctrine\Metadata;

use Doctrine\ORM\Configuration;

interface MetadataStrategyInterface
{
/**
* @param \Doctrine\ORM\Configuration $configuration
*
* @return void
*/
public function apply(Configuration $configuration);
}
27 changes: 27 additions & 0 deletions src/Metadata/StaticPhpStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php namespace Mitch\LaravelDoctrine\Metadata;

use Doctrine\ORM\Configuration;
use Doctrine\Common\Persistence\Mapping\Driver\StaticPHPDriver;

class StaticPhpStrategy implements MetadataStrategyInterface
{
/**
* @type array
*/
private $paths;

public function __construct(array $paths)
{
$this->paths = $paths;
}

/**
* @param \Doctrine\ORM\Configuration $configuration
*
* @return void
*/
public function apply(Configuration $configuration)
{
$configuration->setMetadataDriverImpl(new StaticPHPDriver($this->paths));
}
}
27 changes: 27 additions & 0 deletions src/Metadata/XmlStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php namespace Mitch\LaravelDoctrine\Metadata;

use Doctrine\ORM\Configuration;
use Doctrine\ORM\Mapping\Driver\XmlDriver;

class XmlStrategy implements MetadataStrategyInterface
{
/**
* @type array
*/
private $paths;

public function __construct(array $paths)
{
$this->paths = $paths;
}

/**
* @param \Doctrine\ORM\Configuration $configuration
*
* @return void
*/
public function apply(Configuration $configuration)
{
$configuration->setMetadataDriverImpl(new XmlDriver($this->paths));
}
}
27 changes: 27 additions & 0 deletions src/Metadata/YmlStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php namespace Mitch\LaravelDoctrine\Metadata;

use Doctrine\ORM\Configuration;
use Doctrine\ORM\Mapping\Driver\YamlDriver;

class YmlStrategy implements MetadataStrategyInterface
{
/**
* @type array
*/
private $paths;

public function __construct(array $paths)
{
$this->paths = $paths;
}

/**
* @param \Doctrine\ORM\Configuration $configuration
*
* @return void
*/
public function apply(Configuration $configuration)
{
$configuration->setMetadataDriverImpl(new YamlDriver($this->paths));
}
}
Loading