Commit 580f21c4 authored by romanb's avatar romanb

more cleanups. introducing ClassLoader.

parent 76abfb84
......@@ -31,7 +31,8 @@
* @since 1.0
* @version $Revision$
* @todo Remove all the constants, attributes to the new attribute system,
* All methods to separate classes.
* All methods to appropriate classes.
* Finally remove this class.
*/
final class Doctrine
{
......
<?php
/**
* A class loader used to load class files on demand.
*
* Usage recommendation:
* 1) Use only 1 class loader instance.
* 2) Prepend the base paths to your class libraries (including Doctrine's) to your include path.
* 3) DO NOT setCheckFileExists(true). Doing so is expensive.
*
* @since 2.0
*/
class Doctrine_Common_ClassLoader
{
private $_namespaceSeparator = '_';
private $_fileExtension = '.php';
private $_checkFileExists = false;
private $_basePath;
public function __construct()
{
}
public function setCheckFileExists($bool)
{
$this->_checkFileExists = $bool;
}
public function setClassFileExtension($extension)
{
$this->_fileExtension = $extension;
}
public function setNamespaceSeparator($separator)
{
$this->_namespaceSeparator = $separator;
}
/**
* Sets a static base path that is prepended to the path derived from the class itself.
*
* @param string $basePath
*/
public function setBasePath($basePath)
{
$this->_basePath = $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;
}
$class = '';
if ($this->_basePath) {
$class .= $this->_basePath . DIRECTORY_SEPARATOR;
}
$class .= str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $className)
. $this->_fileExtension;
if ($this->_checkFileExists) {
if (!$fh = @fopen($class, 'r', true)) {
return false;
}
@fclose($fh);
}
require $class;
return true;
}
/**
* Registers this class loader using spl_autoload_register().
*/
public function register()
{
spl_autoload_register(array($this, 'loadClass'));
}
}
?>
\ No newline at end of file
......@@ -29,7 +29,8 @@ abstract class Doctrine_DBAL_Types_Type
abstract public function getName();
/**
* Factory method.
* Factory method to create type instances.
* Type instances are implemented as flyweights.
*
* @param string $name The name of the type (as returned by getName()).
* @return Doctrine::DBAL::Types::Type
......
<?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.phpdoctrine.org>.
*/
#namespace Doctrine::DBAL::Expressions;
/**
* Doctrine_Expression
*
* @package Doctrine
* @subpackage Expression
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.org
* @since 1.0
* @version $Revision$
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @todo Merge all Expression classes into the appropriate DBAL DatabasePlatform classes.
*/
class Doctrine_Expression
{
protected $_expression;
protected $_conn;
protected $_tokenizer;
/**
* Create an expression
*
* @param string $expr The expression
* @param Doctrine_Connection $conn The connection (optional)
* @return void
*/
public function __construct($expr, $conn = null)
{
$this->_tokenizer = new Doctrine_Query_Tokenizer();
$this->setExpression($expr);
if ($conn !== null) {
$this->_conn = $conn;
}
}
/**
* getConnection
*
* @return Doctrine_Connection The connection
*/
public function getConnection()
{
if ( ! isset($this->_conn)) {
return Doctrine_Manager::connection();
}
return $this->_conn;
}
/**
* setExpression
*
* @param string $clause The expression to set
* @return void
*/
public function setExpression($clause)
{
$this->_expression = $this->parseClause($clause);
}
/**
* parseExpression
*
* @todo: What does this function do?
*
* @param string $expr The expression to parse
* @return void
*/
public function parseExpression($expr)
{
$pos = strpos($expr, '(');
if ($pos === false) {
return $expr;
}
// get the name of the function
$name = substr($expr, 0, $pos);
$argStr = substr($expr, ($pos + 1), -1);
// parse args
foreach ($this->_tokenizer->bracketExplode($argStr, ',') as $arg) {
$args[] = $this->parseClause($arg);
}
return call_user_func_array(array($this->getConnection()->expression, $name), $args);
}
/**
* parseClause
*
* @param string $clause The clause
* @return string The parse clause
*/
public function parseClause($clause)
{
$e = $this->_tokenizer->bracketExplode($clause, ' ');
foreach ($e as $k => $expr) {
$e[$k] = $this->parseExpression($expr);
}
return implode(' ', $e);
}
/**
* getSql
*
* @return string The expression
*/
public function getSql()
{
return $this->_expression;
}
/**
* __toString
*
* @return void
*/
public function __toString()
{
return $this->getSql();
}
}
This diff is collapsed.
......@@ -54,7 +54,7 @@ abstract class Doctrine_ORM_Internal_Hydration_AbstractHydrator
/**
* The current hydration mode.
*/
protected $_hydrationMode = Doctrine::HYDRATE_RECORD;
protected $_hydrationMode = Doctrine_Query::HYDRATE_OBJECT;
protected $_nullObject;
......
......@@ -89,14 +89,14 @@ class Doctrine_ORM_Internal_Hydration_StandardHydrator extends Doctrine_ORM_Inte
$stmt = $parserResult->getDatabaseStatement();
if ($hydrationMode == Doctrine::HYDRATE_NONE) {
if ($hydrationMode == Doctrine_Query::HYDRATE_NONE) {
return $stmt->fetchAll(PDO::FETCH_NUM);
}
$this->_tableAliases = $parserResult->getTableToClassAliasMap();
$this->_queryComponents = $parserResult->getQueryComponents();
if ($hydrationMode == Doctrine::HYDRATE_ARRAY) {
if ($hydrationMode == Doctrine_Query::HYDRATE_ARRAY) {
$driver = new Doctrine_ORM_Internal_Hydration_ArrayDriver();
} else {
$driver = new Doctrine_ORM_Internal_Hydration_ObjectDriver($this->_em);
......@@ -123,7 +123,7 @@ class Doctrine_ORM_Internal_Hydration_StandardHydrator extends Doctrine_ORM_Inte
$idTemplate = array();
// Holds the resulting hydrated data structure
if ($parserResult->isMixedQuery() || $hydrationMode == Doctrine::HYDRATE_SCALAR) {
if ($parserResult->isMixedQuery() || $hydrationMode == Doctrine_Query::HYDRATE_SCALAR) {
$result = array();
} else {
$result = $driver->getElementCollection($rootEntityName);
......@@ -145,7 +145,7 @@ class Doctrine_ORM_Internal_Hydration_StandardHydrator extends Doctrine_ORM_Inte
$cache = array();
// Evaluate HYDRATE_SINGLE_SCALAR
if ($hydrationMode == Doctrine::HYDRATE_SINGLE_SCALAR) {
if ($hydrationMode == Doctrine_Query::HYDRATE_SINGLE_SCALAR) {
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (count($result) > 1 || count($result[0]) > 1) {
throw Doctrine_ORM_Exceptions_HydrationException::nonUniqueResult();
......@@ -154,9 +154,9 @@ class Doctrine_ORM_Internal_Hydration_StandardHydrator extends Doctrine_ORM_Inte
}
// Process result set
while ($data = $stmt->fetch(Doctrine::FETCH_ASSOC)) {
while ($data = $stmt->fetch(PDO::FETCH_ASSOC)) {
// Evaluate HYDRATE_SCALAR
if ($hydrationMode == Doctrine::HYDRATE_SCALAR) {
if ($hydrationMode == Doctrine_Query::HYDRATE_SCALAR) {
$result[] = $this->_gatherScalarRowData($data, $cache);
continue;
}
......
......@@ -20,6 +20,8 @@
* <http://www.phpdoctrine.org>.
*/
#namespace Doctrine::ORM;
/**
* A Doctrine_Query object represents a DQL query. It is used to query databases for
* data in an object-oriented fashion. A DQL query understands relations and inheritance
......@@ -36,6 +38,28 @@
*/
class Doctrine_Query extends Doctrine_Query_Abstract
{
/* Hydration mode constants */
/**
* Hydrates an object graph. This is the default behavior.
*/
const HYDRATE_OBJECT = 1;
/**
* Hydrates an array graph.
*/
const HYDRATE_ARRAY = 2;
/**
* Hydrates a flat, rectangular result set with scalar values.
*/
const HYDRATE_SCALAR = 3;
/**
* Hydrates a single scalar value.
*/
const HYDRATE_SINGLE_SCALAR = 4;
/**
* Hydrates nothing.
*/
const HYDRATE_NONE = 5;
/**
* @var Doctrine_EntityManager The entity manager used by this query object.
*/
......@@ -54,7 +78,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract
/**
* @var string $_sql Cached SQL query.
*/
protected $_sql = null;
protected $_sql;
// Caching Stuff
......
......@@ -151,7 +151,8 @@ abstract class Doctrine_Query_ParserRule
$class = 'Doctrine_Query_Parser_' . $name;
//echo $class . "\r\n";
//TODO: This expensive check is not necessary. Should be removed at the end.
// "new $class" will throw an error anyway if the class is not found.
if ( ! class_exists($class)) {
throw new Doctrine_Query_Parser_Exception(
"Unknown Grammar Rule '$name'. Could not find related compiler class."
......
This diff is collapsed.
......@@ -2,10 +2,25 @@
require_once 'PHPUnit/Framework.php';
require_once 'PHPUnit/TextUI/TestRunner.php';
require_once '../lib/Doctrine.php';
spl_autoload_register(array('Doctrine', 'autoload'));
//require_once '../lib/Doctrine.php';
require_once '../lib/Doctrine/Common/ClassLoader.php';
// Some of these classes depends on Doctrine_* classes
$classLoader = new Doctrine_Common_ClassLoader();
// checking for existance should not be necessary, remove as soon as possible
$classLoader->setCheckFileExists(true);
$classLoader->register();
$modelDir = dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'models';
set_include_path(
get_include_path()
. PATH_SEPARATOR . dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'lib'
. PATH_SEPARATOR . $modelDir . DIRECTORY_SEPARATOR . 'cms'
. PATH_SEPARATOR . $modelDir . DIRECTORY_SEPARATOR . 'company'
. PATH_SEPARATOR . $modelDir . DIRECTORY_SEPARATOR . 'ecommerce'
. PATH_SEPARATOR . $modelDir . DIRECTORY_SEPARATOR . 'forum'
);
// Some of these classes depend on Doctrine_* classes
require_once 'Doctrine_TestCase.php';
require_once 'Doctrine_TestUtil.php';
require_once 'Doctrine_DbalTestCase.php';
......@@ -15,22 +30,3 @@ require_once 'Doctrine_TestSuite.php';
require_once 'Doctrine_OrmTestSuite.php';
require_once 'Doctrine_OrmFunctionalTestSuite.php';
require_once 'Doctrine_DbalTestSuite.php';
$modelDir = dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'models';
Doctrine_Manager::getInstance()->setAttribute(Doctrine::ATTR_MODEL_LOADING, Doctrine::MODEL_LOADING_CONSERVATIVE);
Doctrine::loadModels($modelDir);
/*
//spl_autoload_register(array('Doctrine_TestUtil', 'autoload'));
$modelDir = dirname(__FILE__)
. DIRECTORY_SEPARATOR . '..'
. DIRECTORY_SEPARATOR . 'models'
. DIRECTORY_SEPARATOR;
set_include_path(
get_include_path()
. PATH_SEPARATOR . $modelDir . 'cms'
. PATH_SEPARATOR . $modelDir . 'ecommerce'
. PATH_SEPARATOR . $modelDir . 'forum');
*/
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment