2007-09-21 06:48:13 +04:00
|
|
|
<?php
|
2007-09-23 02:02:58 +04:00
|
|
|
/*
|
|
|
|
* $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.com>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Doctrine_Resource_Client
|
|
|
|
*
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
2007-09-24 08:58:57 +04:00
|
|
|
* @author Jonathan H. Wage <jwage@mac.com>
|
2007-09-23 02:02:58 +04:00
|
|
|
* @package Doctrine
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @version $Revision$
|
|
|
|
* @category Object Relational Mapping
|
|
|
|
* @link www.phpdoctrine.com
|
|
|
|
* @since 1.0
|
|
|
|
*/
|
2007-09-21 06:48:13 +04:00
|
|
|
class Doctrine_Resource_Client extends Doctrine_Resource
|
|
|
|
{
|
2007-09-24 08:58:57 +04:00
|
|
|
public $loadDoctrine = false;
|
2007-09-21 06:48:13 +04:00
|
|
|
|
2007-09-24 08:58:57 +04:00
|
|
|
static public function getInstance($config = null)
|
2007-09-21 06:48:13 +04:00
|
|
|
{
|
2007-09-24 08:58:57 +04:00
|
|
|
static $instance;
|
|
|
|
|
|
|
|
if (!$instance) {
|
|
|
|
$instance = new Doctrine_Resource_Client($config);
|
|
|
|
|
|
|
|
if ($instance->loadDoctrine === true) {
|
|
|
|
$instance->loadDoctrine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function loadDoctrine()
|
|
|
|
{
|
|
|
|
$path = '/tmp/' . md5(serialize($this->getConfig()));
|
2007-09-25 01:32:02 +04:00
|
|
|
$classesPath = $path.'.classes.php';
|
2007-09-24 08:58:57 +04:00
|
|
|
|
2007-09-24 22:46:11 +04:00
|
|
|
if (!file_exists($path)) {
|
2007-09-24 08:58:57 +04:00
|
|
|
$schema = file_get_contents($path);
|
|
|
|
} else {
|
|
|
|
$request = new Doctrine_Resource_Request();
|
|
|
|
$request->set('type', 'load');
|
2007-09-24 22:22:52 +04:00
|
|
|
$request->set('format', $this->getConfig()->get('format'));
|
2007-09-24 08:58:57 +04:00
|
|
|
|
|
|
|
$schema = $request->execute();
|
|
|
|
|
2007-09-24 23:29:56 +04:00
|
|
|
if ($schema) {
|
|
|
|
file_put_contents($path, $schema);
|
|
|
|
}
|
2007-09-24 08:58:57 +04:00
|
|
|
}
|
|
|
|
|
2007-09-24 23:29:56 +04:00
|
|
|
if (file_exists($path) && $schema) {
|
|
|
|
$import = new Doctrine_Import_Schema();
|
|
|
|
$schema = $import->buildSchema($path, $this->getConfig()->get('format'));
|
|
|
|
|
2007-09-25 01:32:02 +04:00
|
|
|
if (file_exists($classesPath)) {
|
|
|
|
$build = "<?php\n";
|
|
|
|
foreach ($schema['schema'] as $className => $details) {
|
|
|
|
$build .= "class " . $className . " extends Doctrine_Resource_Record { protected \$_model = '".$className."'; public function __construct(\$loadRelations = true) { parent::__construct(\$this->_model, \$loadRelations); } }\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
file_put_contents($classesPath, $build);
|
|
|
|
}
|
|
|
|
|
|
|
|
require_once($classesPath);
|
|
|
|
|
2007-09-24 23:29:56 +04:00
|
|
|
$this->getConfig()->set('schema', $schema);
|
|
|
|
}
|
2007-09-21 06:48:13 +04:00
|
|
|
}
|
|
|
|
|
2007-09-25 01:32:02 +04:00
|
|
|
public function find($model, $pk)
|
|
|
|
{
|
|
|
|
$record = $this->newRecord($model);
|
|
|
|
|
|
|
|
$pk = is_array($pk) ? $pk:array($pk);
|
|
|
|
$identifier = $record->identifier();
|
|
|
|
$identifier = is_array($identifier) ? $identifier:array($identifier);
|
|
|
|
|
|
|
|
$where = '';
|
|
|
|
foreach (array_keys($identifier) as $key => $name) {
|
|
|
|
$value = $pk[$key];
|
|
|
|
$where .= $model.'.' . $name . ' = '.$value;
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = new Doctrine_Resource_Query();
|
|
|
|
$query->from($model)->where($where)->limit(1);
|
|
|
|
|
|
|
|
return $query->execute()->getFirst();
|
|
|
|
}
|
|
|
|
|
2007-09-21 22:01:08 +04:00
|
|
|
public function newQuery()
|
2007-09-21 06:48:13 +04:00
|
|
|
{
|
2007-09-24 08:58:57 +04:00
|
|
|
return new Doctrine_Resource_Query();
|
2007-09-21 06:48:13 +04:00
|
|
|
}
|
2007-09-21 22:19:19 +04:00
|
|
|
|
2007-09-24 08:58:57 +04:00
|
|
|
public function newRecord($model, $loadRelations = true)
|
2007-09-21 22:19:19 +04:00
|
|
|
{
|
2007-09-24 08:58:57 +04:00
|
|
|
return new Doctrine_Resource_Record($model, $loadRelations);
|
2007-09-21 22:19:19 +04:00
|
|
|
}
|
2007-09-22 05:32:48 +04:00
|
|
|
|
|
|
|
public function newCollection($model)
|
|
|
|
{
|
2007-09-24 08:58:57 +04:00
|
|
|
return new Doctrine_Resource_Collection($model);
|
2007-09-22 05:32:48 +04:00
|
|
|
}
|
2007-09-24 08:58:57 +04:00
|
|
|
}
|