1
0
mirror of synced 2024-12-05 03:06:05 +03:00

[2.0] Refactored classloader architecture. Renamed ClassLoader => GlobalClassLoader. Introduced IsolatedClassLoader that is suitable for participating in autoload stacks. Added 2 example entity classes to the sandbox with 2 xml mappings and 2 yaml mappings. Simplified sandbox setup.

This commit is contained in:
romanb 2009-09-11 19:50:48 +00:00
parent 5198776429
commit 320d21e2be
21 changed files with 522 additions and 264 deletions

View File

@ -1,184 +0,0 @@
<?php
/*
* $Id$
*
* 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 LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\Common;
/**
* A class loader used to load class files on demand.
*
* IMPORTANT:
*
* This class loader is NOT meant to be put into a "chain" of autoloaders.
* It is not meant to load only Doctrine class file. It is a one-classloader-for-all-classes
* solution. It may not be useable with frameworks that do not follow basic pear/zend
* conventions where the namespace+class name reflects the physical location of the source
* file. This is not a bug. This class loader is, however, compatible with the
* old namespace separator '_' (underscore), so any classes using that convention
* instead of the 5.3 builtin namespaces can be loaded as well.
*
* The only way to put this classloader into an autoloader chain is to use
* setCheckFileExists(true) which is not recommended.
*
* Here is the recommended usage:
* 1) Use only 1 class loader instance.
* 2) Reduce the include_path to only the path to the PEAR packages.
* 2) Set the base paths to any non-pear class libraries through
* $classLoader->setBasePath($prefix, $basePath);
* 3) DO NOT setCheckFileExists(true). Doing so is expensive in terms of performance.
* 4) Use an opcode-cache (i.e. APC) (STRONGLY RECOMMENDED).
*
* The "prefix" is the part of a class name before the very first namespace separator
* character. If the class is named "Foo_Bar_Baz" then the prefix is "Foo".
*
* If no base path is configured for a certain class prefix, the classloader relies on
* the include_path. That's why classes of any pear packages can be loaded without
* registering their base paths. However, since a long include_path has a negative effect
* on performance it is recommended to have only the path to the pear packages in the
* include_path.
*
* For any other class libraries you always have the choice between registering the base
* path on the classloader or putting the base path into the include_path. The former
* should be preferred but the latter is fine also.
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @since 2.0
* @version $Revision: 3938 $
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
class ClassLoader
{
/**
* @var string Namespace separator
*/
private $_namespaceSeparator = '\\';
/**
* @var string File extension used for classes
*/
private $_fileExtension = '.php';
/**
* @var boolean Flag to inspect if file exists in codebase before include it
*/
private $_checkFileExists = false;
/**
* @var array Hashmap of base paths that Autoloader will look into
*/
private $_basePaths = array();
/**
* Constructor registers the autoloader automatically
*
*/
public function __construct()
{
spl_autoload_register(array($this, 'loadClass'));
}
/**
* Set check file exists
*
* @param boolean $bool
* @return void
*/
public function setCheckFileExists($bool)
{
$this->_checkFileExists = $bool;
}
/**
* Set class file extension
*
* @param string $extension
* @return void
*/
public function setClassFileExtension($extension)
{
$this->_fileExtension = $extension;
}
/**
* Sets the namespace separator to use.
*
* @param string $separator
* @return void
*/
public function setNamespaceSeparator($separator)
{
$this->_namespaceSeparator = $separator;
}
/**
* Sets a static base path for classes with a certain prefix that is prepended
* to the path derived from the class itself.
*
* @param string $classPrefix
* @param string $basePath
*/
public function setBasePath($classPrefix, $basePath)
{
$this->_basePaths[$classPrefix] = $basePath;
}
/**
* Loads the given class or interface.
*
* @param string $classname The name of the class to load.
* @return boolean TRUE if the class has been successfully loaded, FALSE otherwise.
*/
public function loadClass($className)
{
if (class_exists($className, false) || interface_exists($className, false)) {
return false;
}
$prefix = '';
$separator = $this->_namespaceSeparator;
if (($pos = strpos($className, $this->_namespaceSeparator)) !== false) {
$prefix = substr($className, 0, strpos($className, $this->_namespaceSeparator));
} else if (($pos = strpos($className, '_')) !== false) {
// Support for '_' namespace separator for compatibility with Zend/PEAR/...
$prefix = substr($className, 0, strpos($className, '_'));
$separator = '_';
}
// If we have a custom path for namespace, use it
$class = ((isset($this->_basePaths[$prefix])) ? $this->_basePaths[$prefix] . DIRECTORY_SEPARATOR : '')
. str_replace($separator, DIRECTORY_SEPARATOR, $className) . $this->_fileExtension;
// Assure file exists in codebase before require if flag is active
if ($this->_checkFileExists) {
if (($fh = @fopen($class, 'r', true)) === false) {
return false;
}
@fclose($fh);
}
require $class;
return true;
}
}

View File

@ -0,0 +1,136 @@
<?php
/*
* $Id$
*
* 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 LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\Common;
/**
* A <tt>GlobalClassLoader</tt> is an autoloader for class files that can be
* installed on the SPL autoload stack. A GlobalClassLoader must be the only
* autoloader on the stack and be used for all classes.
*
* The <tt>GlobalClassLoader</tt> assumes the PHP 5.3 namespace separator but
* is also compatible with the underscore "_" namespace separator.
*
* A recommended class loading setup for optimal performance looks as follows:
*
* 1) Use a GlobalClassLoader.
* 2) Reduce the include_path to only the path to the PEAR packages.
* 2) Register the namespaces of any other (non-pear) class library with their
* absolute base paths, like this: $gcl->registerNamespace('Zend', '/path/to/zf-lib');
*
* If no base path is configured for a certain namespace, the GlobalClassLoader relies on
* the include_path.
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @since 2.0
* @version $Revision: 3938 $
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
class GlobalClassLoader
{
/**
* @var string File extension used for classes
*/
private $_defaultFileExtension = '.php';
/**
* @var array The custom file extensions of class libraries.
*/
private $_fileExtensions = array();
/**
* @var array Hashmap of base paths to class libraries.
*/
private $_basePaths = array();
/**
* Installs this class loader on the SPL autoload stack as the only class loader.
*
* @throws DoctrineException If the SPL autoload stack already contains other autoloaders.
*/
public function register()
{
if (spl_autoload_functions() !== false) {
throw new DoctrineException("Autoload stack is not empty. GlobalClassLoader does not work "
. "in an autoload stack.");
}
spl_autoload_register(array($this, 'loadClass'));
}
/**
* Sets the default file extension of class files.
*
* @param string $extension
* @return void
*/
public function setDefaultFileExtension($extension)
{
$this->_fileExtension = $extension;
}
/**
* Sets a static base path for classes with a certain prefix that is prepended
* to the path derived from the class itself.
*
* @param string $classPrefix The prefix (root namespace) of the class library.
* @param string $basePath The base path to the class library.
* @param string $fileExtension The custom file extension used by the class files in the namespace.
*/
public function registerNamespace($namespace, $basePath, $fileExtension = null)
{
$this->_basePaths[$namespace] = $basePath;
if ($fileExtension !== null) {
$this->_fileExtensions[$namespace] = $fileExtension;
}
}
/**
* Loads the given class or interface.
*
* @param string $classname The name of the class to load.
* @return boolean TRUE if the class has been successfully loaded, FALSE otherwise.
*/
public function loadClass($className)
{
$prefix = '';
$separator = '\\';
if (($pos = strpos($className, $separator)) !== false) {
$prefix = substr($className, 0, strpos($className, $separator));
} else if (($pos = strpos($className, '_')) !== false) {
// Support for '_' namespace separator for compatibility with Zend/PEAR/...
$prefix = substr($className, 0, strpos($className, '_'));
$separator = '_';
}
// Build the class file name
$class = ((isset($this->_basePaths[$prefix])) ?
$this->_basePaths[$prefix] . DIRECTORY_SEPARATOR : '')
. str_replace($separator, DIRECTORY_SEPARATOR, $className)
. (isset($this->_fileExtensions[$prefix]) ?
$this->_fileExtensions[$prefix] : $this->_defaultFileExtension);
require $class;
}
}

View File

@ -0,0 +1,117 @@
<?php
/*
* $Id$
*
* 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 LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\Common;
/**
* An <tt>IsolatedClassLoader</tt> is an autoloader for class files that can be
* installed on the SPL autoload stack. It is a class loader that loads only classes
* of a specific namespace and is suitable for working together with other autoloaders
* in the SPL autoload stack.
*
* If no base path is configured, an IsolatedClassLoader relies on the include_path.
*
* @author Roman Borschel <roman@code-factory.org>
* @since 2.0
*/
class IsolatedClassLoader
{
private $_fileExtension = '.php';
private $_namespace;
private $_basePath;
private $_namespaceSeparator = '\\';
/**
* Creates a new <tt>IsolatedClassLoader</tt> that loads classes of the
* specified namespace.
*
* @param string $ns The namespace to use.
*/
public function __construct($ns)
{
$this->_namespace = $ns;
}
/**
* Sets the namespace separator used by classes in the namespace of this class loader.
*
* @param string $sep The separator to use.
*/
public function setNamespaceSeparator($sep)
{
$this->_namespaceSeparator = $sep;
}
/**
* Sets the base include path for all class files in the namespace of this class loader.
*
* @param string $basePath
*/
public function setBasePath($basePath)
{
$this->_basePath = $basePath;
}
/**
* Sets the file extension of class files in the namespace of this class loader.
*
* @param string $fileExtension
*/
public function setFileExtension($fileExtension)
{
$this->_fileExtension = $fileExtension;
}
/**
* Installs this class loader on the SPL autoload stack.
*/
public function register()
{
spl_autoload_register(array($this, 'loadClass'));
}
/**
* Loads the given class or interface.
*
* @param string $classname The name of the class to load.
* @return boolean TRUE if the class has been successfully loaded, FALSE otherwise.
*/
public function loadClass($className)
{
if (class_exists($className, false) || interface_exists($className, false)) {
return false;
}
if (strpos($className, $this->_namespace) !== 0) {
return false;
}
// Build the class file name
$class = ($this->_basePath !== null ? $this->_basePath . DIRECTORY_SEPARATOR : '')
. str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $className)
. $this->_fileExtension;
require $class;
return true;
}
}

View File

@ -113,6 +113,7 @@ class ClassMetadataFactory
$this->_loadMetadata($className); $this->_loadMetadata($className);
} }
} }
return $this->_loadedMetadata[$className]; return $this->_loadedMetadata[$className];
} }
@ -158,7 +159,7 @@ class ClassMetadataFactory
} }
$parentClasses = array_reverse($parentClasses); $parentClasses = array_reverse($parentClasses);
$parentClasses[] = $name; $parentClasses[] = $name;
// Move down the hierarchy of parent classes, starting from the topmost class // Move down the hierarchy of parent classes, starting from the topmost class
$parent = null; $parent = null;
$visited = array(); $visited = array();
@ -184,7 +185,7 @@ class ClassMetadataFactory
$class->setVersionField($parent->versionField); $class->setVersionField($parent->versionField);
$class->setDiscriminatorMap($parent->discriminatorMap); $class->setDiscriminatorMap($parent->discriminatorMap);
} }
// Invoke driver // Invoke driver
$this->_driver->loadMetadataForClass($className, $class); $this->_driver->loadMetadataForClass($className, $class);

View File

@ -21,7 +21,8 @@
namespace Doctrine\ORM\Mapping\Driver; namespace Doctrine\ORM\Mapping\Driver;
use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Mapping\ClassMetadata,
Doctrine\Common\DoctrineException;
if ( ! class_exists('sfYaml', false)) { if ( ! class_exists('sfYaml', false)) {
require_once __DIR__ . '/../../../../vendor/sfYaml/sfYaml.class.php'; require_once __DIR__ . '/../../../../vendor/sfYaml/sfYaml.class.php';
@ -48,9 +49,9 @@ class YamlDriver extends AbstractFileDriver
public function loadMetadataForClass($className, ClassMetadata $metadata) public function loadMetadataForClass($className, ClassMetadata $metadata)
{ {
$class = $metadata->getReflectionClass(); $class = $metadata->getReflectionClass();
$element = $this->getElement($className); $element = $this->getElement($className);
if ($element['type'] == 'entity') { if ($element['type'] == 'entity') {
$metadata->setCustomRepositoryClass( $metadata->setCustomRepositoryClass(
isset($element['repositoryClass']) ? $xmlRoot['repositoryClass'] : null isset($element['repositoryClass']) ? $xmlRoot['repositoryClass'] : null

View File

@ -1286,10 +1286,10 @@ class Parser
if ($glimpse['value'] != '.') { if ($glimpse['value'] != '.') {
$token = $this->_lexer->lookahead; $token = $this->_lexer->lookahead;
$resultVariable = $this->ResultVariable(); $expr = $this->ResultVariable();
// Check if ResultVariable is defined in query components // Check if ResultVariable is defined in query components
$queryComponent = $this->_validateIdentificationVariable($resultVariable, null, $token); $queryComponent = $this->_validateIdentificationVariable($expr, null, $token);
// Outer defininition used in inner subselect is not enough. // Outer defininition used in inner subselect is not enough.
// ResultVariable exists in queryComponents, check nesting level // ResultVariable exists in queryComponents, check nesting level
@ -1562,10 +1562,10 @@ class Parser
if ($this->_lexer->isNextToken(Lexer::T_IDENTIFIER)) { if ($this->_lexer->isNextToken(Lexer::T_IDENTIFIER)) {
$token = $this->_lexer->lookahead; $token = $this->_lexer->lookahead;
$resultVariable = $this->ResultVariable(); $fieldAliasIdentificationVariable = $this->ResultVariable();
// Include ResultVariable in query components. // Include ResultVariable in query components.
$this->_queryComponents[$resultVariable] = array( $this->_queryComponents[$fieldAliasIdentificationVariable] = array(
'resultvariable' => $expression, 'resultvariable' => $expression,
'nestingLevel' => $this->_nestingLevel, 'nestingLevel' => $this->_nestingLevel,
'token' => $token, 'token' => $token,

View File

@ -59,6 +59,9 @@ class SqlWalker implements TreeWalker
private $_query; private $_query;
private $_dqlToSqlAliasMap = array(); private $_dqlToSqlAliasMap = array();
/** Map from result variable names to their SQL column alias names. */
private $_scalarResultAliasMap = array();
/** Map of all components/classes that appear in the DQL query. */ /** Map of all components/classes that appear in the DQL query. */
private $_queryComponents; private $_queryComponents;
@ -581,13 +584,19 @@ class SqlWalker implements TreeWalker
public function walkOrderByItem($orderByItem) public function walkOrderByItem($orderByItem)
{ {
$expr = $orderByItem->expression; $expr = $orderByItem->expression;
$parts = $expr->parts; if ($expr instanceof AST\PathExpression) {
$dqlAlias = $expr->identificationVariable; $parts = $expr->parts;
$class = $this->_queryComponents[$dqlAlias]['metadata']; $dqlAlias = $expr->identificationVariable;
$columnName = $class->getQuotedColumnName($parts[0], $this->_platform); $class = $this->_queryComponents[$dqlAlias]['metadata'];
$columnName = $class->getQuotedColumnName($parts[0], $this->_platform);
return $this->getSqlTableAlias($class->getTableName(), $dqlAlias) . '.'
. $columnName . ' ' . strtoupper($orderByItem->type); return $this->getSqlTableAlias($class->getTableName(), $dqlAlias) . '.'
. $columnName . ' ' . strtoupper($orderByItem->type);
} else {
$columnName = $this->_queryComponents[$expr]['token']['value'];
return $this->_scalarResultAliasMap[$columnName] . ' ' . strtoupper($orderByItem->type);
}
} }
/** /**
@ -762,6 +771,7 @@ class SqlWalker implements TreeWalker
$columnAlias = 'sclr' . $this->_aliasCounter++; $columnAlias = 'sclr' . $this->_aliasCounter++;
$sql .= $this->walkAggregateExpression($expr) . ' AS ' . $columnAlias; $sql .= $this->walkAggregateExpression($expr) . ' AS ' . $columnAlias;
$this->_scalarResultAliasMap[$resultAlias] = $columnAlias;
$columnAlias = $this->_platform->getSqlResultCasing($columnAlias); $columnAlias = $this->_platform->getSqlResultCasing($columnAlias);
$this->_rsm->addScalarResult($columnAlias, $resultAlias); $this->_rsm->addScalarResult($columnAlias, $resultAlias);
@ -773,9 +783,10 @@ class SqlWalker implements TreeWalker
} else { } else {
$resultAlias = $selectExpression->fieldIdentificationVariable; $resultAlias = $selectExpression->fieldIdentificationVariable;
} }
$columnAlias = 'sclr' . $this->_aliasCounter++; $columnAlias = 'sclr' . $this->_aliasCounter++;
$sql .= $this->walkFunction($expr) . ' AS ' . $columnAlias; $sql .= $this->walkFunction($expr) . ' AS ' . $columnAlias;
$this->_scalarResultAliasMap[$resultAlias] = $columnAlias;
$columnAlias = $this->_platform->getSqlResultCasing($columnAlias); $columnAlias = $this->_platform->getSqlResultCasing($columnAlias);
$this->_rsm->addScalarResult($columnAlias, $resultAlias); $this->_rsm->addScalarResult($columnAlias, $resultAlias);

View File

@ -164,6 +164,11 @@ class SchemaToolTask extends AbstractTask
$printer = $this->getPrinter(); $printer = $this->getPrinter();
$tool = new SchemaTool($this->_em); $tool = new SchemaTool($this->_em);
if (empty($classes)) {
$printer->writeln('No classes to process.', 'INFO');
return;
}
if ($isCreate) { if ($isCreate) {
if (isset($args['dump-sql'])) { if (isset($args['dump-sql'])) {
foreach ($tool->getCreateSchemaSql($classes) as $sql) { foreach ($tool->getCreateSchemaSql($classes) as $sql) {

View File

@ -2,41 +2,32 @@
namespace Doctrine\Tests\Common; namespace Doctrine\Tests\Common;
use Doctrine\Common\ClassLoader; use Doctrine\Common\GlobalClassLoader,
Doctrine\Common\IsolatedClassLoader;
require_once __DIR__ . '/../TestInit.php'; require_once __DIR__ . '/../TestInit.php';
class ClassLoaderTest extends \Doctrine\Tests\DoctrineTestCase class ClassLoaderTest extends \Doctrine\Tests\DoctrineTestCase
{ {
public function testCustomFileExtensionAndNamespaceSeparator() public function testGlobalClassLoaderThrowsExceptionIfPutInChain()
{ {
$classLoader = new \Doctrine\Common\ClassLoader(); $this->setExpectedException('Doctrine\Common\DoctrineException');
$classLoader->setBasePath('ClassLoaderTest', __DIR__);
$classLoader->setClassFileExtension('.class.php'); $classLoader1 = new IsolatedClassLoader('Foo');
$classLoader1->register();
$globalClassLoader = new GlobalClassLoader;
$globalClassLoader->register();
}
public function testIsolatedClassLoaderReturnsFalseOnClassExists()
{
$classLoader = new IsolatedClassLoader('ClassLoaderTest');
$classLoader->setBasePath( __DIR__);
$classLoader->setFileExtension('.class.php');
$classLoader->setNamespaceSeparator('_'); $classLoader->setNamespaceSeparator('_');
$this->assertEquals($classLoader->loadClass('ClassLoaderTest_ClassA'), true); $this->assertEquals($classLoader->loadClass('ClassLoaderTest_ClassA'), true);
$this->assertEquals($classLoader->loadClass('ClassLoaderTest_ClassB'), true);
}
public function testClassLoaderCheckFileExists()
{
$classLoader = new \Doctrine\Common\ClassLoader();
$classLoader->setBasePath('ClassLoaderTest', __DIR__);
$classLoader->setCheckFileExists(true);
// This would return a fatal error without check file exists true
$this->assertEquals($classLoader->loadClass('SomeInvalidClass'), false);
}
public function testAlreadyLoadedClassReturnsFalse()
{
$classLoader = new \Doctrine\Common\ClassLoader();
$classLoader->setBasePath('ClassLoaderTest', __DIR__);
$classLoader->setClassFileExtension('.class.php');
$classLoader->setNamespaceSeparator('_');
$classLoader->setCheckFileExists(true);
$this->assertEquals($classLoader->loadClass('ClassLoaderTest_ClassA'), false); $this->assertEquals($classLoader->loadClass('ClassLoaderTest_ClassA'), false);
$this->assertEquals($classLoader->loadClass('ClassLoaderTest_ClassC'), true); $this->assertEquals($classLoader->loadClass('ClassLoaderTest_ClassC'), true);
} }

View File

@ -408,6 +408,14 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
); );
} }
public function testOrderByCollectionAssociationSize()
{
$this->assertSqlGeneration(
"select u, size(u.articles) as numArticles from Doctrine\Tests\Models\CMS\CmsUser u order by numArticles",
"SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, (SELECT COUNT(c1_.user_id) FROM cms_articles c1_ WHERE c1_.user_id = c0_.id) AS sclr4 FROM cms_users c0_ ORDER BY sclr4 ASC"
);
}
/* Not yet implemented, needs more thought /* Not yet implemented, needs more thought
public function testSingleValuedAssociationFieldInWhere() public function testSingleValuedAssociationFieldInWhere()
{ {

View File

@ -8,11 +8,13 @@ error_reporting(E_ALL | E_STRICT);
require_once 'PHPUnit/Framework.php'; require_once 'PHPUnit/Framework.php';
require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'PHPUnit/TextUI/TestRunner.php';
require_once __DIR__ . '/../../../lib/Doctrine/Common/ClassLoader.php'; require_once __DIR__ . '/../../../lib/Doctrine/Common/IsolatedClassLoader.php';
$classLoader = new \Doctrine\Common\ClassLoader(); $classLoader = new \Doctrine\Common\IsolatedClassLoader('Doctrine');
$classLoader->register();
set_include_path( set_include_path(
get_include_path() '.'
. PATH_SEPARATOR . __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'lib' . PATH_SEPARATOR . __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'lib'
); );

View File

@ -0,0 +1,40 @@
<?php
namespace Entities;
/** @Entity @Table(name="addresses") */
class Address {
/**
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/** @Column(type="string", length=255) */
private $street;
/** @OneToOne(targetEntity="User", mappedBy="address") */
private $user;
public function getId() {
return $this->id;
}
public function getStreet() {
return $this->street;
}
public function setStreet($street) {
$this->street = $street;
}
public function getUser() {
return $this->user;
}
public function setUser(User $user) {
if ($this->user !== $user) {
$this->user = $user;
$user->setAddress($this);
}
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace Entities;
/** @Entity @Table(name="users") */
class User {
/**
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/** @Column(type="string", length=50) */
private $name;
/**
* @OneToOne(targetEntity="Address")
* @JoinColumn(name="address_id", referencedColumnName="id")
*/
private $address;
public function getId() {
return $this->id;
}
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
}
public function getAddress() {
return $this->address;
}
public function setAddress(Address $address) {
if ($this->address !== $address) {
$this->address = $address;
$address->setUser($this);
}
}
}

View File

@ -1,14 +1,33 @@
<?php <?php
#
# This configuration file is loaded by the Doctrine CLI whenever you execute
# a task. A CLI configuration file usually initializes two local variables:
#
# $em - An EntityManager instance that the CLI tasks should use.
# $args - An array of default command line arguments that take effect when an
# argument is not specifically set on the command line.
#
# You can create several CLI configuration files with different names, for different databases.
# Every CLI task recognizes the --config=<path> option where you can specify the configuration
# file to use for a particular task. If this option is not given, the CLI looks for a file
# named "cli-config.php" (this one) in the same directory and uses that by default.
#
require_once __DIR__ . '/../../lib/Doctrine/Common/IsolatedClassLoader.php';
$classLoader = new \Doctrine\Common\IsolatedClassLoader('Entities');
$classLoader->setBasePath(__DIR__);
$classLoader->register();
$config = new \Doctrine\ORM\Configuration(); $config = new \Doctrine\ORM\Configuration();
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache); $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
$connectionOptions = array( $connectionOptions = array(
'driver' => 'pdo_sqlite', 'driver' => 'pdo_sqlite',
'path' => 'database.sqlite' 'path' => 'database.sqlite'
); );
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config); $em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
$args = array( $args = array(
'classdir' => './Entities' 'classdir' => './Entities'
); );

View File

@ -1,28 +0,0 @@
<?php
require '../../lib/Doctrine/Common/ClassLoader.php';
$classLoader = new \Doctrine\Common\ClassLoader();
$classLoader->setBasePath('Doctrine', realpath(__DIR__ . '/../../lib'));
$classLoader->setBasePath('Entities', __DIR__);
$config = new \Doctrine\ORM\Configuration;
$cache = new \Doctrine\Common\Cache\ApcCache;
// Use ArrayCache is APC is not available
// Warning without APC Doctrine will not perform as well
$cache = new \Doctrine\Common\Cache\ArrayCache;
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
# EXAMPLE FOR YAML DRIVER
#$config->setMetadataDriverImpl(new \Doctrine\ORM\Mapping\Driver\YamlDriver(__DIR__ . '/yaml'));
# EXAMPLE FOR XML DRIVER
#$config->setMetadataDriverImpl(new \Doctrine\ORM\Mapping\Driver\XmlDriver(__DIR__ . '/xml'));
$eventManager = new \Doctrine\Common\EventManager();
$connectionOptions = array(
'driver' => 'pdo_sqlite',
'path' => 'database.sqlite'
);
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config, $eventManager);

View File

@ -1,9 +1,10 @@
<?php <?php
require __DIR__ . '/../../lib/Doctrine/Common/ClassLoader.php'; require __DIR__ . '/../../lib/Doctrine/Common/IsolatedClassLoader.php';
$classLoader = new \Doctrine\Common\ClassLoader(); $classLoader = new \Doctrine\Common\IsolatedClassLoader('Doctrine');
$classLoader->setBasePath('Doctrine', __DIR__ . '/../../lib'); $classLoader->setBasePath(__DIR__ . '/../../lib');
$classLoader->register();
$cli = new \Doctrine\ORM\Tools\Cli(); $cli = new \Doctrine\ORM\Tools\Cli();
$cli->run($_SERVER['argv']); $cli->run($_SERVER['argv']);

View File

@ -1,5 +1,35 @@
<?php <?php
require 'config.php'; namespace Sandbox;
// Place your code here. $em is at your service. use Entities\User, Entities\Address;
require '../../lib/Doctrine/Common/GlobalClassLoader.php';
// Set up class loading, we could alternatively use 2 IsolatedClassLoaders
$classLoader = new \Doctrine\Common\GlobalClassLoader();
$classLoader->registerNamespace('Doctrine', realpath(__DIR__ . '/../../lib'));
$classLoader->registerNamespace('Entities', __DIR__);
$classLoader->register();
// Set up caches
$config = new \Doctrine\ORM\Configuration;
$cache = new \Doctrine\Common\Cache\ApcCache;
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
// Database connection information
$connectionOptions = array(
'driver' => 'pdo_sqlite',
'path' => 'database.sqlite'
);
// Create EntityManager
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
## PUT YOUR TEST CODE BELOW
$user = new User;
$address = new Address;
echo "Hello World!";

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Entities\Address" table="addresses">
<id name="id" type="integer">
<generator strategy="AUTO"/>
</id>
<field name="street" type="string" length="255"/>
<one-to-one field="user" target-entity="User" mapped-by="address"/>
</entity>
</doctrine-mapping>

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Entities\User" table="users">
<id name="id" type="integer">
<generator strategy="AUTO"/>
</id>
<field name="name" type="string" length="50"/>
<one-to-one field="address" target-entity="Address">
<join-column name="address_id" referenced-column-name="id"/>
</one-to-one>
</entity>
</doctrine-mapping>

View File

@ -0,0 +1,16 @@
Entities\Address:
type: entity
table: addresses
id:
id:
type: integer
generator:
strategy: AUTO
fields:
street:
type: string
length: 255
oneToOne:
user:
targetEntity: User
mappedBy: address

View File

@ -0,0 +1,18 @@
Entities\User:
type: entity
table: users
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
type: string
length: 50
oneToOne:
address:
targetEntity: Address
joinColumn:
name: address_id
referencedColumnName: id