2013-02-14 02:42:13 +04:00
< ? php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* " AS IS " AND ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT
* LIMITED TO , THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT , INDIRECT , INCIDENTAL ,
* SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES ( INCLUDING , BUT NOT
* LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE ,
* DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT
* ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license . For more information , see
* < http :// www . doctrine - project . org >.
*/
namespace Doctrine\ORM\Cache ;
2013-10-08 02:53:32 +04:00
use Doctrine\Common\Cache\CacheProvider ;
2013-02-14 02:42:13 +04:00
use Doctrine\ORM\Cache ;
use Doctrine\ORM\Cache\Region ;
2013-10-04 07:02:42 +04:00
use Doctrine\ORM\Cache\TimestampRegion ;
2013-10-03 21:55:55 +04:00
use Doctrine\ORM\Cache\RegionsConfiguration ;
2013-02-14 02:42:13 +04:00
use Doctrine\ORM\Mapping\ClassMetadata ;
use Doctrine\ORM\EntityManagerInterface ;
use Doctrine\ORM\Cache\Region\DefaultRegion ;
use Doctrine\ORM\Cache\Region\FileLockRegion ;
2013-10-04 07:02:42 +04:00
use Doctrine\ORM\Cache\Region\UpdateTimestampCache ;
2013-02-14 02:42:13 +04:00
use Doctrine\ORM\Persisters\EntityPersister ;
use Doctrine\ORM\Persisters\CollectionPersister ;
use Doctrine\ORM\Cache\Persister\ReadOnlyCachedEntityPersister ;
use Doctrine\ORM\Cache\Persister\ReadWriteCachedEntityPersister ;
use Doctrine\ORM\Cache\Persister\ReadOnlyCachedCollectionPersister ;
use Doctrine\ORM\Cache\Persister\ReadWriteCachedCollectionPersister ;
use Doctrine\ORM\Cache\Persister\NonStrictReadWriteCachedEntityPersister ;
use Doctrine\ORM\Cache\Persister\NonStrictReadWriteCachedCollectionPersister ;
/**
* @ since 2.5
* @ author Fabio B . Silva < fabio . bat . silva @ gmail . com >
*/
class DefaultCacheFactory implements CacheFactory
{
/**
2013-10-08 02:53:32 +04:00
* @ var \Doctrine\Common\Cache\CacheProvider
2013-02-14 02:42:13 +04:00
*/
private $cache ;
/**
2013-10-03 21:55:55 +04:00
* @ var \Doctrine\ORM\Cache\RegionsConfiguration
2013-02-14 02:42:13 +04:00
*/
2013-10-03 21:55:55 +04:00
private $regionsConfig ;
2013-02-14 02:42:13 +04:00
2013-10-04 07:02:42 +04:00
/**
2013-10-17 20:11:56 +04:00
* @ var \Doctrine\ORM\Cache\TimestampRegion | null
2013-10-04 07:02:42 +04:00
*/
private $timestampRegion ;
2013-02-14 02:42:13 +04:00
/**
2013-10-17 20:11:56 +04:00
* @ var \Doctrine\ORM\Cache\Region []
2013-02-14 02:42:13 +04:00
*/
2013-10-17 20:11:56 +04:00
private $regions = array ();
2013-02-14 02:42:13 +04:00
/**
2013-10-17 20:11:56 +04:00
* @ var string | null
2013-02-14 02:42:13 +04:00
*/
private $fileLockRegionDirectory ;
/**
2013-10-03 21:55:55 +04:00
* @ param \Doctrine\ORM\Cache\RegionsConfiguration $cacheConfig
2013-10-08 02:53:32 +04:00
* @ param \Doctrine\Common\Cache\CacheProvider $cache
2013-02-14 02:42:13 +04:00
*/
2013-10-08 02:53:32 +04:00
public function __construct ( RegionsConfiguration $cacheConfig , CacheProvider $cache )
2013-02-14 02:42:13 +04:00
{
$this -> cache = $cache ;
2013-10-03 21:55:55 +04:00
$this -> regionsConfig = $cacheConfig ;
2013-02-14 02:42:13 +04:00
}
/**
* @ param string $fileLockRegionDirectory
*/
public function setFileLockRegionDirectory ( $fileLockRegionDirectory )
{
2013-10-17 20:11:56 +04:00
$this -> fileLockRegionDirectory = ( string ) $fileLockRegionDirectory ;
2013-02-14 02:42:13 +04:00
}
/**
* @ return string
*/
public function getFileLockRegionDirectory ()
{
return $this -> fileLockRegionDirectory ;
}
/**
* @ param \Doctrine\ORM\Cache\Region $region
*/
public function setRegion ( Region $region )
{
2013-12-17 00:55:54 +04:00
$this -> regions [ $region -> getName ()] = $region ;
2013-02-14 02:42:13 +04:00
}
2013-10-04 07:02:42 +04:00
/**
* @ param \Doctrine\ORM\Cache\TimestampRegion $region
*/
public function setTimestampRegion ( TimestampRegion $region )
{
2013-12-17 00:55:54 +04:00
$this -> timestampRegion = $region ;
2013-10-04 07:02:42 +04:00
}
2013-02-14 02:42:13 +04:00
/**
* { @ inheritdoc }
*/
public function buildCachedEntityPersister ( EntityManagerInterface $em , EntityPersister $persister , ClassMetadata $metadata )
{
$region = $this -> getRegion ( $metadata -> cache );
$usage = $metadata -> cache [ 'usage' ];
if ( $usage === ClassMetadata :: CACHE_USAGE_READ_ONLY ) {
return new ReadOnlyCachedEntityPersister ( $persister , $region , $em , $metadata );
}
if ( $usage === ClassMetadata :: CACHE_USAGE_NONSTRICT_READ_WRITE ) {
return new NonStrictReadWriteCachedEntityPersister ( $persister , $region , $em , $metadata );
}
if ( $usage === ClassMetadata :: CACHE_USAGE_READ_WRITE ) {
return new ReadWriteCachedEntityPersister ( $persister , $region , $em , $metadata );
}
throw new \InvalidArgumentException ( sprintf ( " Unrecognized access strategy type [%s] " , $usage ));
}
/**
* { @ inheritdoc }
*/
public function buildCachedCollectionPersister ( EntityManagerInterface $em , CollectionPersister $persister , array $mapping )
{
$usage = $mapping [ 'cache' ][ 'usage' ];
$region = $this -> getRegion ( $mapping [ 'cache' ]);
if ( $usage === ClassMetadata :: CACHE_USAGE_READ_ONLY ) {
return new ReadOnlyCachedCollectionPersister ( $persister , $region , $em , $mapping );
}
if ( $usage === ClassMetadata :: CACHE_USAGE_NONSTRICT_READ_WRITE ) {
return new NonStrictReadWriteCachedCollectionPersister ( $persister , $region , $em , $mapping );
}
if ( $usage === ClassMetadata :: CACHE_USAGE_READ_WRITE ) {
return new ReadWriteCachedCollectionPersister ( $persister , $region , $em , $mapping );
}
throw new \InvalidArgumentException ( sprintf ( " Unrecognized access strategy type [%s] " , $usage ));
}
/**
* { @ inheritdoc }
*/
public function buildQueryCache ( EntityManagerInterface $em , $regionName = null )
{
2013-12-03 01:56:16 +04:00
return new DefaultQueryCache (
$em ,
$this -> getRegion (
array (
'region' => $regionName ? : Cache :: DEFAULT_QUERY_REGION_NAME ,
'usage' => ClassMetadata :: CACHE_USAGE_NONSTRICT_READ_WRITE
)
)
);
2013-02-14 02:42:13 +04:00
}
/**
* { @ inheritdoc }
*/
public function buildCollectionHydrator ( EntityManagerInterface $em , array $mapping )
{
return new DefaultCollectionHydrator ( $em );
}
/**
* { @ inheritdoc }
*/
public function buildEntityHydrator ( EntityManagerInterface $em , ClassMetadata $metadata )
{
return new DefaultEntityHydrator ( $em );
}
/**
* { @ inheritdoc }
*/
public function getRegion ( array $cache )
{
if ( isset ( $this -> regions [ $cache [ 'region' ]])) {
return $this -> regions [ $cache [ 'region' ]];
}
2013-10-04 07:02:42 +04:00
$region = new DefaultRegion ( $cache [ 'region' ], clone $this -> cache , $this -> regionsConfig -> getLifetime ( $cache [ 'region' ]));
2013-02-14 02:42:13 +04:00
if ( $cache [ 'usage' ] === ClassMetadata :: CACHE_USAGE_READ_WRITE ) {
if ( ! $this -> fileLockRegionDirectory ) {
2013-10-17 20:11:56 +04:00
throw new \LogicException (
2013-12-03 01:56:16 +04:00
'If you what to use a "READ_WRITE" cache an implementation of "Doctrine\ORM\Cache\ConcurrentRegion" is required, ' .
2013-02-14 02:42:13 +04:00
'The default implementation provided by doctrine is "Doctrine\ORM\Cache\Region\FileLockRegion" if you what to use it please provide a valid directory, DefaultCacheFactory#setFileLockRegionDirectory(). '
);
}
$directory = $this -> fileLockRegionDirectory . DIRECTORY_SEPARATOR . $cache [ 'region' ];
2013-10-03 21:55:55 +04:00
$region = new FileLockRegion ( $region , $directory , $this -> regionsConfig -> getLockLifetime ( $cache [ 'region' ]));
2013-02-14 02:42:13 +04:00
}
return $this -> regions [ $cache [ 'region' ]] = $region ;
}
2013-10-04 07:02:42 +04:00
/**
* { @ inheritdoc }
*/
public function getTimestampRegion ()
{
if ( $this -> timestampRegion === null ) {
$name = Cache :: DEFAULT_TIMESTAMP_REGION_NAME ;
$lifetime = $this -> regionsConfig -> getLifetime ( $name );
$this -> timestampRegion = new UpdateTimestampCache ( $name , clone $this -> cache , $lifetime );
}
return $this -> timestampRegion ;
}
2013-02-14 02:42:13 +04:00
}