Commit bea129db authored by Jonathan.Wage's avatar Jonathan.Wage

Added support for packages to the doctrine builder and yaml schema.

parent 094e6cb3
...@@ -38,6 +38,13 @@ ...@@ -38,6 +38,13 @@
*/ */
class Doctrine_Import_Builder class Doctrine_Import_Builder
{ {
/**
* written
*
* @var array
*/
private $written = array();
/** /**
* Path * Path
* *
...@@ -47,12 +54,20 @@ class Doctrine_Import_Builder ...@@ -47,12 +54,20 @@ class Doctrine_Import_Builder
*/ */
private $path = ''; private $path = '';
private $packagesPrefix = 'Package'; /**
* packagesPrefix
*
* @var string
*/
private $packagePrefix = 'Package';
/**
* packagesPath
*
* @var string
*/
private $packagesPath = ''; private $packagesPath = '';
private $pathAfterPackage = DIRECTORY_SEPARATOR;
/** /**
* suffix * suffix
* *
...@@ -69,8 +84,15 @@ class Doctrine_Import_Builder ...@@ -69,8 +84,15 @@ class Doctrine_Import_Builder
* *
* @var string $suffix * @var string $suffix
*/ */
private $generateBaseClasses = false; private $generateBaseClasses = true;
/**
* generateTableClasses
*
* @var string
*/
private $generateTableClasses = true;
/** /**
* baseClassesDirectory * baseClassesDirectory
* *
...@@ -107,13 +129,37 @@ class Doctrine_Import_Builder ...@@ -107,13 +129,37 @@ class Doctrine_Import_Builder
*/ */
public function setTargetPath($path) public function setTargetPath($path)
{ {
if ( ! file_exists($path)) { Doctrine::makeDirectories($path);
mkdir($path, 0777);
if (!$this->packagesPath) {
$this->packagesPath = $path . DIRECTORY_SEPARATOR . 'packages';
} }
$this->path = $path; $this->path = $path;
} }
/**
* setPackagePath
*
* @param string $packagePrefix
* @return void
*/
public function setPackagePrefix($packagePrefix)
{
$this->packagePrefix = $packagePrefix;
}
/**
* setPackagesPath
*
* @param string $packagesPath
* @return void
*/
public function setPackagesPath($packagesPath)
{
$this->packagesPath = $packagesPath;
}
/** /**
* generateBaseClasses * generateBaseClasses
* *
...@@ -121,15 +167,52 @@ class Doctrine_Import_Builder ...@@ -121,15 +167,52 @@ class Doctrine_Import_Builder
* *
* @param string $bool * @param string $bool
* @return void * @return void
* @author Jonathan H. Wage
*/ */
public function generateBaseClasses($bool = null) public function generateBaseClasses($bool = null)
{ {
if ($bool !== null) { if ($bool !== null) {
$this->generateBaseClasses = $bool; $this->generateBaseClasses = $bool;
} }
return $this->generateBaseClasses; return $this->generateBaseClasses;
}
/**
* generateTableClasses
*
* Specify whether or not to generate table classes which extend from Doctrine_Table
*
* @param string $bool
* @return void
*/
public function generateTableClasses($bool = null)
{
if ($bool !== null) {
$this->generateTableClasses = $bool;
}
return $this->generateTableClasses;
}
/**
* setBaseClassesDirectory
*
* @return void
*/
public function setBaseClassesDirectory($baseClassesDirectory)
{
$this->baseClassesDirectory;
}
/**
* setSuffix
*
* @param string $suffix
* @return void
*/
public function setSuffix($suffix)
{
$this->suffix = $suffix;
} }
/** /**
...@@ -586,52 +669,88 @@ END; ...@@ -586,52 +669,88 @@ END;
throw new Doctrine_Import_Builder_Exception('Missing class name.'); throw new Doctrine_Import_Builder_Exception('Missing class name.');
} }
if ( !isset($options['fileName'])) { if ($this->generateBaseClasses()) {
if (empty($this->path)) { $options['is_package'] = (isset($options['package']) && $options['package']) ? true:false;
throw new Doctrine_Import_Builder_Exception('No build target directory set.');
if ($options['is_package']) {
$e = explode('.', $options['package']);
$options['package_name'] = $e[0];
unset($e[0]);
$options['package_path'] = implode(DIRECTORY_SEPARATOR, $e);
} }
// Top level definition that extends from all the others
if (is_writable($this->path) === false) { $topLevel = $options;
throw new Doctrine_Import_Builder_Exception('Build target directory ' . $this->path . ' is not writable.'); unset($topLevel['tableName']);
// If we have a package then we need to make this extend the package definition and not the base definition
// The package definition will then extends the base definition
$topLevel['inheritance']['extends'] = (isset($topLevel['package']) && $topLevel['package']) ? $this->packagePrefix . $topLevel['className']:'Base' . $topLevel['className'];
$topLevel['no_definition'] = true;
$topLevel['generate_once'] = true;
$topLevel['is_main_class'] = true;
// Package level definition that extends from the base definition
if (isset($options['package'])) {
$packageLevel = $options;
$packageLevel['className'] = $topLevel['inheritance']['extends'];
$packageLevel['inheritance']['extends'] = 'Base' . $topLevel['className'];
$packageLevel['no_definition'] = true;
$packageLevel['abstract'] = true;
$packageLevel['override_parent'] = true;
$packageLevel['generate_once'] = true;
$packageLevel['is_package_class'] = true;
} }
$options['fileName'] = $this->path . DIRECTORY_SEPARATOR . $options['className'] . $this->suffix; $baseClass = $options;
} $baseClass['className'] = 'Base' . $baseClass['className'];
$baseClass['abstract'] = true;
if ($this->generateBaseClasses()) { $baseClass['override_parent'] = true;
$baseClass['is_base_class'] = true;
// We only want to generate this one if it doesn't already exist
if ( ! file_exists($options['fileName'])) { $this->writeDefinition($baseClass, $columns, $relations, $indexes, $attributes, $templates, $actAs);
$optionsBak = $options;
unset($options['tableName']);
$options['inheritance']['extends'] = 'Base' . $options['className'];
$options['requires'] = array($this->baseClassesDirectory . DIRECTORY_SEPARATOR . $options['inheritance']['extends'] . $this->suffix);
$options['no_definition'] = true;
$this->writeDefinition($options); if (!empty($packageLevel)) {
$this->writeDefinition($packageLevel);
}
$options = $optionsBak; $this->writeDefinition($topLevel);
}
$generatedPath = $this->path . DIRECTORY_SEPARATOR . $this->baseClassesDirectory;
if ( ! file_exists($generatedPath)) {
mkdir($generatedPath);
}
$options['className'] = 'Base' . $options['className'];
$options['abstract'] = true;
$options['fileName'] = $generatedPath . DIRECTORY_SEPARATOR . $options['className'] . $this->suffix;
$options['override_parent'] = true;
$this->writeDefinition($options, $columns, $relations, $indexes, $attributes, $templates, $actAs);
} else { } else {
$this->writeDefinition($options, $columns, $relations, $indexes, $attributes, $templates, $actAs); $this->writeDefinition($options, $columns, $relations, $indexes, $attributes, $templates, $actAs);
} }
} }
/**
* writeTableDefinition
*
* @return void
*/
public function writeTableDefinition($className, $path, $options = array())
{
$className = $className . 'Table';
$content = '<?php' . PHP_EOL;
$content .= sprintf(self::$tpl, false,
$className,
isset($options['extends']) ? $options['extends']:'Doctrine_Table',
null,
null,
null
);
Doctrine::makeDirectories($path);
$writePath = $path . DIRECTORY_SEPARATOR . $className . $this->suffix;
$this->written[$className] = $writePath;
if (!file_exists($writePath)) {
file_put_contents($writePath, $content);
}
}
/** /**
* writeDefinition * writeDefinition
* *
...@@ -646,14 +765,64 @@ END; ...@@ -646,14 +765,64 @@ END;
*/ */
public function writeDefinition(array $options, array $columns = array(), array $relations = array(), array $indexes = array(), array $attributes = array(), array $templates = array(), array $actAs = array()) public function writeDefinition(array $options, array $columns = array(), array $relations = array(), array $indexes = array(), array $attributes = array(), array $templates = array(), array $actAs = array())
{ {
$content = $this->buildDefinition($options, $columns, $relations, $indexes, $attributes, $templates, $actAs); $definition = $this->buildDefinition($options, $columns, $relations, $indexes, $attributes, $templates, $actAs);
$code = "<?php\n";
$fileName = $options['className'] . $this->suffix;
$packagesPath = $this->packagesPath ? $this->packagesPath:$this->path;
// If this is a main class that either extends from Base or Package class
if (isset($options['is_main_class']) && $options['is_main_class']) {
// If is package then we need to put it in a package subfolder
if (isset($options['is_package']) && $options['is_package']) {
$writePath = $this->path . DIRECTORY_SEPARATOR . $options['package_name'];
$this->writeTableDefinition($options['className'], $writePath, array('extends' => $options['inheritance']['extends'] . 'Table'));
// Otherwise lets just put it in the root of the path
} else {
$writePath = $this->path;
$this->writeTableDefinition($options['className'], $writePath);
}
}
// If is the package class then we need to make the path to the complete package
if (isset($options['is_package_class']) && $options['is_package_class']) {
$path = str_replace('.', DIRECTORY_SEPARATOR, $options['package']);
$writePath = $packagesPath . DIRECTORY_SEPARATOR . $path;
$this->writeTableDefinition($options['className'], $writePath);
}
// If it is the base class of the doctrine record definition
if (isset($options['is_base_class']) && $options['is_base_class']) {
// If it is a part of a package then we need to put it in a package subfolder
if (isset($options['is_package']) && $options['is_package']) {
$writePath = $this->path . DIRECTORY_SEPARATOR . $options['package_name'] . DIRECTORY_SEPARATOR . $this->baseClassesDirectory;
// Otherwise lets just put it in the root generated folder
} else {
$writePath = $this->path . DIRECTORY_SEPARATOR . $this->baseClassesDirectory;
}
}
if (isset($writePath)) {
Doctrine::makeDirectories($writePath);
$writePath .= DIRECTORY_SEPARATOR . $fileName;
} else {
Doctrine::makeDirectories($this->path);
$writePath = $this->path . DIRECTORY_SEPARATOR . $fileName;
}
$code = "<?php" . PHP_EOL;
if (isset($options['requires'])) { if (isset($options['requires'])) {
if ( ! is_array($options['requires'])) { if ( ! is_array($options['requires'])) {
$options['requires'] = array($options['requires']); $options['requires'] = array($options['requires']);
} }
foreach ($options['requires'] as $require) { foreach ($options['requires'] as $require) {
$code .= "require_once('".$require."');\n"; $code .= "require_once('".$require."');\n";
} }
...@@ -663,13 +832,22 @@ END; ...@@ -663,13 +832,22 @@ END;
$code .= "// Connection Component Binding\n"; $code .= "// Connection Component Binding\n";
$code .= "Doctrine_Manager::getInstance()->bindComponent('" . $options['connectionClassName'] . "', '" . $options['connection'] . "');\n"; $code .= "Doctrine_Manager::getInstance()->bindComponent('" . $options['connectionClassName'] . "', '" . $options['connection'] . "');\n";
} }
$code .= PHP_EOL . $content;
$bytes = file_put_contents($options['fileName'], $code); $code .= PHP_EOL . $definition;
$this->written[$options['className']] = $writePath;
if (isset($options['generate_once']) && $options['generate_once'] === true) {
if (!file_exists($writePath)) {
$bytes = file_put_contents($writePath, $code);
}
} else {
$bytes = file_put_contents($writePath, $code);
}
if ($bytes === false) { if (isset($bytes) && $bytes === false) {
throw new Doctrine_Import_Builder_Exception("Couldn't write file " . $options['fileName']); throw new Doctrine_Import_Builder_Exception("Couldn't write file " . $writePath);
} }
} }
} }
\ No newline at end of file
...@@ -121,7 +121,7 @@ class Doctrine_Import_Schema ...@@ -121,7 +121,7 @@ class Doctrine_Import_Schema
continue; continue;
} }
$options = $this->getOptions($properties, $directory); $options = $this->getOptions($properties);
$columns = $this->getColumns($properties); $columns = $this->getColumns($properties);
$relations = $this->getRelations($properties); $relations = $this->getRelations($properties);
$indexes = $this->getIndexes($properties); $indexes = $this->getIndexes($properties);
...@@ -136,20 +136,18 @@ class Doctrine_Import_Schema ...@@ -136,20 +136,18 @@ class Doctrine_Import_Schema
/** /**
* getOptions * getOptions
* *
* FIXME: Directory argument needs to be removed
*
* @param string $properties Array of table properties * @param string $properties Array of table properties
* @param string $directory Directory we are writing the class to * @param string $directory Directory we are writing the class to
* @return array $options Array of options from a parse schemas properties * @return array $options Array of options from a parse schemas properties
*/ */
public function getOptions($properties, $directory) public function getOptions($properties)
{ {
$options = array(); $options = array();
$options['className'] = $properties['className']; $options['className'] = $properties['className'];
$options['fileName'] = $directory.DIRECTORY_SEPARATOR.$properties['className'].'.class.php';
$options['tableName'] = isset($properties['tableName']) ? $properties['tableName']:null; $options['tableName'] = isset($properties['tableName']) ? $properties['tableName']:null;
$options['connection'] = isset($properties['connection']) ? $properties['connection']:null; $options['connection'] = isset($properties['connection']) ? $properties['connection']:null;
$options['connectionClassName'] = isset($properties['connection']) ? $properties['className']:null; $options['connectionClassName'] = isset($properties['connection']) ? $properties['className']:null;
$options['package'] = $properties['package'];
if (isset($properties['inheritance'])) { if (isset($properties['inheritance'])) {
$options['inheritance'] = $properties['inheritance']; $options['inheritance'] = $properties['inheritance'];
...@@ -290,6 +288,7 @@ class Doctrine_Import_Schema ...@@ -290,6 +288,7 @@ class Doctrine_Import_Schema
$build[$className]['attributes'] = isset($table['attributes']) ? $table['attributes']:array(); $build[$className]['attributes'] = isset($table['attributes']) ? $table['attributes']:array();
$build[$className]['templates'] = isset($table['templates']) ? $table['templates']:array(); $build[$className]['templates'] = isset($table['templates']) ? $table['templates']:array();
$build[$className]['actAs'] = isset($table['actAs']) ? $table['actAs']:array(); $build[$className]['actAs'] = isset($table['actAs']) ? $table['actAs']:array();
$build[$className]['package'] = isset($table['package']) ? $table['package']:null;
} }
if (isset($table['inheritance'])) { if (isset($table['inheritance'])) {
......
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