Commit 34af8e3a authored by Jonathan.Wage's avatar Jonathan.Wage

Moved cli to sandbox folder. Fixes to importing schema and generating sql.

parent ea368a36
......@@ -558,6 +558,7 @@ final class Doctrine
public static function generateModelsFromYaml($yamlPath, $directory)
{
$import = new Doctrine_Import_Schema();
$import->generateBaseClasses(true);
return $import->importSchema($yamlPath, 'yml', $directory);
}
......@@ -754,8 +755,8 @@ final class Doctrine
$path = $directory . DIRECTORY_SEPARATOR . $fileName;
$code = '<?php' . PHP_EOL;
$code .= "// Automatically generated by Doctrine\n";
$code .= "class " . $className . " extends Doctrine_Migration\n";
$code .= "// Automatically generated by the Doctrine ORM Framework\n";
$code .= "class " . Doctrine::classify($className) . " extends Doctrine_Migration\n";
$code .= "{\n";
$code .= "\tpublic function up()\n\t{ }\n\n";
$code .= "\tpublic function down()\n\t{ }\n";
......
......@@ -55,9 +55,13 @@ abstract class Doctrine_Cli_Task
return true;
}
public function getArgument($name)
public function getArgument($name, $default = null)
{
if (isset($this->arguments[$name])) {
return $this->arguments[$name];
} else if ($default) {
return $default;
}
}
public function getArguments()
......
......@@ -41,6 +41,14 @@ class Doctrine_Cli_Task_GenerateSql extends Doctrine_Cli_Task
{
$sql = Doctrine::generateSqlFromModels($this->getArgument('models_path'));
file_put_contents($this->getArgument('sql_path'), implode("\n", $sql));
if (is_dir($this->getArgument('sql_path'))) {
$path = $this->getArgument('sql_path') . DIRECTORY_SEPARATOR . 'schema.sql';
} else if (is_file($this->getArgument('sql_path'))) {
$path = $this->getArgument('sql_path');
} else {
throw new Doctrine_Cli_Exception('Invalid sql path.');
}
file_put_contents($path, implode("\n", $sql));
}
}
\ No newline at end of file
<?php
/*
* $Id: Config.php 2753 2007-10-07 20:58:08Z Jonathan.Wage $
*
* 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_Config
*
* Class used to simplify the setup and configuration of a doctrine implementation.
*
* @package Doctrine
* @subpackage Config
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision: 2753 $
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @author Jonathan H. Wage <jwage@mac.com>
*/
class Doctrine_Config
{
protected $connections = array();
protected $cliConfig = array();
/**
* addConnection
*
* @param string $adapter
* @param string $name
* @return void
*/
public function addConnection($adapter, $name = null)
{
$connections[] = Doctrine_Manager::getInstance()->openConnection($adapter, $name);
}
/**
* bindComponent
*
* @param string $modelName
* @param string $connectionName
* @return void
*/
public function bindComponent($modelName, $connectionName)
{
return Doctrine_Manager::getInstance()->bindComponent($modelName, $connectionName);
}
/**
* setAttribute
*
* @param string $key
* @param string $value
* @return void
*/
public function setAttribute($key, $value)
{
foreach ($this->connections as $connection) {
$connection->setAttribute($key, $value);
}
}
/**
* addCliConfig
*
* @param string $key
* @param string $value
* @return void
*/
public function addCliConfig($key, $value)
{
$this->cliConfig[$key] = $value;
}
/**
* getCliConfig
*
* @return void
*/
public function getCliConfig()
{
return $this->cliConfig;
}
}
\ No newline at end of file
......@@ -189,6 +189,7 @@ class Doctrine_Import extends Doctrine_Connection_Module
foreach ($connections as $connection) {
$builder = new Doctrine_Import_Builder();
$builder->generateBaseClasses(true);
$builder->setTargetPath($directory);
$classes = array();
......
......@@ -43,6 +43,12 @@ class Doctrine_Import_Schema
public $indexes = array();
public $generateBaseClasses = false;
/**
* generateBaseClasses
*
* @param string $bool
* @return void
*/
public function generateBaseClasses($bool = null)
{
if ($bool !== null) {
......@@ -51,17 +57,39 @@ class Doctrine_Import_Schema
return $this->generateBaseClasses;
}
/**
* buildSchema
*
* @param string $schema
* @param string $format
* @return void
*/
public function buildSchema($schema, $format)
{
$array = array();
foreach ((array) $schema AS $s) {
if (is_file($s)) {
$array = array_merge($array, $this->parseSchema($s, $format));
} else if (is_dir($s)) {
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($s),
RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($it as $file) {
$e = explode('.', $file->getFileName());
if (end($e) === $format) {
$array = array_merge($array, $this->parseSchema($file->getPathName(), $format));
}
}
}
}
$this->buildRelationships($array);
return array('schema' => $array, 'relations' => $this->relations, 'indexes' => $this->indexes);
}
/**
* importSchema
*
......@@ -96,6 +124,13 @@ class Doctrine_Import_Schema
}
}
/**
* getOptions
*
* @param string $properties
* @param string $directory
* @return void
*/
public function getOptions($properties, $directory)
{
$options = array();
......@@ -110,11 +145,23 @@ class Doctrine_Import_Schema
return $options;
}
/**
* getColumns
*
* @param string $properties
* @return void
*/
public function getColumns($properties)
{
return isset($properties['columns']) ? $properties['columns']:array();
}
/**
* getRelations
*
* @param string $properties
* @return void
*/
public function getRelations($properties)
{
return isset($this->relations[$properties['className']]) ? $this->relations[$properties['className']]:array();
......@@ -181,6 +228,12 @@ class Doctrine_Import_Schema
return $build;
}
/**
* buildRelationships
*
* @param string $array
* @return void
*/
public function buildRelationships(&$array)
{
foreach ($array as $name => $properties) {
......@@ -225,6 +278,11 @@ class Doctrine_Import_Schema
$this->fixRelationships();
}
/**
* fixRelationships
*
* @return void
*/
public function fixRelationships()
{
// define both sides of the relationship
......
<?php
require_once('config.php');
$cli = new Doctrine_Cli($config->getCliConfig());
$cli->run($_SERVER['argv']);
\ No newline at end of file
<?php
require_once('config.php');
// Configure Doctrine Cli
// Normally these are arguments to the cli tasks but if they are set here the arguments will be auto-filled
$config = array('data_fixtures_path' => DATA_FIXTURES_PATH,
'models_path' => MODELS_PATH,
'migrations_path' => MIGRATIONS_PATH,
'sql_path' => SQL_PATH,
'yaml_schema_path' => YAML_SCHEMA_PATH);
$cli = new Doctrine_Cli($config);
$cli->run($_SERVER['argv']);
\ No newline at end of file
#!/usr/bin/env php
<?php
chdir(dirname(__FILE__));
?>
#!/usr/bin/php
Welcome to the interactive Doctrine Compiler 0.0.1 (Beta).
WARNING: You're on your own - this script modifies your
......@@ -28,6 +26,21 @@ While the program is running:
exit;
}
if ($argc > 1) {
$doctrinePath = $argv[1];
}
$drivers = array(
'Db2',
'Firebird',
'Informix',
'Mssql',
'Mysql',
'Oracle',
'Pgsql',
'Sqlite'
);
function addIncludePath($path) {
set_include_path(
$path . PATH_SEPARATOR . get_include_path()
......@@ -97,30 +110,20 @@ function ask(
}
}
// Enable error reporting
if (($answer = ask("Would you like to turn on error reporting?", 'n')) == 'y') {
error_reporting(E_ALL);
$showErrors = true;
} else {
error_reporting(0);
$showErrors = false;
}
autoQuit($answer);
// Process library path command line argument
if ($argc > 1) {
$doctrinePath = $argv[1];
} else {
$doctrinePath = dirname(__FILE__);
}
// Get Doctrine's library path
$usablePath = false;
while (!$usablePath) {
if (!isset($doctrinePath)) {
$doctrinePath = dirname(__FILE__);
}
$path = ask("Please enter the path to Doctrine's lib directory:", $doctrinePath, array($doctrinePath), false);
autoQuit($path);
try {
......@@ -136,58 +139,10 @@ while (!$usablePath) {
}
}
// Process target filename command line argument
if ($argc > 2) {
$targetFile = $argv[2];
} else {
$targetFile = $path.DIRECTORY_SEPARATOR.'Doctrine.compiled.php';
}
clearstatcache();
$usableFilename = false;
while (!$usableFilename) {
$target = ask("Please enter the target filename for the 'compiled' Doctrine file that will be created:", $targetFile, array($targetFile), false);
autoQuit($target);
if (file_exists($target)) {
if (is_writable($target) && (!is_dir($target))) {
$usableFilename = true;
} else {
$msg = "The target filename '$target' cannot be used (it is not writable, or it is a directory).";
}
} else {
if (is_writable(dirname($target))) {
$usableFilename = true;
} else {
$msg = "The directory in which the target file will be created is not writable.";
}
}
if (!$usableFilename) {
showMessage($msg);
if (($answer = ask("Would you like to specify a different target filename?")) != 'y') {
quit();
}
}
}
// Define the default drivers - unfortunately, this is hard-coded for now
$drivers = array(
'Db2',
'Firebird',
'Informix',
'Mssql',
'Mysql',
'Oracle',
'Pgsql',
'Sqlite'
);
$includeDrivers = array();
$excludeDrivers = array();
// Determine driver support
foreach ($drivers as $driver) {
if (($answer = ask("Would you like to enable support for $driver?")) == 'y') {
$includeDrivers[] = $driver;
......@@ -198,22 +153,19 @@ foreach ($drivers as $driver) {
}
// Verify driver support
if (!count($includeDrivers)) {
showMessage("You have not selected any drivers. Usually this is not a good idea, unless you know what you're doing.");
showMessage("You have not selected any drivers. Normally this is not a good idea, unless you know what you're doing.");
if (($answer = ask("Are you sure you want to compile without any drivers?")) != 'y') {
quit();
}
autoQuit($answer);
}
// Try to prevent errors related to memory allocation
$requiredMemory = '20M';
showMessage("Trying to set the PHP memory limit to $requiredMemory...");
ini_set('memory_limit', $requiredMemory);
if (ini_get('memory_limit') != $requiredMemory) {
showMessage("WARNING: The program could not set the PHP memory limit to $requiredMemory. The compilation might fail.");
// 'Compile' Doctrine...
showMessage("Trying to set the PHP memory limit to 18MB...");
ini_set('memory_limit', '18M');
if (ini_get('memory_limit') != '18M') {
showMessage("WARNING: The program could not set the PHP memory limit to 18MB. The compilation might fail.");
if (($answer = ask("Do you still want to continue?")) != 'y') {
quit;
}
......@@ -221,28 +173,25 @@ if (ini_get('memory_limit') != $requiredMemory) {
showMessage("PHP memory limit adjusted successfully.");
}
// Build / 'Compile' Doctrine...
$target = './Doctrine.compiled.php';
try {
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::LEAVES_ONLY);
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($it as $file) {
foreach ($it as $file) {
$e = explode('.', $file->getFileName());
// We don't want to require versioning files, or cli files
if (end($e) === 'php' && strpos($file->getFileName(), '.inc') === false && strpos($file->getFileName(), 'cli') !== 0) {
// we don't want to require versioning files
if (end($e) === 'php' && strpos($file->getFileName(), '.inc') === false) {
require_once $file->getPathName();
}
}
}
$classes = array_merge(get_declared_classes(), get_declared_interfaces());
$classes = array_merge(get_declared_classes(), get_declared_interfaces());
$ret = array();
$ret = array();
foreach ($classes as $class) {
foreach ($classes as $class) {
$e = explode('_', $class);
if ($e[0] !== 'Doctrine') {
......@@ -250,7 +199,6 @@ try {
}
// Skip excluded drivers
$skipClass = false;
foreach ($excludeDrivers as $excludeDriver) {
if (in_array($excludeDriver, $e)) {
......@@ -274,43 +222,31 @@ try {
$end = $refl->getEndLine();
$ret = array_merge($ret, array_slice($lines, $start, ($end - $start)));
}
}
// first write the 'compiled' data to a text file, so
// that we can use php_strip_whitespace (which only works on files)
if ($target == null) {
$target = $path . DIRECTORY_SEPARATOR . 'Doctrine.compiled.php';
}
$fp = @fopen($target, 'w');
// first write the 'compiled' data to a text file, so
// that we can use php_strip_whitespace (which only works on files)
$fp = @fopen($target, 'w');
if ($fp === false) {
if ($fp === false) {
throw new Exception("Couldn't write compiled data. Failed to open $target");
}
fwrite($fp, "<?php ". implode('', $ret));
fclose($fp);
}
fwrite($fp, "<?php ". implode('', $ret));
fclose($fp);
$stripped = php_strip_whitespace($target);
$fp = @fopen($target, 'w');
if ($fp === false) {
$stripped = php_strip_whitespace($target);
$fp = @fopen($target, 'w');
if ($fp === false) {
throw new Exception("Couldn't write compiled data. Failed to open $file");
}
fwrite($fp, $stripped);
fclose($fp);
} catch (Exception $e) {
if (!$showErrors) {
if (($answer = ask("Sorry, an error occurred during the build. Would you like to see the error?")) == 'y') {
showMessage("\n$e");
} else {
quit();
}
}
showMessage("\nBuild Aborted.");
exit;
}
fwrite($fp, $stripped);
fclose($fp);
// Say bye...
showMessage("\nCompilation Finished.");
showMessage("Thank you for using the interactive Doctrine Compiler. Have fun following the Doctrine :)\n");
......
<?php
/*
* $Id: Config.php 2753 2007-10-07 20:58:08Z Jonathan.Wage $
* $Id: config.php 2753 2007-10-07 20:58:08Z Jonathan.Wage $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
......@@ -23,8 +23,6 @@
* Doctrine Configuration File
*
* This is a sample implementation of Doctrine
* You can use the Doctrine_Config interface below to setup the basics.
* Or if you prefer you can setup your Doctrine configuration manually.
*
* @package Doctrine
* @subpackage Config
......@@ -36,23 +34,17 @@
* @author Jonathan H. Wage <jwage@mac.com>
*/
// Load Doctrine
require_once('Doctrine.php');
spl_autoload_register(array('Doctrine', 'autoload'));
// New Doctrine Config
$config = new Doctrine_Config();
define('SANDBOX_PATH', dirname(__FILE__));
define('DOCTRINE_PATH', dirname(SANDBOX_PATH) . DIRECTORY_SEPARATOR . 'lib');
define('DATA_FIXTURES_PATH', SANDBOX_PATH . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'fixtures');
define('MODELS_PATH', SANDBOX_PATH . DIRECTORY_SEPARATOR . 'models');
define('MIGRATIONS_PATH', SANDBOX_PATH . DIRECTORY_SEPARATOR . 'models');
define('SQL_PATH', SANDBOX_PATH . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'sql');
define('YAML_SCHEMA_PATH', SANDBOX_PATH . DIRECTORY_SEPARATOR . 'schema');
define('DSN', 'sqlite:/' . SANDBOX_PATH . DIRECTORY_SEPARATOR . 'sandbox.db');
// Setup Connections
//$config->addConnection('mysql://user:pass@localhost/db_name', 'connection_1');
//$config->addConnection(new PDO('dsn', 'username', 'password'), 'connection_2);
require_once(DOCTRINE_PATH . DIRECTORY_SEPARATOR . 'Doctrine.php');
// Bind components/models to connections
//$config->bindComponent('User', 'connection_1');
spl_autoload_register(array('Doctrine', 'autoload'));
// Configure Doctrine Cli
// Normally these are arguments to the cli tasks but if they are set here the arguments will be auto-filled
//$config->addCliConfig('data_fixtures_path', '/path/to/your/data_fixtures');
//$config->addCliConfig('models_path', '/path/to/your/models');
//$config->addCliConfig('migrations_path', '/peth/to/your/migration/classes');
//$config->addCliConfig('sql_path', '/path/to/your/exported/sql');
\ No newline at end of file
Doctrine_Manager::connection(DSN, 'sandbox');
\ 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