1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Psi\Bridge\ObjectAgent\Doctrine\PhpcrOdm\Tests\Functional; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
6
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain; |
7
|
|
|
use Doctrine\DBAL\DriverManager; |
8
|
|
|
use Doctrine\ODM\PHPCR\Configuration; |
9
|
|
|
use Doctrine\ODM\PHPCR\DocumentManager; |
10
|
|
|
use Doctrine\ODM\PHPCR\Mapping\Driver\AnnotationDriver; |
11
|
|
|
use Doctrine\ODM\PHPCR\Mapping\Driver\XmlDriver; |
12
|
|
|
use Doctrine\ODM\PHPCR\NodeTypeRegistrator; |
13
|
|
|
use Jackalope\RepositoryFactoryDoctrineDBAL; |
14
|
|
|
use Jackalope\Transport\DoctrineDBAL\RepositorySchema; |
15
|
|
|
use PhpBench\DependencyInjection\Container; |
16
|
|
|
use PhpBench\DependencyInjection\ExtensionInterface; |
17
|
|
|
use PHPCR\SimpleCredentials; |
18
|
|
|
use Psi\Bridge\ObjectAgent\Doctrine\PhpcrOdm\PhpcrOdmAgent; |
19
|
|
|
|
20
|
|
|
class PhpcrOdmExtension implements ExtensionInterface |
21
|
|
|
{ |
22
|
|
|
public function getDefaultConfig() |
23
|
|
|
{ |
24
|
|
|
return [ |
25
|
|
|
'db_path' => __DIR__ . '/../../../../cache/test.sqlite', |
26
|
|
|
]; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function load(Container $container) |
30
|
|
|
{ |
31
|
|
|
$container->register('phpcr_odm', function (Container $container) { |
32
|
|
|
$dbPath = $container->getParameter('db_path'); |
33
|
|
|
$registerNodeTypes = false; |
34
|
|
|
|
35
|
|
|
$connection = DriverManager::getConnection([ |
36
|
|
|
'driver' => 'pdo_sqlite', |
37
|
|
|
'path' => $dbPath, |
38
|
|
|
]); |
39
|
|
|
|
40
|
|
|
// automatically setup the schema if the db doesn't exist yet. |
41
|
|
|
if (!file_exists($dbPath)) { |
42
|
|
|
if (!file_exists($dir = dirname($dbPath))) { |
43
|
|
|
mkdir($dir); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$schema = new RepositorySchema(); |
47
|
|
|
foreach ($schema->toSql($connection->getDatabasePlatform()) as $sql) { |
48
|
|
|
$connection->exec($sql); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$registerNodeTypes = true; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$factory = new RepositoryFactoryDoctrineDBAL(); |
55
|
|
|
$repository = $factory->getRepository([ |
56
|
|
|
'jackalope.doctrine_dbal_connection' => $connection, |
57
|
|
|
]); |
58
|
|
|
$session = $repository->login(new SimpleCredentials(null, null), 'default'); |
59
|
|
|
|
60
|
|
|
if ($registerNodeTypes) { |
61
|
|
|
$typeRegistrator = new NodeTypeRegistrator(); |
62
|
|
|
$typeRegistrator->registerNodeTypes($session); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$xmlDriver = new XmlDriver([__DIR__ . '/mappings']); |
66
|
|
|
$annotationDriver = new AnnotationDriver(new AnnotationReader(), [ |
67
|
|
|
__DIR__ . '/../../../vendor/doctrine/phpcr-odm/lib/Doctrine/ODM/PHPCR/Document', |
68
|
|
|
]); |
69
|
|
|
$chain = new MappingDriverChain(); |
70
|
|
|
$chain->addDriver($xmlDriver, 'Psi\Bridge\ObjectAgent\Doctrine\PhpcrOdm\Tests\Functional\Model'); |
71
|
|
|
$chain->addDriver($annotationDriver, 'Doctrine'); |
72
|
|
|
|
73
|
|
|
$config = new Configuration(); |
74
|
|
|
$config->setMetadataDriverImpl($chain); |
75
|
|
|
|
76
|
|
|
return DocumentManager::create($session, $config); |
77
|
|
|
}); |
78
|
|
|
|
79
|
|
|
$container->register('psi_object_agent.phpcr_odm', function (Container $container) { |
80
|
|
|
return new PhpcrOdmAgent($container->get('phpcr_odm')); |
81
|
|
|
}); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|