Author: francois
Date: 2010-05-06 18:11:03 +0200 (Thu, 06 May 2010)
New Revision: 29379

Added:
   plugins/sfPropel15Plugin/trunk/lib/routing/sfPropel15Route.class.php
   
plugins/sfPropel15Plugin/trunk/lib/routing/sfPropel15RouteCollection.class.php
Log:
[sfPropel15Plugin] Initial commit for model routes using Propel 1.5 (WIP)

Added: plugins/sfPropel15Plugin/trunk/lib/routing/sfPropel15Route.class.php
===================================================================
--- plugins/sfPropel15Plugin/trunk/lib/routing/sfPropel15Route.class.php        
                        (rev 0)
+++ plugins/sfPropel15Plugin/trunk/lib/routing/sfPropel15Route.class.php        
2010-05-06 16:11:03 UTC (rev 29379)
@@ -0,0 +1,280 @@
+<?php
+
+/*
+ * This file is part of the symfony package.
+ * (c) Fabien Potencier <[email protected]>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * sfObjectRoute represents a route that is bound to PHP object(s).
+ *
+ * An object route can represent a single object or a list of objects.
+ *
+ * @package    symfony
+ * @subpackage routing
+ * @author     Fabien Potencier <[email protected]>
+ * @version    SVN: $Id: sfObjectRoute.class.php 20784 2009-08-04 20:53:57Z 
Kris.Wallsmith $
+ */
+class sfPropel15Route extends sfRequestRoute
+{
+  protected
+    $object  = false,
+    $objects = false;
+
+  /**
+   * Constructor.
+   *
+   * @param string $pattern       The pattern to match
+   * @param array  $defaults      An array of default parameter values
+   * @param array  $requirements  An array of requirements for parameters 
(regexes)
+   * @param array  $options       An array of options
+   *
+   * @see sfRoute
+   */
+  public function __construct($pattern, array $defaults = array(), array 
$requirements = array(), array $options = array())
+  {
+    if (!isset($options['model']))
+    {
+      throw new InvalidArgumentException(sprintf('You must pass a "model" 
option for a %s object (%s).', get_class($this), $pattern));
+    }
+
+    if (!isset($options['type']))
+    {
+      throw new InvalidArgumentException(sprintf('You must pass a "type" 
option for a %s object (%s).', get_class($this), $pattern));
+    }
+
+    if (!in_array($options['type'], array('object', 'list')))
+    {
+      throw new InvalidArgumentException(sprintf('The "type" option can only 
be "object" or "list", "%s" given (%s).', $options['type'], $pattern));
+    }
+
+    parent::__construct($pattern, $defaults, $requirements, $options);
+  }
+
+  /**
+   * Returns true if the parameters matches this route, false otherwise.
+   *
+   * @param  mixed  $params  The parameters
+   * @param  array  $context The context
+   *
+   * @return Boolean         true if the parameters matches this route, false 
otherwise.
+   */
+  public function matchesParameters($params, $context = array())
+  {
+    return parent::matchesParameters('object' == $this->options['type'] ? 
$this->convertObjectToArray($params) : $params);
+  }
+
+  /**
+   * Generates a URL from the given parameters.
+   *
+   * @param  mixed   $params    The parameter values
+   * @param  array   $context   The context
+   * @param  Boolean $absolute  Whether to generate an absolute URL
+   *
+   * @return string The generated URL
+   */
+  public function generate($params, $context = array(), $absolute = false)
+  {
+    return parent::generate('object' == $this->options['type'] ? 
$this->convertObjectToArray($params) : $params, $context, $absolute);
+  }
+
+  /**
+   * Gets the object related to the current route and parameters.
+   *
+   * This method is only accessible if the route is bound and of type "object".
+   *
+   * @return Object The related object
+   */
+  public function getObject()
+  {
+    if (!$this->isBound())
+    {
+      throw new LogicException('The route is not bound.');
+    }
+
+    if ('object' != $this->options['type'])
+    {
+      throw new LogicException(sprintf('The route "%s" is not of type 
"object".', $this->pattern));
+    }
+
+    if (false !== $this->object)
+    {
+      return $this->object;
+    }
+
+    // check the related object
+    $this->object = $this->getQuery()
+      ->filterByArray($this->getModelParameters($this->parameters))
+      ->findOne();
+    if (!$this->object && (!isset($this->options['allow_empty']) || 
!$this->options['allow_empty']))
+    {
+      throw new sfError404Exception(sprintf('Unable to find the %s object with 
the following parameters "%s").', $this->options['model'], str_replace("\n", 
'', var_export($this->filterParameters($this->parameters), true))));
+    }
+
+    return $this->object;
+  }
+
+  /**
+   * Gets the list of objects related to the current route and parameters.
+   *
+   * This method is only accessible if the route is bound and of type "list".
+   *
+   * @return array And array of related objects
+   */
+  public function getObjects()
+  {
+    if (!$this->isBound())
+    {
+      throw new LogicException('The route is not bound.');
+    }
+
+    if ('list' != $this->options['type'])
+    {
+      throw new LogicException(sprintf('The route "%s" is not of type 
"list".', $this->pattern));
+    }
+
+    if (false !== $this->objects)
+    {
+      return $this->objects;
+    }
+
+               $this->object = $this->getQuery()
+                 ->filterByArray($this->getModelParameters($this->parameters))
+                 ->find();
+
+    if (!count($this->objects) && isset($this->options['allow_empty']) && 
!$this->options['allow_empty'])
+    {
+      throw new sfError404Exception(sprintf('No %s object found for the 
following parameters "%s").', $this->options['model'], str_replace("\n", '', 
var_export($this->filterParameters($this->parameters), true))));
+    }
+
+    return $this->objects;
+  }
+       
+  protected function getQuery()
+  {
+       return PropelQuery::from($this->options['model']);
+  }
+  
+  protected function getModelParameters($parameters)
+  {
+       if (!is_array($parameters))
+    {
+      return $parameters;
+    }
+    $parameters = $this->filterParameters($parameters);
+               $peerName = constant($this->options['model'] . '::PEER');
+    $params = array();
+    foreach ($this->getRealVariables() as $variable)
+    {
+       try
+      {
+        $column = call_user_func(array($peerName, 'translateFieldName'), 
$variable, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_PHPNAME);
+      }
+      catch (Exception $e)
+      {
+        $column = sfInflector::camelize($variable);
+      }
+      $params[$column] = $parameters[$variable];
+    }
+
+    return $params;
+  }
+
+  protected function filterParameters($parameters)
+  {
+       if (!is_array($parameters))
+    {
+      return $parameters;
+    }
+
+    $params = array();
+    foreach (array_keys($this->variables) as $variable)
+    {
+      $params[$variable] = $parameters[$variable];
+    }
+
+    return $params;
+  }
+
+  protected function convertObjectToArray($object)
+  {
+    if (!$this->compiled)
+    {
+      $this->compile();
+    }
+
+    if (is_array($object))
+    {
+      if (!isset($object['sf_subject']))
+      {
+        return $object;
+      }
+
+      $parameters = $object;
+      $object = $parameters['sf_subject'];
+      unset($parameters['sf_subject']);
+    }
+    else
+    {
+      $parameters = array();
+    }
+
+    return array_merge($parameters, $this->doConvertObjectToArray($object));
+  }
+
+  protected function doConvertObjectToArray($object)
+  {
+    if (isset($this->options['convert']) || method_exists($object, 'toParams'))
+    {
+      return parent::doConvertObjectToArray($object);
+    }
+
+    $peerName = constant($this->options['model'] . '::PEER');
+
+    $parameters = array();
+    foreach ($this->getRealVariables() as $variable)
+    {
+      try
+      {
+        $method = 'get'.call_user_func(array($peerName, 'translateFieldName'), 
$variable, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_PHPNAME);
+      }
+      catch (Exception $e)
+      {
+        $method = 'get'.sfInflector::camelize($variable);
+      }
+
+      $parameters[$variable] = $object->$method();
+    }
+
+    return $parameters;
+  }
+
+  protected function getRealVariables()
+  {
+    $variables = array();
+
+    foreach (array_keys($this->variables) as $variable)
+    {
+      if (0 === strpos($variable, 'sf_') || in_array($variable, 
array('module', 'action')))
+      {
+        continue;
+      }
+
+      $variables[] = $variable;
+    }
+
+    return $variables;
+  }
+  
+  public function __call($name, $arguments)
+  {
+    if($name == 'get' . $this->options['model'])
+    {
+      return call_user_func_array(array($this, 'getObject'), $arguments);
+    }
+    throw new Exception(sprintf('Call to undefined method 
sfPropel15Route::%s.', $name));
+  }
+}


Property changes on: 
plugins/sfPropel15Plugin/trunk/lib/routing/sfPropel15Route.class.php
___________________________________________________________________
Added: svn:executable
   + *
Added: svn:keywords
   + "Id Rev Revision Author"
Added: svn:eol-style
   + native

Added: 
plugins/sfPropel15Plugin/trunk/lib/routing/sfPropel15RouteCollection.class.php
===================================================================
--- 
plugins/sfPropel15Plugin/trunk/lib/routing/sfPropel15RouteCollection.class.php  
                            (rev 0)
+++ 
plugins/sfPropel15Plugin/trunk/lib/routing/sfPropel15RouteCollection.class.php  
    2010-05-06 16:11:03 UTC (rev 29379)
@@ -0,0 +1,23 @@
+<?php
+
+/*
+ * This file is part of the symfony package.
+ * (c) Fabien Potencier <[email protected]>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * sfPropelRouteCollection represents a collection of routes bound to Propel 
objects.
+ *
+ * @package    symfony
+ * @subpackage routing
+ * @author     Fabien Potencier <[email protected]>
+ * @version    SVN: $Id: sfPropelRouteCollection.class.php 12755 2008-11-08 
09:48:03Z fabien $
+ */
+class sfPropel15RouteCollection extends sfObjectRouteCollection
+{
+  protected
+    $routeClass = 'sfPropel15Route';
+}


Property changes on: 
plugins/sfPropel15Plugin/trunk/lib/routing/sfPropel15RouteCollection.class.php
___________________________________________________________________
Added: svn:executable
   + *
Added: svn:keywords
   + "Id Rev Revision Author"
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