Author: stefbach
Date: 2010-03-10 00:33:58 +0100 (Wed, 10 Mar 2010)
New Revision: 28443
Added:
plugins/sfAmfPlugin/data/generator/sfAmfPropelService/default/template/sfAmfBaseServiceTemplate.php
plugins/sfAmfPlugin/lib/task/generator/doctrine/sfAmfPluginDoctrineBaseServiceGenerator.class.php
plugins/sfAmfPlugin/lib/task/generator/propel/sfAmfPluginPropelBaseServiceGenerator.class.php
plugins/sfAmfPlugin/lib/task/sfAmfPluginBuildBaseServiceTask.class.php
plugins/sfAmfPlugin/lib/task/skeleton/Service.php
Modified:
plugins/sfAmfPlugin/
Log:
[sfAmfPlugin] Added base services generator (as Propel model classes)
Property changes on: plugins/sfAmfPlugin
___________________________________________________________________
Modified: svk:merge
- 7ce51a29-67ea-4596-9a32-2e0fcaf8b5e0:/local/symfony/sfAmfPlugin:39
+ 7ce51a29-67ea-4596-9a32-2e0fcaf8b5e0:/local/symfony/sfAmfPlugin:41
Added:
plugins/sfAmfPlugin/data/generator/sfAmfPropelService/default/template/sfAmfBaseServiceTemplate.php
===================================================================
---
plugins/sfAmfPlugin/data/generator/sfAmfPropelService/default/template/sfAmfBaseServiceTemplate.php
(rev 0)
+++
plugins/sfAmfPlugin/data/generator/sfAmfPropelService/default/template/sfAmfBaseServiceTemplate.php
2010-03-09 23:33:58 UTC (rev 28443)
@@ -0,0 +1,161 @@
+[?php
+
+/**
+ * <?php echo $this->table->getClassname() ?> service base class.
+ *
+ * @package ##PROJECT_NAME##
+ * @author ##AUTHOR_NAME##
+ */
+abstract class Base<?php echo $this->table->getClassname() ?>Service extends
sfAmfService implements Service
+{
+ /**
+ * create a record in database from ValueObject
+ *
+ * @param ValueObject Remote object
+ * @return boolean True if creation succeeds
+ */
+ public function create($valueObject)
+ {
+ $baseObject = $this->fromValueObject($valueObject);
+ try
+ {
+ $baseObject->save();
+ return true;
+ }
+ catch(PropelException $e)
+ {
+ return false;
+ }
+ }
+
+ /**
+ * create a record in database from ValueObject
+ *
+ * @param int Limit of ValueObject to return, 0 being no limit (default: 0)
+ * @return Array of ValueObject
+ * @AmfClassMapping(name="com.coqsenpate.www.vo.<?php echo
$this->table->getClassname(); ?>ValueObject")
+ * @AmfReturnType("ArrayCollection")
+ */
+ public function retrieveAll($limit = 0)
+ {
+ $valueObjects = array();
+ $baseObjects = <?php echo $this->table->getClassname()
?>Peer::doSelect(new Criteria());
+
+ foreach($baseObjects as $baseObject)
+ {
+ $valueObjects[] = $this->toValueObject($baseObject);
+ }
+
+ return $valueObjects;
+ }
+
+ /**
+ * get a record from database
+ *
+ * @param int Object id
+ * @return ValueObject Object from atabase record
+ * @AmfClassMapping(name="com.coqsenpate.www.vo.<?php echo
$this->table->getClassname(); ?>ValueObject")
+ */
+ public function retrieveById($id)
+ {
+ return $this->toValueObject(<?php echo $this->table->getClassname()
?>Peer::retrieveByPK($id));
+ }
+
+ /**
+ * update a record in database from ValueObject
+ *
+ * @param ValueObject Remote object
+ * @return boolean True if update succeeds
+ */
+ public function update($valueObject)
+ {
+ return $this->create($valueObject);
+ }
+
+ /**
+ * delete a record in database from ValueObject
+ *
+ * @param ValueObject Remote object
+ * @return boolean True if delete succeeds
+ */
+ public function delete($valueObject)
+ {
+ $this->fromValueObject($valueObject)->delete();
+ }
+
+ /**
+ * get initialized BaseObject from ValueObject
+ *
+ * @param ValueObject Remote object
+ * @return BaseObject The model object
+ */
+ public function fromValueObject($valueObject)
+ {
+ $baseObject = new <?php echo $this->table->getClassname() ?>();
+
+<?php
+// set baseObject fields via setter based on valueObject fields
+// PK is ignored as it need a custom action.
+$pkColumn = '';
+$columns = $this->table->getColumns();
+
+foreach ($columns as $column)
+{
+ if ($column->isPrimaryKey())
+ {
+ $pkColumn = $column;
+ }
+ else
+ {
+ $columnPhpName = $column->getPhpName();
+ $valueObjectProp = '$valueObject->' . lcfirst($columnPhpName);
+echo ' if (' . $valueObjectProp . ' != null) $baseObject->set' .
$columnPhpName . '(' . $valueObjectProp . ");\n";
+ }
+}
+
+// if a PK column exists
+if ($pkColumn != '')
+{
+?>
+ if ($valueObject-><?php echo lcfirst($pkColumn->getPhpName()) ?> == -1)
+ {
+ $baseObject->setNew(true);
+ }
+ else
+ {
+ $baseObject->setNew(false);
+ $baseObject->set<?php echo $pkColumn->getPhpName() ?>($valueObject->id);
+ }
+<?php
+}
+?>
+
+ return $baseObject;
+ }
+
+ /**
+ * get ValueObject from (Propel) BaseObject
+ *
+ * @param BaseObject The model object
+ * @return ValueObject The object to be used remotely
+ */
+ public function toValueObject($baseObject)
+ {
+ $valueObject = new <?php echo $this->table->getClassname() ?>ValueObject();
+
+ if ($baseObject != null)
+ {
+<?php
+$columns = $this->table->getColumns();
+
+foreach ($columns as $column)
+{
+$columnPhpName = $column->getPhpName();
+echo ' $valueObject->' . lcfirst($columnPhpName) . ' = $baseObject->get'
. $columnPhpName . "();\n";
+}
+?>
+ }
+
+ return $valueObject;
+ }
+}
Property changes on:
plugins/sfAmfPlugin/data/generator/sfAmfPropelService/default/template/sfAmfBaseServiceTemplate.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/sfAmfPluginDoctrineBaseServiceGenerator.class.php
===================================================================
---
plugins/sfAmfPlugin/lib/task/generator/doctrine/sfAmfPluginDoctrineBaseServiceGenerator.class.php
(rev 0)
+++
plugins/sfAmfPlugin/lib/task/generator/doctrine/sfAmfPluginDoctrineBaseServiceGenerator.class.php
2010-03-09 23:33:58 UTC (rev 28443)
@@ -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 sfAmfPluginDoctrineBaseServiceGenerator 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/sfAmfPluginDoctrineBaseServiceGenerator.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/sfAmfPluginPropelBaseServiceGenerator.class.php
===================================================================
---
plugins/sfAmfPlugin/lib/task/generator/propel/sfAmfPluginPropelBaseServiceGenerator.class.php
(rev 0)
+++
plugins/sfAmfPlugin/lib/task/generator/propel/sfAmfPluginPropelBaseServiceGenerator.class.php
2010-03-09 23:33:58 UTC (rev 28443)
@@ -0,0 +1,79 @@
+<?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 sfAmfPluginPropelBaseServiceGenerator extends sfAmfPluginPropelGenerator
+{
+ protected $basePackage = null;
+
+ public function initialize(sfGeneratorManager $generatorManager)
+ {
+ parent::initialize($generatorManager);
+ }
+
+ public function generate($params = array())
+ {
+ // retrieve base_package
+ if (!array_key_exists('base_package', $params))
+ {
+ if (array_key_exists('package', $params))
+ $this->basePackage = $params['package'];
+ }
+ else
+ {
+ $this->basePackage = $params['base_package'];
+ }
+
+ parent::generate($params);
+ }
+
+ protected function generateAll()
+ {
+ // generate files for each table
+ foreach ($this->dbMap->getTables() as $tableName => $table)
+ {
+ $this->table = $table;
+ $tableClassname = $table->getClassname();
+
+ $serviceFileName = $tableClassname . 'Service';
+ $baseServiceFileName = 'Base' . $serviceFileName;
+
+ // base services
+ $baseServiceFile = $this->getAbsoluteFileName($baseServiceFileName,
$this->basePackage);
+
+ $this->writeFile($baseServiceFile,
+ $this->evalTemplate('sfAmfBaseServiceTemplate.php'));
+
+ // services
+ $serviceFile = $this->getAbsoluteFileName($serviceFileName);
+
+ if (file_exists($serviceFile))
+ {
+ throw new sfCommandException(
+ sprintf("Services already exist in %s directory. You must remove
them manually",
+ $this->getServiceDirname()
+ ));
+ }
+
+ $this->serviceParent = $baseServiceFileName;
+
+ $this->writeFile($serviceFile,
+ $this->evalTemplate('sfAmfServiceTemplate.php'));
+ }
+ }
+
+}
Property changes on:
plugins/sfAmfPlugin/lib/task/generator/propel/sfAmfPluginPropelBaseServiceGenerator.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/sfAmfPluginBuildBaseServiceTask.class.php
===================================================================
--- plugins/sfAmfPlugin/lib/task/sfAmfPluginBuildBaseServiceTask.class.php
(rev 0)
+++ plugins/sfAmfPlugin/lib/task/sfAmfPluginBuildBaseServiceTask.class.php
2010-03-09 23:33:58 UTC (rev 28443)
@@ -0,0 +1,79 @@
+<?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
+ * base class from database
+ *
+ * @author Stephane Bachelier (http://blog.0x89b.org)
+ * @copyright Stephane Bachelier
+ * @license MIT
+ * @version SVN: $Id $
+ */
+
+class sfAmfPluginBuildBaseServiceTask extends sfAmfPluginGeneratorTask
+{
+ /**
+ * @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'),
+ new sfCommandOption('base_package', '-b',
sfCommandOption::PARAMETER_REQUIRED, 'Package name for based classes (i.e.
org.symfony.service.base)'),
+ ));
+
+ $this->namespace = 'amf';
+ $this->name = 'build-base-services';
+ $this->briefDescription = 'Generate base services based on schema';
+ $this->detailedDescription = <<<EOF
+The amf:build-base-services task generates a service class for each table in
database
+
+Call it with:
+
+ [php symfony amf:build-base-services|INFO]
+EOF;
+ }
+
+ /**
+ * @see sfTask
+ */
+ protected function execute($arguments = array(), $options = array())
+ {
+ // copy Service to service_dir
+ $this->logSection($this->namespace, 'copy base Service class to ' .
$options['service_dir']);
+
+ if (!interface_exists('Service'))
+ {
+ print 'unknown interface Service';
+ }
+
+ $skeletonDir = dirname(__FILE__). '/skeleton';
+ $serviceFile = 'Service.php';
+
+ $this->getFilesystem()->copy($skeletonDir . '/' . $serviceFile,
+ sfConfig::get('sf_lib_dir') . '/' . $options['service_dir'] . '/' .
$serviceFile);
+
+ // generate base services
+ $this->generate(
+ 'BaseService',
+ $options['connection'],
+ array(
+ 'connection' => $options['connection'],
+ 'service_dir' => $options['service_dir'],
+ 'package' => $options['package'],
+ 'base_package' => $options['base_package'],
+ ));
+
+ }
+
+}
+
Property changes on:
plugins/sfAmfPlugin/lib/task/sfAmfPluginBuildBaseServiceTask.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/skeleton/Service.php
===================================================================
--- plugins/sfAmfPlugin/lib/task/skeleton/Service.php
(rev 0)
+++ plugins/sfAmfPlugin/lib/task/skeleton/Service.php 2010-03-09 23:33:58 UTC
(rev 28443)
@@ -0,0 +1,67 @@
+<?php
+
+/**
+ * base service interface
+ *
+ * @package ##PROJECT_NAME##
+ * @author ##AUTHOR_NAME##
+ */
+interface Service
+{
+ /**
+ * create a record in database from ValueObject
+ *
+ * @param ValueObject Remote object
+ * @return boolean True if creation succeeds
+ */
+ public function create($valueObject);
+
+ /**
+ * create a record in database from ValueObject
+ *
+ * @param int Limit of ValueObject to return, 0 being no limit (default: 0)
+ * @return Array of ValueObject
+ */
+ public function retrieveAll($limit = 0);
+
+ /**
+ * get a record from database
+ *
+ * @param int Object id
+ * @return ValueObject Object from atabase record
+ */
+ public function retrieveById($id);
+
+ /**
+ * update a record in database from ValueObject
+ *
+ * @param ValueObject Remote object
+ * @return boolean True if update succeeds
+ */
+ public function update($valueObject);
+
+ /**
+ * delete a record in database from ValueObject
+ *
+ * @param ValueObject Remote object
+ * @return boolean True if delete succeeds
+ */
+ public function delete($valueObject);
+
+ /**
+ * get initialized BaseObject from ValueObject
+ *
+ * @param ValueObject Remote object
+ * @return BaseObject The model object
+ */
+ public function fromValueObject($valueObject);
+
+ /**
+ * get ValueObject from (Propel) BaseObject
+ *
+ * @param BaseObject The model object
+ * @return ValueObject The object to be used remotely
+ */
+ public function toValueObject($baseObject);
+
+}
Property changes on: plugins/sfAmfPlugin/lib/task/skeleton/Service.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.