Author: stefbach
Date: 2010-03-09 01:36:30 +0100 (Tue, 09 Mar 2010)
New Revision: 28422

Added:
   plugins/sfAmfPlugin/data/
   plugins/sfAmfPlugin/data/generator/
   plugins/sfAmfPlugin/data/generator/sfAmfPropelService/
   plugins/sfAmfPlugin/data/generator/sfAmfPropelService/default/
   plugins/sfAmfPlugin/data/generator/sfAmfPropelService/default/template/
   
plugins/sfAmfPlugin/data/generator/sfAmfPropelService/default/template/sfAmfServiceTemplate.php
   plugins/sfAmfPlugin/lib/task/generator/
   plugins/sfAmfPlugin/lib/task/generator/doctrine/
   
plugins/sfAmfPlugin/lib/task/generator/doctrine/sfAmfPluginDoctrineGenerator.class.php
   
plugins/sfAmfPlugin/lib/task/generator/doctrine/sfAmfPluginDoctrineServiceGenerator.class.php
   plugins/sfAmfPlugin/lib/task/generator/propel/
   
plugins/sfAmfPlugin/lib/task/generator/propel/sfAmfPluginPropelGenerator.class.php
   
plugins/sfAmfPlugin/lib/task/generator/propel/sfAmfPluginPropelServiceGenerator.class.php
   plugins/sfAmfPlugin/lib/task/generator/sfAmfPluginGenerator.class.php
   plugins/sfAmfPlugin/lib/task/sfAmfPluginBuildServiceTask.class.php
Modified:
   plugins/sfAmfPlugin/
Log:
[sfAmfPlugin] Added generator to create services for all tables



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

Added: 
plugins/sfAmfPlugin/data/generator/sfAmfPropelService/default/template/sfAmfServiceTemplate.php
===================================================================
--- 
plugins/sfAmfPlugin/data/generator/sfAmfPropelService/default/template/sfAmfServiceTemplate.php
                             (rev 0)
+++ 
plugins/sfAmfPlugin/data/generator/sfAmfPropelService/default/template/sfAmfServiceTemplate.php
     2010-03-09 00:36:30 UTC (rev 28422)
@@ -0,0 +1,12 @@
+[?php
+
+/**
+ * <?php echo $this->table->getClassname() ?> service.
+ *
+ * @package    ##PACKAGE_NAME##
+ * @author     ##AUTHOR_NAME##
+ */
+class <?php echo $this->table->getClassname() ?>Service extends sfAmfService
+{
+
+}


Property changes on: 
plugins/sfAmfPlugin/data/generator/sfAmfPropelService/default/template/sfAmfServiceTemplate.php
___________________________________________________________________
Added: svn:mime-type
   + text/x-php
Added: svn:keywords
   + Id Rev Date
Added: svn:eol-style
   + native

Added: 
plugins/sfAmfPlugin/lib/task/generator/doctrine/sfAmfPluginDoctrineGenerator.class.php
===================================================================
--- 
plugins/sfAmfPlugin/lib/task/generator/doctrine/sfAmfPluginDoctrineGenerator.class.php
                              (rev 0)
+++ 
plugins/sfAmfPlugin/lib/task/generator/doctrine/sfAmfPluginDoctrineGenerator.class.php
      2010-03-09 00:36:30 UTC (rev 28422)
@@ -0,0 +1,37 @@
+<?php
+/**
+ * This file is part of the sfAmfPlugin package.
+ * (c) 2008, 2009 Timo Haberkern <[email protected]>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * Generator-Class for the generation of AMF-Service class based on Doctrine 
+ *
+ * @author Stephane Bachelier (http://blog.0x89b.org)
+ * @copyright Stephane Bachelier
+ * @license MIT
+ * @version SVN: $Id $
+ */
+
+abstract class sfAmfPluginDoctrineGenerator extends sfAmfPluginGenerator
+{
+  public function initialize(sfGeneratorManager $generatorManager)
+  {
+    parent::initialize($generatorManager);
+
+    $this->setGeneratorClass('sfAmfDoctrineService');
+  }
+
+  protected function setConnection($connection)
+  {
+    // TODO
+  }
+
+  protected function loadClasses()
+  {
+    // TODO
+  }
+}


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

Added: 
plugins/sfAmfPlugin/lib/task/generator/doctrine/sfAmfPluginDoctrineServiceGenerator.class.php
===================================================================
--- 
plugins/sfAmfPlugin/lib/task/generator/doctrine/sfAmfPluginDoctrineServiceGenerator.class.php
                               (rev 0)
+++ 
plugins/sfAmfPlugin/lib/task/generator/doctrine/sfAmfPluginDoctrineServiceGenerator.class.php
       2010-03-09 00:36:30 UTC (rev 28422)
@@ -0,0 +1,32 @@
+<?php
+/**
+ * This file is part of the sfAmfPlugin package.
+ * (c) 2008, 2009 Timo Haberkern <[email protected]>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * Generator-Class for the generation of AMF-Service class based on Doctrine 
+ *
+ * @author Stephane Bachelier (http://blog.0x89b.org)
+ * @copyright Stephane Bachelier
+ * @license MIT
+ * @version SVN: $Id $
+ */
+
+class sfAmfPluginDoctrineServiceGenerator extends sfAmfPluginDoctrineGenerator
+{
+  public function initialize(sfGeneratorManager $generatorManager)
+  {
+    parent::initialize($generatorManager);
+  }
+
+  public function generateAll()
+  {
+    // TODO
+    throw new sfCommandException("Doctrine generator must be implemented.");
+  }
+
+}


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

Added: 
plugins/sfAmfPlugin/lib/task/generator/propel/sfAmfPluginPropelGenerator.class.php
===================================================================
--- 
plugins/sfAmfPlugin/lib/task/generator/propel/sfAmfPluginPropelGenerator.class.php
                          (rev 0)
+++ 
plugins/sfAmfPlugin/lib/task/generator/propel/sfAmfPluginPropelGenerator.class.php
  2010-03-09 00:36:30 UTC (rev 28422)
@@ -0,0 +1,62 @@
+<?php
+/**
+ * This file is part of the sfAmfPlugin package.
+ * (c) 2008, 2009 Timo Haberkern <[email protected]>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * Generator-Class for the generation of AMF-Service class based on Propel 
+ *
+ * @author Stephane Bachelier (http://blog.0x89b.org)
+ * @copyright Stephane Bachelier
+ * @license MIT
+ * @version SVN: $Id $
+ */
+
+abstract class sfAmfPluginPropelGenerator extends sfAmfPluginGenerator
+{
+  protected $dbMap = null;
+
+  public function initialize(sfGeneratorManager $generatorManager)
+  {
+    parent::initialize($generatorManager);
+
+    $this->setGeneratorClass('sfAmfPropelService');
+  }
+
+  protected function setConnection($connection)
+  {
+    $this->dbMap = Propel::getDatabaseMap($connection);
+  }
+
+  /**
+   * load map builder classes
+   */ 
+  protected function loadClasses()
+  {
+    if (is_null($this->dbMap))
+    {
+      throw new sfCommandException('Connection is not set');
+    }
+
+    $classes = 
sfFinder::type('file')->name('*TableMap.php')->in($this->generatorManager->getConfiguration()->getModelDirs());
+
+    if (count($classes) == 0)
+    {
+      throw new sfCommandException("Model classes are not found! Are you sure 
that you've run php symfony propel:build-model ? ");
+    }
+
+    foreach( $classes as $class )
+    {
+      $omClass = basename($class, 'TableMap.php');
+      if (class_exists($omClass) && is_subclass_of($omClass, 'BaseObject'))
+      {
+        $tableMapClass = basename($class, '.php');
+        $this->dbMap->addTableFromMapClass($tableMapClass);
+      }
+    }
+  }
+}


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

Added: 
plugins/sfAmfPlugin/lib/task/generator/propel/sfAmfPluginPropelServiceGenerator.class.php
===================================================================
--- 
plugins/sfAmfPlugin/lib/task/generator/propel/sfAmfPluginPropelServiceGenerator.class.php
                           (rev 0)
+++ 
plugins/sfAmfPlugin/lib/task/generator/propel/sfAmfPluginPropelServiceGenerator.class.php
   2010-03-09 00:36:30 UTC (rev 28422)
@@ -0,0 +1,52 @@
+<?php
+/**
+ * This file is part of the sfAmfPlugin package.
+ * (c) 2008, 2009 Timo Haberkern <[email protected]>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * Generator-Class for the generation of AMF-Service class based on Propel 
+ *
+ * @author Stephane Bachelier (http://blog.0x89b.org)
+ * @copyright Stephane Bachelier
+ * @license MIT
+ * @version SVN: $Id $
+ */
+
+class sfAmfPluginPropelServiceGenerator extends sfAmfPluginPropelGenerator
+{
+  public function initialize(sfGeneratorManager $generatorManager)
+  {
+    parent::initialize($generatorManager);
+  }
+
+  protected function generateAll()
+  {
+    // generate files for each table
+    foreach ($this->dbMap->getTables() as $tableName => $table)
+    {
+      $this->table = $table;
+      $tableClassname = $table->getClassname();
+
+      $serviceName = $tableClassname . 'Service';
+      $serviceFile = $this->getServiceFileName($serviceName);
+
+      if (file_exists($serviceFile))
+      {
+        throw new sfCommandException(
+          sprintf("Services already exist in %s directory. You must remove 
them manually",
+          $this->getServiceDirname()
+        ));
+      }
+      
+      file_put_contents($serviceFile, 
+        $this->evalTemplate('sfAmfServiceTemplate.php'));
+
+      $this->replaceTokens($serviceFile);
+    }
+  }
+
+}


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

Added: plugins/sfAmfPlugin/lib/task/generator/sfAmfPluginGenerator.class.php
===================================================================
--- plugins/sfAmfPlugin/lib/task/generator/sfAmfPluginGenerator.class.php       
                        (rev 0)
+++ plugins/sfAmfPlugin/lib/task/generator/sfAmfPluginGenerator.class.php       
2010-03-09 00:36:30 UTC (rev 28422)
@@ -0,0 +1,132 @@
+<?php
+/**
+ * This file is part of the sfAmfPlugin package.
+ * (c) 2008, 2009 Timo Haberkern <[email protected]>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * Generator-Class for the generation of AMF-Service class based on Doctrine 
+ *
+ * @author Stephane Bachelier (http://blog.0x89b.org)
+ * @copyright Stephane Bachelier
+ * @license MIT
+ * @version SVN: $Id $
+ */
+
+abstract class sfAmfPluginGenerator extends sfGenerator
+{
+  protected $package = null;
+  protected $serviceDirname = null;
+  protected $connection = null;
+
+  /**
+   *
+   */ 
+  public function generate($params = array())
+  {
+    $this->setPackage($params['package']);
+    $this->setServiceDirname($params['service_dir']);
+    $this->setConnection($params['connection']);
+
+    // verify and generate
+    $this->checkServiceDirectories();
+    $this->loadClasses();
+    $this->generateAll();
+  }
+
+  abstract protected function setConnection($connection);
+  abstract protected function loadClasses();
+
+  abstract protected function generateAll();
+
+  /**
+   *
+   */ 
+  protected function replaceTokens($file)
+  {
+    $properties = 
parse_ini_file(sfConfig::get('sf_config_dir').'/properties.ini', true);
+    $constants = array(
+      'PROJECT_NAME'  => isset($properties['symfony']['name']) ? 
$properties['symfony']['name'] : 'symfony',
+      'AUTHOR_NAME'   => isset($properties['symfony']['author']) ? 
$properties['symfony']['author'] : 'Your name here',
+      'PACKAGE_NAME'  => !is_null($this->package) ? $this->package : 'package 
name', 
+    );  
+
+    if (!is_readable($file))
+    {
+      throw new sfCommandException("Failed to replace tokens as file is not 
accessible.");
+    }
+
+    // customize service file
+    $sfFilesystem = new sfFilesystem();
+    $sfFilesystem->replaceTokens($file, '##', '##', $constants);
+
+  }
+
+  /**
+   *
+   */ 
+  protected function setPackage($name)
+  {
+    $this->package = $name;
+  }
+
+  /**
+   *
+   */ 
+  protected function getPackage($dir = false)
+  {
+    if (is_null($this->package))
+      return '';
+
+    return !$dir ? $this->package : str_replace('.', '/', $this->package); 
+
+  }
+
+  /**
+   *
+   */ 
+  protected function setServiceDirname($name)
+  {
+    $this->serviceDirname = $name;
+  }
+
+  /**
+   *
+   */ 
+  protected function getServiceDirname()
+  {
+    return !is_null($this->serviceDirname) ? $this->serviceDirname : 
'services';
+  }
+
+  /**
+   *
+   */
+  protected function getServiceDirectory()
+  {
+    return sfConfig::get('sf_lib_dir') . '/' . 
+                    $this->getServiceDirname() . '/' . 
+                    $this->getPackage(true) . '/';
+  }
+
+  /**
+   *
+   */ 
+  protected function getServiceFileName($serviceName)
+  {
+    return $this->getServiceDirectory() . $serviceName . 'Service.class.php';
+  }
+
+  /**
+   *
+   */ 
+  protected function checkServiceDirectories()
+  {
+    if (!is_dir($this->getServiceDirectory()))
+    {
+      mkdir($this->getServiceDirectory(), 0777, true); 
+    }
+  }
+}


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

Added: plugins/sfAmfPlugin/lib/task/sfAmfPluginBuildServiceTask.class.php
===================================================================
--- plugins/sfAmfPlugin/lib/task/sfAmfPluginBuildServiceTask.class.php          
                (rev 0)
+++ plugins/sfAmfPlugin/lib/task/sfAmfPluginBuildServiceTask.class.php  
2010-03-09 00:36:30 UTC (rev 28422)
@@ -0,0 +1,80 @@
+<?php
+/**
+ * This file is part of the sfAmfPlugin package.
+ * (c) 2008, 2009 Timo Haberkern <[email protected]>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * Task-Class for symfony command-line task for the generation of AMF-Service
+ * class from database
+ *
+ * @author Stephane Bachelier (http://blog.0x89b.org)
+ * @copyright Stephane Bachelier
+ * @license MIT
+ * @version SVN: $Id $
+ */
+
+class sfAmfPluginBuildServiceTask extends sfBaseTask 
+{
+  /**
+   * @see sfTask
+   */
+  protected function configure()
+  {
+    $this->addOptions(array(
+      new sfCommandOption('connection', '-c', 
sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 
sfConfig::get('sf_orm')),
+      new sfCommandOption('package', '-p', 
sfCommandOption::PARAMETER_REQUIRED, 'Package name (i.e. 
org.symfony.services)'),
+      new sfCommandOption('service_dir', '-s', 
sfCommandOption::PARAMETER_REQUIRED, 'The service dir name', 'services'),
+    ));
+
+    $this->namespace        = 'amf';
+    $this->name             = 'build-services';
+    $this->briefDescription = 'Generate services based on schema';
+    $this->detailedDescription = <<<EOF
+The amf:build-services task generates a service class for each table in 
database 
+
+Call it with:
+
+  [php symfony amf:build-services|INFO]
+EOF;
+  }
+
+  /**
+   * @see sfTask
+   */
+  protected function execute($arguments = array(), $options = array()) 
+  {
+    $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'],
+    ));
+
+  }
+
+  /**
+   *
+   */ 
+  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)");  
+    }
+  }
+    
+}
+    


Property changes on: 
plugins/sfAmfPlugin/lib/task/sfAmfPluginBuildServiceTask.class.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