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
|
|
|
|
*
|
|
|
|
* @package Doctrine
|
2007-10-04 01:43:22 +04:00
|
|
|
* @subpackage Resource
|
|
|
|
* @author Jonathan H. Wage <jwage@mac.com>
|
2007-09-23 02:02:58 +04:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @version $Revision$
|
|
|
|
* @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-25 18:09:05 +04:00
|
|
|
static public function getInstance($url = null, $config = null)
|
2007-09-21 06:48:13 +04:00
|
|
|
{
|
2007-09-24 08:58:57 +04:00
|
|
|
static $instance;
|
|
|
|
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! $instance) {
|
2007-09-25 18:09:05 +04:00
|
|
|
$instance = new Doctrine_Resource_Client($url, $config);
|
2007-09-24 08:58:57 +04:00
|
|
|
|
|
|
|
if ($instance->loadDoctrine === true) {
|
|
|
|
$instance->loadDoctrine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
2007-09-25 18:09:05 +04:00
|
|
|
public function __construct($url, $config)
|
|
|
|
{
|
|
|
|
if ($url) {
|
|
|
|
$config['url'] = $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::__construct($config);
|
|
|
|
}
|
|
|
|
|
2007-09-24 08:58:57 +04:00
|
|
|
public function loadDoctrine()
|
|
|
|
{
|
2007-09-28 02:02:13 +04:00
|
|
|
$path = '/tmp/' . $this->getClientKey();
|
2007-09-25 01:32:02 +04:00
|
|
|
$classesPath = $path.'.classes.php';
|
2007-09-24 08:58:57 +04:00
|
|
|
|
2007-09-26 01:39:38 +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();
|
2007-09-27 01:12:52 +04:00
|
|
|
$request->set('action', 'load');
|
2007-09-24 08:58:57 +04:00
|
|
|
|
|
|
|
$schema = $request->execute();
|
|
|
|
|
2007-09-24 23:29:56 +04:00
|
|
|
if ($schema) {
|
2007-09-28 02:02:13 +04:00
|
|
|
file_put_contents($path, Doctrine_Parser::dump($schema, Doctrine_Resource::FORMAT));
|
2007-09-24 23:29:56 +04:00
|
|
|
}
|
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();
|
2007-09-28 02:02:13 +04:00
|
|
|
$schema = $import->buildSchema($path, Doctrine_Resource::FORMAT);
|
2007-09-24 23:29:56 +04:00
|
|
|
|
2007-10-21 10:23:59 +04:00
|
|
|
if ( ! file_exists($classesPath)) {
|
2007-09-25 01:32:02 +04:00
|
|
|
$build = "<?php\n";
|
|
|
|
foreach ($schema['schema'] as $className => $details) {
|
2007-09-27 01:12:52 +04:00
|
|
|
$build .= "class " . $className . " extends Doctrine_Resource_Record { protected \$_model = '".$className."'; public function __construct() { parent::__construct(\$this->_model); } }\n";
|
2007-09-26 01:39:38 +04:00
|
|
|
|
|
|
|
$schema['schema'][$className]['relations'] = isset($schema['relations'][$className]) ? $schema['relations'][$className]:array();
|
2007-09-25 01:32:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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-28 02:02:13 +04:00
|
|
|
public function getClientKey()
|
|
|
|
{
|
|
|
|
return md5(Doctrine_Resource::FORMAT.serialize($this->getConfig()));
|
|
|
|
}
|
|
|
|
|
2007-09-25 01:50:50 +04:00
|
|
|
public function getTable($table)
|
2007-09-25 01:32:02 +04:00
|
|
|
{
|
2007-09-26 01:39:38 +04:00
|
|
|
static $instance;
|
|
|
|
|
2007-10-21 10:23:59 +04:00
|
|
|
if( ! isset($instance[$table])) {
|
2007-09-26 01:39:38 +04:00
|
|
|
$instance[$table] = new Doctrine_Resource_Table($table);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $instance[$table];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function printSchema()
|
|
|
|
{
|
|
|
|
$schema = $this->getConfig('schema');
|
|
|
|
|
|
|
|
echo '<h2>Schema</h2>';
|
|
|
|
|
|
|
|
echo '<ul>';
|
|
|
|
|
|
|
|
foreach ($schema['schema'] as $className => $info) {
|
|
|
|
echo '<a name="'.$className.'"></a>';
|
|
|
|
echo '<li><h3>'.$className.'</h3></li>';
|
|
|
|
|
|
|
|
echo '<ul>';
|
|
|
|
echo '<li>Columns';
|
|
|
|
echo '<ul>';
|
|
|
|
foreach ($info['columns'] as $columnName => $column)
|
|
|
|
{
|
|
|
|
echo '<li>' . $columnName;
|
|
|
|
|
|
|
|
echo '<ul>';
|
|
|
|
foreach ($column as $key => $value) {
|
|
|
|
if ($value) {
|
|
|
|
echo '<li>'.$key.': '.$value.'</li>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
echo '</ul>';
|
|
|
|
|
|
|
|
echo '</li>';
|
|
|
|
}
|
|
|
|
echo '</ul>';
|
|
|
|
echo '</li>';
|
|
|
|
echo '</ul>';
|
|
|
|
|
|
|
|
if (isset($info['relations']) && !empty($info['relations'])) {
|
|
|
|
echo '<ul>';
|
|
|
|
echo '<li>Relations';
|
|
|
|
echo '<ul>';
|
|
|
|
foreach ($info['relations'] as $relationName => $relation)
|
|
|
|
{
|
|
|
|
echo '<li><a href="#'.$relation['class'].'">' . $relationName . '</a></li>';
|
|
|
|
}
|
|
|
|
echo '</ul>';
|
|
|
|
echo '</li>';
|
|
|
|
echo '</ul>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo '</ul>';
|
|
|
|
|
|
|
|
exit;
|
2007-09-25 01:32:02 +04:00
|
|
|
}
|
2007-09-24 08:58:57 +04:00
|
|
|
}
|