From 34af8e3aa116db5b1ea81dccb9d3ebd19a721415 Mon Sep 17 00:00:00 2001 From: "Jonathan.Wage" Date: Mon, 15 Oct 2007 13:47:24 +0000 Subject: [PATCH] Moved cli to sandbox folder. Fixes to importing schema and generating sql. --- lib/Doctrine.php | 5 +- lib/Doctrine/Cli/Task.php | 8 +- lib/Doctrine/Cli/Task/GenerateSql.php | 10 +- lib/Doctrine/Config.php | 100 ----------- lib/Doctrine/Import.php | 1 + lib/Doctrine/Import/Schema.php | 62 ++++++- lib/cli.php | 5 - {lib => sandbox}/cli | 0 sandbox/cli.php | 13 ++ {lib => sandbox}/cli_compiler.php | 248 ++++++++++---------------- {lib => sandbox}/config.php.dist | 34 ++-- 11 files changed, 197 insertions(+), 289 deletions(-) delete mode 100644 lib/Doctrine/Config.php delete mode 100644 lib/cli.php rename {lib => sandbox}/cli (100%) create mode 100644 sandbox/cli.php rename {lib => sandbox}/cli_compiler.php (52%) rename {lib => sandbox}/config.php.dist (58%) diff --git a/lib/Doctrine.php b/lib/Doctrine.php index 01e337eb3..7118a06b1 100644 --- a/lib/Doctrine.php +++ b/lib/Doctrine.php @@ -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 = 'arguments[$name]; + if (isset($this->arguments[$name])) { + return $this->arguments[$name]; + } else if ($default) { + return $default; + } } public function getArguments() diff --git a/lib/Doctrine/Cli/Task/GenerateSql.php b/lib/Doctrine/Cli/Task/GenerateSql.php index 6688131cb..30a97ca5b 100644 --- a/lib/Doctrine/Cli/Task/GenerateSql.php +++ b/lib/Doctrine/Cli/Task/GenerateSql.php @@ -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 diff --git a/lib/Doctrine/Config.php b/lib/Doctrine/Config.php deleted file mode 100644 index 30096ca0a..000000000 --- a/lib/Doctrine/Config.php +++ /dev/null @@ -1,100 +0,0 @@ -. - */ - -/** - * 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 - * @author Jonathan H. Wage - */ -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 diff --git a/lib/Doctrine/Import.php b/lib/Doctrine/Import.php index cedd08338..7e0e3c91e 100644 --- a/lib/Doctrine/Import.php +++ b/lib/Doctrine/Import.php @@ -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(); diff --git a/lib/Doctrine/Import/Schema.php b/lib/Doctrine/Import/Schema.php index bbf786e83..e238b87dd 100644 --- a/lib/Doctrine/Import/Schema.php +++ b/lib/Doctrine/Import/Schema.php @@ -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) { - $array = array_merge($array, $this->parseSchema($s, $format)); + 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 diff --git a/lib/cli.php b/lib/cli.php deleted file mode 100644 index 0c04c08d4..000000000 --- a/lib/cli.php +++ /dev/null @@ -1,5 +0,0 @@ -getCliConfig()); -$cli->run($_SERVER['argv']); \ No newline at end of file diff --git a/lib/cli b/sandbox/cli similarity index 100% rename from lib/cli rename to sandbox/cli diff --git a/sandbox/cli.php b/sandbox/cli.php new file mode 100644 index 000000000..e0651351e --- /dev/null +++ b/sandbox/cli.php @@ -0,0 +1,13 @@ + 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 diff --git a/lib/cli_compiler.php b/sandbox/cli_compiler.php similarity index 52% rename from lib/cli_compiler.php rename to sandbox/cli_compiler.php index 1fefb1dfb..584756c67 100755 --- a/lib/cli_compiler.php +++ b/sandbox/cli_compiler.php @@ -1,7 +1,5 @@ -#!/usr/bin/env php - +#!/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,96 +173,80 @@ 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) { + $e = explode('.', $file->getFileName()); - 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) { - require_once $file->getPathName(); - } + // 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()); - - $ret = array(); - - foreach ($classes as $class) { - $e = explode('_', $class); - - if ($e[0] !== 'Doctrine') { - continue; - } - - // Skip excluded drivers - - $skipClass = false; - foreach ($excludeDrivers as $excludeDriver) { - if (in_array($excludeDriver, $e)) { - $skipClass = true; - break; - } - } - if ($skipClass) { - echo "\nExcluding -> $class"; - continue; - } - - $refl = new ReflectionClass($class); - $file = $refl->getFileName(); - - echo "\nIncluding -> $file"; - - $lines = file($file); - - $start = $refl->getStartLine() - 1; - $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) - - $fp = @fopen($target, 'w'); - - if ($fp === false) { - throw new Exception("Couldn't write compiled data. Failed to open $target"); - } - fwrite($fp, " $class"; + continue; + } + + $refl = new ReflectionClass($class); + $file = $refl->getFileName(); + + echo "\nIncluding -> $file"; + + $lines = file($file); + + $start = $refl->getStartLine() - 1; + $end = $refl->getEndLine(); + + $ret = array_merge($ret, array_slice($lines, $start, ($end - $start))); +} + +if ($target == null) { + $target = $path . DIRECTORY_SEPARATOR . 'Doctrine.compiled.php'; +} + +// 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) { + throw new Exception("Couldn't write compiled data. Failed to open $target"); +} +fwrite($fp, " */ -// Load Doctrine -require_once('Doctrine.php'); +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'); + +require_once(DOCTRINE_PATH . DIRECTORY_SEPARATOR . 'Doctrine.php'); + spl_autoload_register(array('Doctrine', 'autoload')); -// New Doctrine Config -$config = new Doctrine_Config(); - -// Setup Connections -//$config->addConnection('mysql://user:pass@localhost/db_name', 'connection_1'); -//$config->addConnection(new PDO('dsn', 'username', 'password'), 'connection_2); - -// Bind components/models to connections -//$config->bindComponent('User', 'connection_1'); - -// 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