Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tomáš Trávníček
doctrine-dbal
Commits
a25c7042
Commit
a25c7042
authored
Mar 16, 2010
by
jwage
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[2.0] Fixing PhpDriver and covering it with tests
parent
71b1f709
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
164 additions
and
113 deletions
+164
-113
PhpDriver.php
lib/Doctrine/ORM/Mapping/Driver/PhpDriver.php
+10
-105
PhpExporter.php
lib/Doctrine/ORM/Tools/Export/Driver/PhpExporter.php
+18
-7
AbstractMappingDriverTest.php
.../Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php
+2
-1
AllTests.php
tests/Doctrine/Tests/ORM/Mapping/AllTests.php
+1
-0
PhpMappingDriverTest.php
tests/Doctrine/Tests/ORM/Mapping/PhpMappingDriverTest.php
+31
-0
Doctrine.Tests.ORM.Mapping.User.php
...Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.User.php
+102
-0
No files found.
lib/Doctrine/ORM/Mapping/Driver/PhpDriver.php
View file @
a25c7042
...
...
@@ -26,7 +26,8 @@ use Doctrine\Common\Cache\ArrayCache,
Doctrine\DBAL\Schema\AbstractSchemaManager
,
Doctrine\ORM\Mapping\ClassMetadataInfo
,
Doctrine\ORM\Mapping\MappingException
,
Doctrine\Common\Util\Inflector
;
Doctrine\Common\Util\Inflector
,
Doctrine\ORM\Mapping\Driver\AbstractFileDriver
;
/**
* The PhpDriver includes php files which just populate ClassMetadataInfo
...
...
@@ -41,126 +42,30 @@ use Doctrine\Common\Cache\ArrayCache,
* @author Jonathan H. Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
class
PhpDriver
implements
Driver
class
PhpDriver
extends
AbstractFile
Driver
{
/**
* @var array The array of class names found and the path to the file
*/
private
$_classPaths
=
array
();
/**
* The paths where to look for mapping files.
*
* @var array
*/
protected
$_paths
=
array
();
/**
* The file extension of mapping documents.
*
* @var string
* {@inheritdoc}
*/
protected
$_fileExtension
=
'.php'
;
/**
* Initializes a new PhpDriver that looks in the given path(s) for mapping
* documents and operates in the specified operating mode.
*
* @param string|array $paths One or multiple paths where mapping documents can be found.
*/
public
function
__construct
(
$paths
)
{
$this
->
addPaths
((
array
)
$paths
);
}
/**
* Append lookup paths to metadata driver.
*
* @param array $paths
*/
public
function
addPaths
(
array
$paths
)
{
$this
->
_paths
=
array_unique
(
array_merge
(
$this
->
_paths
,
$paths
));
}
/**
* Retrieve the defined metadata lookup paths.
*
* @return array
*/
public
function
getPaths
()
{
return
$this
->
_paths
;
}
/**
* Get the file extension used to look for mapping files under
*
* @return void
*/
public
function
getFileExtension
()
{
return
$this
->
_fileExtension
;
}
/**
* Set the file extension used to look for mapping files under
*
* @param string $fileExtension The file extension to set
* @return void
*/
public
function
setFileExtension
(
$fileExtension
)
{
$this
->
_fileExtension
=
$fileExtension
;
}
protected
$_metadata
;
/**
* {@inheritdoc}
*/
public
function
loadMetadataForClass
(
$className
,
ClassMetadataInfo
$metadata
)
{
$path
=
$this
->
_classPaths
[
$className
];
require_once
$path
;
$this
->
_metadata
=
$metadata
;
$this
->
_loadMappingFile
(
$this
->
_findMappingFile
(
$className
));
}
/**
* {@inheritdoc}
*/
public
function
isTransient
(
$className
)
{
return
true
;
}
/**
* {@inheritDoc}
*/
public
function
getAllClassNames
()
protected
function
_loadMappingFile
(
$file
)
{
$classes
=
array
();
if
(
$this
->
_paths
)
{
foreach
((
array
)
$this
->
_paths
as
$path
)
{
if
(
!
is_dir
(
$path
))
{
throw
MappingException
::
phpDriverRequiresConfiguredDirectoryPath
();
}
$iterator
=
new
\RecursiveIteratorIterator
(
new
\RecursiveDirectoryIterator
(
$path
),
\RecursiveIteratorIterator
::
LEAVES_ONLY
);
foreach
(
$iterator
as
$file
)
{
if
((
$fileName
=
$file
->
getBasename
(
$this
->
_fileExtension
))
==
$file
->
getBasename
())
{
continue
;
}
$classes
[]
=
$fileName
;
$this
->
_classPaths
[
$fileName
]
=
$file
->
getPathName
();
}
}
}
return
$classes
;
$metadata
=
$this
->
_metadata
;
require_once
$file
;
}
}
\ No newline at end of file
lib/Doctrine/ORM/Tools/Export/Driver/PhpExporter.php
View file @
a25c7042
...
...
@@ -80,6 +80,14 @@ class PhpExporter extends AbstractExporter
$lines
[]
=
'$metadata->setChangeTrackingPolicy(ClassMetadataInfo::CHANGETRACKING_'
.
$this
->
_getChangeTrackingPolicyString
(
$metadata
->
changeTrackingPolicy
)
.
');'
;
}
if
(
$metadata
->
lifecycleCallbacks
)
{
foreach
(
$metadata
->
lifecycleCallbacks
as
$event
=>
$callbacks
)
{
foreach
(
$callbacks
as
$callback
)
{
$lines
[]
=
"
\$
metadata->addLifecycleCallback('
$callback
', '
$event
');"
;
}
}
}
foreach
(
$metadata
->
fieldMappings
as
$fieldMapping
)
{
$lines
[]
=
'$metadata->mapField('
.
$this
->
_varExport
(
$fieldMapping
)
.
');'
;
}
...
...
@@ -89,16 +97,16 @@ class PhpExporter extends AbstractExporter
}
foreach
(
$metadata
->
associationMappings
as
$associationMapping
)
{
$cascade
=
array
(
'remove'
,
'persist'
,
'refresh'
,
'merge'
,
'detach'
);
foreach
(
$cascade
as
$key
=>
$value
)
{
if
(
!
$associationMapping
->
{
'isCascade'
.
ucfirst
(
$value
)})
{
unset
(
$cascade
[
$key
]);
}
}
$associationMappingArray
=
array
(
'fieldName'
=>
$associationMapping
->
sourceFieldName
,
'targetEntity'
=>
$associationMapping
->
targetEntityName
,
'cascade'
=>
array
(
'remove'
=>
$associationMapping
->
isCascadeRemove
,
'persist'
=>
$associationMapping
->
isCascadePersist
,
'refresh'
=>
$associationMapping
->
isCascadeRefresh
,
'merge'
=>
$associationMapping
->
isCascadeMerge
,
'detach'
=>
$associationMapping
->
isCascadeDetach
,
),
'cascade'
=>
$cascade
,
);
if
(
$associationMapping
instanceof
\Doctrine\ORM\Mapping\OneToOneMapping
)
{
...
...
@@ -115,6 +123,7 @@ class PhpExporter extends AbstractExporter
$oneToManyMappingArray
=
array
(
'mappedBy'
=>
$associationMapping
->
mappedBy
,
'orphanRemoval'
=>
$associationMapping
->
orphanRemoval
,
'orderBy'
=>
$associationMapping
->
orderBy
);
$associationMappingArray
=
array_merge
(
$associationMappingArray
,
$oneToManyMappingArray
);
...
...
@@ -123,10 +132,12 @@ class PhpExporter extends AbstractExporter
$manyToManyMappingArray
=
array
(
'mappedBy'
=>
$associationMapping
->
mappedBy
,
'joinTable'
=>
$associationMapping
->
joinTable
,
'orderBy'
=>
$associationMapping
->
orderBy
);
$associationMappingArray
=
array_merge
(
$associationMappingArray
,
$manyToManyMappingArray
);
}
$lines
[]
=
'$metadata->'
.
$method
.
'('
.
$this
->
_varExport
(
$associationMappingArray
)
.
');'
;
}
...
...
tests/Doctrine/Tests/ORM/Mapping/AbstractMappingDriverTest.php
View file @
a25c7042
...
...
@@ -157,7 +157,8 @@ abstract class AbstractMappingDriverTest extends \Doctrine\Tests\OrmTestCase
* @depends testManyToManyAssociationWithCascadeAll
* @param ClassMetadata $class
*/
public
function
testLifecycleCallbacksSupportMultipleMethodNames
(
$class
)
{
public
function
testLifecycleCallbacksSupportMultipleMethodNames
(
$class
)
{
$this
->
assertEquals
(
count
(
$class
->
lifecycleCallbacks
[
'prePersist'
]),
2
);
$this
->
assertEquals
(
$class
->
lifecycleCallbacks
[
'prePersist'
][
1
],
'doOtherStuffOnPrePersistToo'
);
...
...
tests/Doctrine/Tests/ORM/Mapping/AllTests.php
View file @
a25c7042
...
...
@@ -23,6 +23,7 @@ class AllTests
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Mapping\XmlMappingDriverTest'
);
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Mapping\YamlMappingDriverTest'
);
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Mapping\AnnotationDriverTest'
);
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Mapping\PhpMappingDriverTest'
);
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Mapping\ClassMetadataFactoryTest'
);
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Mapping\ClassMetadataLoadEventTest'
);
$suite
->
addTestSuite
(
'Doctrine\Tests\ORM\Mapping\BasicInheritanceMappingTest'
);
...
...
tests/Doctrine/Tests/ORM/Mapping/PhpMappingDriverTest.php
0 → 100644
View file @
a25c7042
<?php
namespace
Doctrine\Tests\ORM\Mapping
;
use
Doctrine\ORM\Mapping\ClassMetadata
,
Doctrine\ORM\Mapping\Driver\PhpDriver
,
Doctrine\ORM\Tools\Export\ClassMetadataExporter
;
require_once
__DIR__
.
'/../../TestInit.php'
;
class
PhpMappingDriverTest
extends
AbstractMappingDriverTest
{
protected
function
_loadDriver
()
{
$path
=
__DIR__
.
DIRECTORY_SEPARATOR
.
'php'
;
/*
// Convert YAML mapping information to PHP
// Uncomment this code if the YAML changes and you want to update the PHP code
// for the same mapping information
$cme = new ClassMetadataExporter();
$cme->addMappingSource(__DIR__ . DIRECTORY_SEPARATOR . 'yaml', 'yaml');
$exporter = $cme->getExporter('php', $path);
$exporter->setMetadatas($cme->getMetadatasForMappingSources());
$exporter->export();
*/
return
new
PhpDriver
(
$path
);
}
}
\ No newline at end of file
tests/Doctrine/Tests/ORM/Mapping/php/Doctrine.Tests.ORM.Mapping.User.php
0 → 100644
View file @
a25c7042
<?php
use
Doctrine\ORM\Mapping\ClassMetadataInfo
;
$metadata
->
setInheritanceType
(
ClassMetadataInfo
::
INHERITANCE_TYPE_NONE
);
$metadata
->
setPrimaryTable
(
array
(
'name'
=>
'cms_users'
,
));
$metadata
->
setChangeTrackingPolicy
(
ClassMetadataInfo
::
CHANGETRACKING_DEFERRED_IMPLICIT
);
$metadata
->
addLifecycleCallback
(
'doStuffOnPrePersist'
,
'prePersist'
);
$metadata
->
addLifecycleCallback
(
'doOtherStuffOnPrePersistToo'
,
'prePersist'
);
$metadata
->
addLifecycleCallback
(
'doStuffOnPostPersist'
,
'postPersist'
);
$metadata
->
mapField
(
array
(
'id'
=>
true
,
'fieldName'
=>
'id'
,
'type'
=>
'integer'
,
'columnName'
=>
'id'
,
));
$metadata
->
mapField
(
array
(
'fieldName'
=>
'name'
,
'type'
=>
'string'
,
'length'
=>
50
,
'unique'
=>
true
,
'nullable'
=>
true
,
'columnName'
=>
'name'
,
));
$metadata
->
mapField
(
array
(
'fieldName'
=>
'email'
,
'type'
=>
'string'
,
'columnName'
=>
'user_email'
,
'columnDefinition'
=>
'CHAR(32) NOT NULL'
,
));
$metadata
->
setIdGeneratorType
(
ClassMetadataInfo
::
GENERATOR_TYPE_AUTO
);
$metadata
->
mapOneToOne
(
array
(
'fieldName'
=>
'address'
,
'targetEntity'
=>
'Doctrine\\Tests\\ORM\\Mapping\\Address'
,
'cascade'
=>
array
(
0
=>
'remove'
,
),
'mappedBy'
=>
NULL
,
'joinColumns'
=>
array
(
0
=>
array
(
'name'
=>
'address_id'
,
'referencedColumnName'
=>
'id'
,
),
),
'orphanRemoval'
=>
false
,
));
$metadata
->
mapOneToMany
(
array
(
'fieldName'
=>
'phonenumbers'
,
'targetEntity'
=>
'Doctrine\\Tests\\ORM\\Mapping\\Phonenumber'
,
'cascade'
=>
array
(
1
=>
'persist'
,
),
'mappedBy'
=>
'user'
,
'orphanRemoval'
=>
false
,
'orderBy'
=>
array
(
'number'
=>
'ASC'
,
),
));
$metadata
->
mapManyToMany
(
array
(
'fieldName'
=>
'groups'
,
'targetEntity'
=>
'Doctrine\\Tests\\ORM\\Mapping\\Group'
,
'cascade'
=>
array
(
0
=>
'remove'
,
1
=>
'persist'
,
2
=>
'refresh'
,
3
=>
'merge'
,
4
=>
'detach'
,
),
'mappedBy'
=>
NULL
,
'joinTable'
=>
array
(
'name'
=>
'cms_users_groups'
,
'joinColumns'
=>
array
(
0
=>
array
(
'name'
=>
'user_id'
,
'referencedColumnName'
=>
'id'
,
'unique'
=>
false
,
'nullable'
=>
false
,
),
),
'inverseJoinColumns'
=>
array
(
0
=>
array
(
'name'
=>
'group_id'
,
'referencedColumnName'
=>
'id'
,
'columnDefinition'
=>
'INT NULL'
,
),
),
),
'orderBy'
=>
NULL
,
));
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment