Author: stefbach
Date: 2010-03-10 00:31:01 +0100 (Wed, 10 Mar 2010)
New Revision: 28442

Added:
   plugins/sfAmfPlugin/lib/task/sfAmfPluginGeneratorTask.php
Modified:
   plugins/sfAmfPlugin/
   plugins/sfAmfPlugin/lib/task/generator/sfAmfPluginGenerator.class.php
   plugins/sfAmfPlugin/lib/task/sfAmfPluginBuildServiceTask.class.php
   plugins/sfAmfPlugin/lib/task/sfAmfPluginBuildValueObjectTask.class.php
Log:
 [sfAmfPlugin] Made some refactoring on tasks/generators



Property changes on: plugins/sfAmfPlugin
___________________________________________________________________
Modified: svk:merge
   - 7ce51a29-67ea-4596-9a32-2e0fcaf8b5e0:/local/symfony/sfAmfPlugin:37
   + 7ce51a29-67ea-4596-9a32-2e0fcaf8b5e0:/local/symfony/sfAmfPlugin:39

Modified: plugins/sfAmfPlugin/lib/task/generator/sfAmfPluginGenerator.class.php
===================================================================
--- plugins/sfAmfPlugin/lib/task/generator/sfAmfPluginGenerator.class.php       
2010-03-09 17:55:36 UTC (rev 28441)
+++ plugins/sfAmfPlugin/lib/task/generator/sfAmfPluginGenerator.class.php       
2010-03-09 23:31:01 UTC (rev 28442)
@@ -23,11 +23,43 @@
   protected $connection = null;
   protected $serviceParent = null;
 
+  // for logging
+  private $dispatcher = null;
+  private $formatter = null;
+  private $logSection = 'amf';
+
   /**
    *
    */ 
+  public function initialize(sfGeneratorManager $generatorManager)
+  {
+    parent::initialize($generatorManager);
+
+    $this->dispatcher = new sfEventDispatcher();
+    $this->formatter = new sfFormatter();
+  }
+
+  private function initializeLogSection(sfEventDispatcher $dispatcher, 
sfFormatter $formatter)
+  {
+    $this->dispatcher = $dispatcher;
+    $this->formatter  = $formatter;
+  }
+
+  /**
+   *
+   */ 
+  public function logSection($message, $size = null, $style = 'INFO')
+  {
+    $this->dispatcher->notify(new sfEvent($this, 'command.log', 
array($this->formatter->formatSection($this->logSection, $message, $size, 
$style))));
+  }
+
+  /**
+   *
+   */ 
   public function generate($params = array())
   {
+    $this->initializeLogSection($params['dispatcher'], $params['formatter']);
+
     $this->setPackage($params['package']);
     $this->setServiceDirname($params['service_dir']);
     $this->setConnection($params['connection']);
@@ -77,13 +109,22 @@
   /**
    *
    */ 
-  protected function getPackage($dir = false)
+  protected function getPackage()
   {
-    if (is_null($this->package))
+    return $this->package;
+  }
+
+  /**
+   *
+   */
+  protected function getPackageDirectory($package = null)
+  {
+    $package = is_null($package) ? $this->package : $package; 
+
+    if (is_null($package))
       return '';
 
-    return !$dir ? $this->package : str_replace('.', '/', $this->package); 
-
+    return str_replace('.', '/', $package);
   }
 
   /**
@@ -109,7 +150,7 @@
   {
     return sfConfig::get('sf_lib_dir') . '/' . 
                     $this->getServiceDirname() . '/' . 
-                    (is_null($package) ? $this->getPackage(true) : $package). 
'/';
+                    $this->getPackageDirectory($package) . '/';
   }
 
   /**
@@ -143,5 +184,7 @@
 
     file_put_contents($file, $template);
     $this->replaceTokens($file);
+
+    $this->logSection(sprintf("Added %s", basename($file)));
   }
 }

Modified: plugins/sfAmfPlugin/lib/task/sfAmfPluginBuildServiceTask.class.php
===================================================================
--- plugins/sfAmfPlugin/lib/task/sfAmfPluginBuildServiceTask.class.php  
2010-03-09 17:55:36 UTC (rev 28441)
+++ plugins/sfAmfPlugin/lib/task/sfAmfPluginBuildServiceTask.class.php  
2010-03-09 23:31:01 UTC (rev 28442)
@@ -17,7 +17,7 @@
  * @version SVN: $Id $
  */
 
-class sfAmfPluginBuildServiceTask extends sfBaseTask 
+class sfAmfPluginBuildServiceTask extends sfAmfPluginGeneratorTask 
 {
   /**
    * @see sfTask
@@ -49,32 +49,15 @@
   {
     $this->logSection($this->namespace, 'generating service classes');
 
-    $generatorManager = new sfGeneratorManager($this->configuration);
-    $generatorManager->generate($this->generatorClass($options['connection']), 
array(
-      'connection'        => $options['connection'],
-      'service_dir'       => $options['service_dir'],
-      'package'           => $options['package'],
+    $this->generate(
+      'Service', 
+      $options['connection'], 
+      array(
+        'connection'        => $options['connection'],
+        'service_dir'       => $options['service_dir'],
+        'package'           => $options['package'],
     ));
-
   }
 
-  /**
-   *
-   */ 
-  private function generatorClass($connection)
-  {
-    switch($connection)
-    {
-      case 'propel':
-        return 'sfAmfPluginPropelServiceGenerator';
-
-      case 'doctrine':
-        return 'sfAmfPluginDoctrineServiceGenerator';
-
-      default:
-        throw new sfCommandException("Connection is not supported. (Only 
propel or doctrine)");  
-    }
-  }
-    
 }
     

Modified: plugins/sfAmfPlugin/lib/task/sfAmfPluginBuildValueObjectTask.class.php
===================================================================
--- plugins/sfAmfPlugin/lib/task/sfAmfPluginBuildValueObjectTask.class.php      
2010-03-09 17:55:36 UTC (rev 28441)
+++ plugins/sfAmfPlugin/lib/task/sfAmfPluginBuildValueObjectTask.class.php      
2010-03-09 23:31:01 UTC (rev 28442)
@@ -17,7 +17,7 @@
  * @version SVN: $Id $
  */
 
-class sfAmfPluginBuildValueObjectTask extends sfBaseTask 
+class sfAmfPluginBuildValueObjectTask extends sfAmfPluginGeneratorTask 
 {
   /**
    * @see sfTask
@@ -49,32 +49,16 @@
   {
     $this->logSection($this->namespace, 'generating valueobject classes');
 
-    $generatorManager = new sfGeneratorManager($this->configuration);
-    $generatorManager->generate($this->generatorClass($options['connection']), 
array(
-      'connection'        => $options['connection'],
-      'service_dir'       => $options['service_dir'],
-      'package'           => $options['package'],
+    $this->generate(
+      'ValueObject', 
+      $options['connection'], 
+      array(
+        'connection'        => $options['connection'],
+        'service_dir'       => $options['service_dir'],
+        'package'           => $options['package'],
     ));
 
   }
-
-  /**
-   *
-   */ 
-  private function generatorClass($connection)
-  {
-    switch($connection)
-    {
-      case 'propel':
-        return 'sfAmfPluginPropelValueObjectGenerator';
-
-      case 'doctrine':
-        return 'sfAmfPluginDoctrineValueObjectGenerator';
-
-      default:
-        throw new sfCommandException("Connection is not supported. (Only 
propel or doctrine)");  
-    }
-  }
     
 }
     

Added: plugins/sfAmfPlugin/lib/task/sfAmfPluginGeneratorTask.php
===================================================================
--- plugins/sfAmfPlugin/lib/task/sfAmfPluginGeneratorTask.php                   
        (rev 0)
+++ plugins/sfAmfPlugin/lib/task/sfAmfPluginGeneratorTask.php   2010-03-09 
23:31:01 UTC (rev 28442)
@@ -0,0 +1,50 @@
+<?php
+/**
+ * @package:     sfAmfPluginTask
+ * @author:      Stephane Bachelier ([email protected])
+ * @license:     GPL (see http://www.gnu.org/licenses/gpl.txt)
+ * @created:     2010-03-09.
+ * @last change: 19-Mär-2005.
+ * @version:     0.0
+ * Description:
+ * Usage:
+ * TODO:
+ * CHANGES:
+ * 
+ */
+
+abstract class sfAmfPluginGeneratorTask extends sfBaseTask {
+    
+  /**
+   *
+   */ 
+  protected function generatorClass($connection, $generator)
+  {
+    switch($connection)
+    {
+      case 'propel':
+      case 'doctrine':
+        $class = 'sfAmfPlugin' . lcfirst($connection) . $generator . 
'Generator';
+
+        if (!class_exists($class))
+        {
+          throw new sfCommandException(sprintf("[%s] generator is not found", 
$class));
+        }
+        return $class;
+
+      default:
+        throw new sfCommandException("Connection is not supported. (Only 
propel or doctrine)");  
+    }
+  }
+
+  protected function generate($type, $connection, $params)
+  {
+    $baseOptions = array(
+      'dispatcher'        => $this->dispatcher,
+      'formatter'         => $this->formatter,
+    );
+
+    $generatorManager = new sfGeneratorManager($this->configuration);
+    $generatorManager->generate($this->generatorClass($connection, $type), 
array_merge($baseOptions, $params));
+  }
+}


Property changes on: plugins/sfAmfPlugin/lib/task/sfAmfPluginGeneratorTask.php
___________________________________________________________________
Added: svn:mime-type
   + text/x-php
Added: svn:keywords
   + Id Rev Date
Added: svn:eol-style
   + native

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.

Reply via email to