Commit b2cf2e20 authored by guilhermeblanco's avatar guilhermeblanco

[2.0] Added custom Entity repository, DiscriminatorColumn and DiscriminatorMap...

[2.0] Added custom Entity repository, DiscriminatorColumn and DiscriminatorMap upport in AnnotationExporter
parent 3d34f266
...@@ -45,9 +45,15 @@ class AnnotationExporter extends AbstractExporter ...@@ -45,9 +45,15 @@ class AnnotationExporter extends AbstractExporter
private $_classToExtend; private $_classToExtend;
private $_currentCode; private $_currentCode;
/**
* Constructor
*
* @param string $dir Diretory to fetch for metadatas
*/
public function __construct($dir = null) public function __construct($dir = null)
{ {
parent::__construct($dir); parent::__construct($dir);
$this->setNumSpaces(4); $this->setNumSpaces(4);
} }
...@@ -61,6 +67,7 @@ class AnnotationExporter extends AbstractExporter ...@@ -61,6 +67,7 @@ class AnnotationExporter extends AbstractExporter
public function exportClassMetadata(ClassMetadataInfo $metadata) public function exportClassMetadata(ClassMetadataInfo $metadata)
{ {
$this->_currentCode = null; $this->_currentCode = null;
if (file_exists($this->_outputPath)) { if (file_exists($this->_outputPath)) {
$this->_currentCode = file_get_contents($this->_outputPath); $this->_currentCode = file_get_contents($this->_outputPath);
} }
...@@ -77,10 +84,13 @@ class AnnotationExporter extends AbstractExporter ...@@ -77,10 +84,13 @@ class AnnotationExporter extends AbstractExporter
$body = $code; $body = $code;
$code = $this->_currentCode; $code = $this->_currentCode;
$code = explode("\n", $code); $code = explode("\n", $code);
unset($code[array_search('}', $code)]); unset($code[array_search('}', $code)]);
foreach ($body as $line) { foreach ($body as $line) {
$code[] = $line; $code[] = $line;
} }
$code[] = '}'; $code[] = '}';
} }
...@@ -89,13 +99,16 @@ class AnnotationExporter extends AbstractExporter ...@@ -89,13 +99,16 @@ class AnnotationExporter extends AbstractExporter
// Remove empty lines before last "}" // Remove empty lines before last "}"
for ($i = count($code) - 1; $i > 0; --$i) { for ($i = count($code) - 1; $i > 0; --$i) {
$line = trim($code[$i]); $line = trim($code[$i]);
if ($line && $line != '}') { if ($line && $line != '}') {
break; break;
} }
if ( ! $line) { if ( ! $line) {
unset($code[$i]); unset($code[$i]);
} }
} }
$code = array_values($code); $code = array_values($code);
$exported = implode("\n", $code); $exported = implode("\n", $code);
...@@ -142,14 +155,18 @@ class AnnotationExporter extends AbstractExporter ...@@ -142,14 +155,18 @@ class AnnotationExporter extends AbstractExporter
foreach ($this->_metadatas as $metadata) { foreach ($this->_metadatas as $metadata) {
$outputPath = $this->_outputDir . '/' . str_replace('\\', DIRECTORY_SEPARATOR, $metadata->name) . $this->_extension; $outputPath = $this->_outputDir . '/' . str_replace('\\', DIRECTORY_SEPARATOR, $metadata->name) . $this->_extension;
$outputDir = dirname($outputPath); $outputDir = dirname($outputPath);
if ( ! is_dir($outputDir)) { if ( ! is_dir($outputDir)) {
mkdir($outputDir, 0777, true); mkdir($outputDir, 0777, true);
} }
if ( ! file_exists($outputPath)) { if ( ! file_exists($outputPath)) {
$this->_isNew = true; $this->_isNew = true;
} }
$this->_outputPath = $outputPath; $this->_outputPath = $outputPath;
$output = $this->exportClassMetadata($metadata); $output = $this->exportClassMetadata($metadata);
file_put_contents($outputPath, $output); file_put_contents($outputPath, $output);
} }
} }
...@@ -213,6 +230,85 @@ class AnnotationExporter extends AbstractExporter ...@@ -213,6 +230,85 @@ class AnnotationExporter extends AbstractExporter
return substr($metadata->name, 0, strrpos($metadata->name, '\\')); return substr($metadata->name, 0, strrpos($metadata->name, '\\'));
} }
private function _getEntityAnnotation($metadata)
{
if ($metadata->isMappedSuperclass) {
return '@MappedSupperClass';
}
$str = '@Entity';
if ($metadata->customRepositoryClassName) {
$str .= '(repositoryClass=' . $metadata->customRepositoryClassName . ')';
}
return $str;
}
private function _getTableAnnotation($metadata)
{
$table = array();
$table[] = 'name="' . $metadata->primaryTable['name'] . '"';
if (isset($metadata->primaryTable['schema'])) {
$table[] = 'schema="' . $metadata->primaryTable['schema'] . '"';
}
return '@Table(' . implode(', ', $table) . ')';
}
private function _getInheritanceAnnotation($metadata)
{
if ($metadata->inheritanceType != ClassMetadataInfo::INHERITANCE_TYPE_NONE) {
switch ($metadata->inheritanceType) {
case ClassMetadataInfo::INHERITANCE_TYPE_JOINED:
$type = "JOINED";
break;
case ClassMetadataInfo::INHERITANCE_TYPE_SINGLE_TABLE:
$type = "SINGLE_TABLE";
break;
case ClassMetadataInfo::INHERITANCE_TYPE_TABLE_PER_CLASS:
$type = "TABLE_PER_CLASS";
break;
}
return '@InheritanceType("'.$type.'")';
}
return '';
}
private function _getDiscriminatorColumnAnnotation($metadata)
{
if ($metadata->inheritanceType != ClassMetadataInfo::INHERITANCE_TYPE_NONE) {
$discrColumn = $metadata->discriminatorValue;
$columnDefinition = 'name="' . $discrColumn['name']
. '", type="' . $discrColumn['type']
. '", length=' . $discrColumn['length'];
return '@DiscriminatorColumn(' . $columnDefinition . ')';
}
return '';
}
private function _getDiscriminatorMapAnnotation($metadata)
{
if ($metadata->inheritanceType != ClassMetadataInfo::INHERITANCE_TYPE_NONE) {
$inheritanceClassMap = array();
foreach ($metadata->discriminatorMap as $type => $class) {
$inheritanceClassMap[] .= '"' . $type . '" = "' . $class . '"';
}
return '@DiscriminatorMap({' . implode(', ', $inheritanceClassMap) . '})';
}
return '';
}
private function _addMethod($type, $fieldName, $metadata, array &$methods) private function _addMethod($type, $fieldName, $metadata, array &$methods)
{ {
$methodName = $type . ucfirst($fieldName); $methodName = $type . ucfirst($fieldName);
...@@ -287,39 +383,6 @@ class AnnotationExporter extends AbstractExporter ...@@ -287,39 +383,6 @@ class AnnotationExporter extends AbstractExporter
return $methods; return $methods;
} }
private function _getTableAnnotation($metadata)
{
$table = array();
$table[] = 'name="' . $metadata->primaryTable['name'] . '"';
if (isset($metadata->primaryTable['schema'])) {
$table[] = 'schema="' . $metadata->primaryTable['schema'] . '"';
}
return '@Table(' . implode(', ', $table) . ')';
}
private function _getInheritanceAnnotation($metadata)
{
if ($metadata->inheritanceType != ClassMetadataInfo::INHERITANCE_TYPE_NONE) {
switch ($metadata->inheritanceType) {
case ClassMetadataInfo::INHERITANCE_TYPE_JOINED:
$type = "JOINED";
break;
case ClassMetadataInfo::INHERITANCE_TYPE_SINGLE_TABLE:
$type = "SINGLE_TABLE";
break;
case ClassMetadataInfo::INHERITANCE_TYPE_TABLE_PER_CLASS:
$type = "TABLE_PER_CLASS";
break;
}
return '@InheritanceType("'.$type.'")';
}
return '';
}
private function _getJoinColumnAnnotation(array $joinColumn) private function _getJoinColumnAnnotation(array $joinColumn)
{ {
$joinColumnAnnot = array(); $joinColumnAnnot = array();
......
[?php [?php
<?php if ($this->_hasNamespace($metadata)): ?> <?php if ($this->_hasNamespace($metadata)): ?>
namespace <?php echo $this->_getNamespace($metadata) ?>; namespace <?php echo $this->_getNamespace($metadata) ?>;
<?php endif; ?> <?php endif; ?>
<?php if ($this->_extendsClass()): ?>
<?php if ($this->_extendsClass()): ?>
use <?php echo $this->_getClassToExtendNamespace() ?>; use <?php echo $this->_getClassToExtendNamespace() ?>;
<?php endif; ?> <?php endif; ?>
/** /**
<?php if ($metadata->isMappedSuperclass): ?> * <?php echo $this->_getEntityAnnotation($metadata)."\n"; ?>
* @MappedSuperclass
<?php else: ?>
* @Entity
<?php endif; ?>
* <?php echo $this->_getTableAnnotation($metadata)."\n" ?> * <?php echo $this->_getTableAnnotation($metadata)."\n" ?>
* <?php echo $this->_getInheritanceAnnotation($metadata)."\n" ?> * <?php echo $this->_getInheritanceAnnotation($metadata)."\n" ?>
* <?php echo $this->_getDiscriminatorColumnAnnotation($metadata)."\n" ?>
* <?php echo $this->_getDiscriminatorMapAnnotation($metadata)."\n" ?>
*/ */
class <?Php echo $this->_getClassName($metadata) ?><?php if ($this->_extendsClass()): ?> extends <?php echo $this->_getClassToExtendName() ?><?php endif; ?> class <?php echo $this->_getClassName($metadata); ?><?php if ($this->_extendsClass()): ?> extends <?php echo $this->_getClassToExtendName() ?><?php endif; ?>
{ {
<?php include('annotation_body.tpl.php') ?> <?php include('annotation_body.tpl.php') ?>
} }
\ 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