Permalink
Browse files

Moved Twig-Filters to PropelTwigExtension and moved the twig init cod…

…e into parent class
  • Loading branch information...
1 parent de51665 commit a5d053f3a989084375e8caff197ee508adde1166 @mpscholten committed Oct 13, 2013
@@ -11,6 +11,7 @@
namespace Propel\Generator\Builder\Om;
use Propel\Generator\Builder\DataModelBuilder;
+use Propel\Generator\Builder\PropelTwigExtension;
use Propel\Generator\Builder\Util\PropelTemplate;
use Propel\Generator\Exception\InvalidArgumentException;
use Propel\Generator\Exception\LogicException;
@@ -53,6 +54,41 @@
protected $whiteListOfDeclaredClasses = array('PDO', 'Exception', 'DateTime');
/**
+ * Contains a instance of twig for rendering the templates
+ *
+ * @var \Twig_Environment
+ */
+ private $twig;
+
+ public function __construct(Table $table)
+ {
+ parent::__construct($table);
+ $this->twig = $this->createTwig();
+ }
+
+ protected function initTwig()
+ {
+ $loader = new \Twig_Loader_Filesystem(__DIR__ . '/templates/');
+ $twig = new \Twig_Environment($loader, [
+ 'autoescape' => false,
+ 'strict_variables' => true,
+ 'cache' => sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'propel2-cache',
+ 'auto_reload' => true
+ ]);
+ $twig->addExtension(new PropelTwigExtension());
+
+ foreach ($table->getBehaviors() as $behavior) {
+ $path = $behavior->getTemplateDirectory();
+ if ($path !== null) {
+ $loader->prependPath($behavior->getTemplateDirectory(), $behavior->getTemplateNamespace());
+ }
+ }
+
+ return $twig;
+ }
+
+
+ /**
* Builds the PHP source for current class and returns it as a string.
*
* This is the main entry point and defines a basic structure that classes should follow.
@@ -32,41 +32,6 @@
*/
class ObjectBuilder extends AbstractObjectBuilder
{
- private $twig;
-
- public function __construct(Table $table)
- {
- parent::__construct($table);
- $loader = new \Twig_Loader_Filesystem(__DIR__ . '/templates/');
- $this->twig = new \Twig_Environment($loader, ['autoescape' => false, 'strict_variables' => true, 'cache' => sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'propel2-cache', 'auto_reload' => true]);
- $this->twig->addFilter('addSlashes', new \Twig_SimpleFilter('addSlashes', 'addslashes'));
- $this->twig->addFilter('lcfirst', new \Twig_SimpleFilter('lcfirst', 'lcfirst'));
- $this->twig->addFilter('ucfirst', new \Twig_SimpleFilter('ucfirst', 'ucfirst'));
- $this->twig->addFilter('indent', new \Twig_SimpleFilter('indent', function($string) {
- $lines = explode(PHP_EOL, $string);
- $output = '';
-
- foreach($lines as $line) {
- $output .= ' ' . $line . PHP_EOL;
- }
-
- return $output;
- }));
- $this->twig->addFilter(
- 'varExport',
- new \Twig_SimpleFilter('varExport', function ($input) {
- return var_export($input, true);
- })
- );
-
- foreach($table->getBehaviors() as $behavior) {
- $path = $behavior->getTemplateDirectory();
- if($path !== null) {
- $loader->prependPath($behavior->getTemplateDirectory(), $behavior->getTemplateNamespace());
- }
- }
- }
-
/**
* Returns the package for the base object classes.
@@ -0,0 +1,118 @@
+<?php
+
+
+namespace Propel\Generator\Builder;
+
+
+use Twig_Environment;
+
+class PropelTwigExtension implements \Twig_ExtensionInterface {
+
+ /**
+ * Initializes the runtime environment.
+ *
+ * This is where you can load some file that contains filter functions for instance.
+ *
+ * @param Twig_Environment $environment The current Twig_Environment instance
+ */
+ public function initRuntime(Twig_Environment $environment)
+ {
+ }
+
+ /**
+ * Returns the token parser instances to add to the existing list.
+ *
+ * @return array An array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances
+ */
+ public function getTokenParsers()
+ {
+
+ }
+
+ /**
+ * Returns the node visitor instances to add to the existing list.
+ *
+ * @return array An array of Twig_NodeVisitorInterface instances
+ */
+ public function getNodeVisitors()
+ {
+
+ }
+
+ /**
+ * Returns a list of filters to add to the existing list.
+ *
+ * @return array An array of filters
+ */
+ public function getFilters()
+ {
+ return [
+ new \Twig_SimpleFilter('addSlashes', 'addslashes'),
+ new \Twig_SimpleFilter('lcfirst', 'lcfirst'),
+ new \Twig_SimpleFilter('ucfirst', 'ucfirst'),
+ new \Twig_SimpleFilter('indent', function ($string) {
+ $lines = explode(PHP_EOL, $string);
+ $output = '';
+
+ foreach ($lines as $line) {
+ $output .= ' ' . $line . PHP_EOL;
+ }
+
+ return $output;
+ }),
+ new \Twig_SimpleFilter('varExport', function ($input) {
+ return var_export($input, true);
+ }),
+ ];
+ }
+
+ /**
+ * Returns a list of tests to add to the existing list.
+ *
+ * @return array An array of tests
+ */
+ public function getTests()
+ {
+
+ }
+
+ /**
+ * Returns a list of functions to add to the existing list.
+ *
+ * @return array An array of functions
+ */
+ public function getFunctions()
+ {
+
+ }
+
+ /**
+ * Returns a list of operators to add to the existing list.
+ *
+ * @return array An array of operators
+ */
+ public function getOperators()
+ {
+
+ }
+
+ /**
+ * Returns a list of global variables to add to the existing list.
+ *
+ * @return array An array of global variables
+ */
+ public function getGlobals()
+ {
+
+ }
+
+ /**
+ * Returns the name of the extension.
+ *
+ * @return string The extension name
+ */
+ public function getName()
+ {
+ return 'propel';
+ }
+}

0 comments on commit a5d053f

Please sign in to comment.