Permalink
Browse files

Issue #289

  • Loading branch information...
1 parent 4834bb6 commit 0cc80095cd0d4f1f5e64f3a142beaa14424943f5 @cristianoc72 cristianoc72 committed Aug 10, 2012
View
45 src/Propel/Generator/Builder/Om/AbstractOMBuilder.php
@@ -73,6 +73,7 @@ public function build()
$this->addClassClose($script);
$ignoredNamespace = $this->getNamespace();
+
if ($useStatements = $this->getUseStatements($ignoredNamespace ?: 'namespace')) {
$script = $useStatements . $script;
}
@@ -81,9 +82,11 @@ public function build()
$script = $namespaceStatement . $script;
}
- return '<' . "?php
+ $script = "<?php
" . $script;
+
+ return $this->clean($script);
}
/**
@@ -777,4 +780,44 @@ public function renderTemplate($filename, $vars = array(), $templateDir = '/temp
return $template->render($vars);
}
+
+ /**
+ * Most of the code comes from the PHP-CS-Fixer project
+ */
+ private function clean($content)
+ {
+ // trailing whitespaces
+ $content = preg_replace('/[ \t]*$/m', '', $content);
+
+ // indentation
+ $content = preg_replace_callback('/^([ \t]+)/m', function ($matches) use ($content) {
+ return str_replace("\t", ' ', $matches[0]);
+ }, $content);
+
+ // line feed
+ $content = str_replace("\r\n", "\n", $content);
+
+ // Unused "use" statements
+ preg_match_all('/^use (?P<class>[^\s;]+)(?:\s+as\s+(?P<alias>.*))?;/m', $content, $matches, PREG_SET_ORDER);
+ foreach ($matches as $match) {
+ if (isset($match['alias'])) {
+ $short = $match['alias'];
+ } else {
+ $parts = explode('\\', $match['class']);
+ $short = array_pop($parts);
+ }
+
+ preg_match_all('/\b'.$short.'\b/i', str_replace($match[0]."\n", '', $content), $m);
+ if (!count($m[0])) {
+ $content = str_replace($match[0]."\n", '', $content);
+ }
+ }
+
+ // end of line
+ if (strlen($content) && "\n" != substr($content, -1)) {
+ $content = $content."\n";
+ }
+
+ return $content;
+ }
}
View
24 src/Propel/Generator/Builder/Om/ExtensionObjectBuilder.php
@@ -41,28 +41,31 @@ protected function addClassOpen(&$script)
$tableDesc = $table->getDescription();
$baseClassName = $this->getClassNameFromBuilder($this->getObjectBuilder());
- $script .= "
+ if ($this->getBuildProperty('addClassLevelComment')) {
+ $script .= "
/**
* Skeleton subclass for representing a row from the '$tableName' table.
*
* $tableDesc
*";
- if ($this->getBuildProperty('addTimeStamp')) {
- $now = strftime('%c');
- $script .= "
+ if ($this->getBuildProperty('addTimeStamp')) {
+ $now = strftime('%c');
+ $script .= "
* This class was autogenerated by Propel " . $this->getBuildProperty('version') . " on:
*
* $now
*";
- }
- $script .= "
+ }
+ $script .= "
* You should add additional methods to this class to meet the
* application requirements. This class will only be generated as
* long as it does not already exist in the output directory.
*
- */
-".($table->isAbstract() ? "abstract " : "")."class ".$this->getUnqualifiedClassName()." extends $baseClassName {
+ */";
+ }
+ $script .= "
+ ".($table->isAbstract() ? "abstract " : "")."class ".$this->getUnqualifiedClassName()." extends $baseClassName {
";
}
@@ -85,9 +88,8 @@ protected function addClassBody(&$script)
protected function addClassClose(&$script)
{
$script .= "
-} // " . $this->getUnqualifiedClassName() . "
+}
";
$this->applyBehaviorModifier('extensionObjectFilter', $script, "");
}
-
-} // ExtensionObjectBuilder
+}
View
21 src/Propel/Generator/Builder/Om/ObjectBuilder.php
@@ -207,22 +207,28 @@ protected function addClassOpen(&$script)
null !== ($parentClass = ClassTools::classname($this->getBaseClass()))) {
$parentClass = ' extends '.$parentClass;
}
- $script .= "
+
+ if ($this->getBuildProperty('addClassLevelComment')) {
+ $script .= "
/**
* Base class that represents a row from the '$tableName' table.
*
* $tableDesc
*";
- if ($this->getBuildProperty('addTimeStamp')) {
- $now = strftime('%c');
- $script .= "
+ if ($this->getBuildProperty('addTimeStamp')) {
+ $now = strftime('%c');
+ $script .= "
* This class was autogenerated by Propel " . $this->getBuildProperty('version') . " on:
*
* $now
*";
+ }
+ $script .= "
+* @package propel.generator.".$this->getPackage()."
+*/";
}
+
$script .= "
- */
abstract class ".$this->getUnqualifiedClassName().$parentClass." implements ActiveRecordInterface ";
if ($interface = $this->getInterface()) {
@@ -235,8 +241,7 @@ protected function addClassOpen(&$script)
}
$script .= "
-{
-";
+{";
}
/**
@@ -355,7 +360,7 @@ protected function addClassBody(&$script)
protected function addClassClose(&$script)
{
$script .= "
-} // " . $this->getUnqualifiedClassName() . "
+}
";
$this->applyBehaviorModifier('objectFilter', $script, "");
}
View
1 tools/generator/default.properties
@@ -84,6 +84,7 @@ propel.addGenericAccessors = true
propel.addGenericMutators = true
propel.addSaveMethod = true
propel.addTimeStamp = false
+propel.addClassLevelComment = true
propel.addHooks = true
propel.saveException = \Propel\Generator\Exception\PropelException
propel.emulateForeignKeyConstraints = false

0 comments on commit 0cc8009

Please sign in to comment.