1
0
mirror of synced 2025-01-18 22:41:43 +03:00
2007-09-25 14:09:05 +00:00

104 lines
3.5 KiB
PHP

<?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.com>.
*/
/**
* Doctrine_Resource_Client
*
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @author Jonathan H. Wage <jwage@mac.com>
* @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
*/
class Doctrine_Resource_Client extends Doctrine_Resource
{
public $loadDoctrine = false;
static public function getInstance($url = null, $config = null)
{
static $instance;
if (!$instance) {
$instance = new Doctrine_Resource_Client($url, $config);
if ($instance->loadDoctrine === true) {
$instance->loadDoctrine();
}
}
return $instance;
}
public function __construct($url, $config)
{
if ($url) {
$config['url'] = $url;
}
parent::__construct($config);
}
public function loadDoctrine()
{
$path = '/tmp/' . md5(serialize($this->getConfig()));
$classesPath = $path.'.classes.php';
if (!file_exists($path)) {
$schema = file_get_contents($path);
} else {
$request = new Doctrine_Resource_Request();
$request->set('type', 'load');
$request->set('format', $this->getConfig()->get('format'));
$schema = $request->execute();
if ($schema) {
file_put_contents($path, $schema);
}
}
if (file_exists($path) && $schema) {
$import = new Doctrine_Import_Schema();
$schema = $import->buildSchema($path, $this->getConfig()->get('format'));
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);
$this->getConfig()->set('schema', $schema);
}
}
public function getTable($table)
{
return new Doctrine_Resource_Table($table);
}
}