Development

Changeset 13270

You must first sign up to be able to contribute.

Changeset 13270

Show
Ignore:
Timestamp:
11/23/08 03:43:11 (1 year ago)
Author:
Jonathan.Wage
Message:

[1.2] syncing sfDoctrinePlugin with sfPropelPlugin - routing, admin generator, build forms tasks (closes #5029)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfDoctrinePlugin/trunk/lib/record/sfDoctrineRecord.class.php

    r13217 r13270  
    111111 
    112112  /** 
    113    * Function required by symfony >= 1.2 admin generators 
    114    * 
    115    * @return array 
    116    */ 
    117   public function toParams() 
    118   { 
    119     return $this->identifier(); 
    120   } 
    121  
    122   /** 
    123113   * Function require by symfony >= 1.2 admin generators 
    124114   * 
  • plugins/sfDoctrinePlugin/trunk/lib/routing/sfDoctrineRoute.class.php

    r12670 r13270  
    2323class sfDoctrineRoute extends sfObjectRoute 
    2424{ 
     25  protected 
     26    $query = null; 
     27 
    2528  /** 
    2629   * Constructor. 
     
    3841 
    3942    $this->options['object_model'] = $this->options['model']; 
     43  } 
     44 
     45  public function setListCriteria(Doctrine_Query $query) 
     46  { 
     47    if (!$this->isBound()) 
     48    { 
     49      throw new LogicException('The route is not bound.'); 
     50    } 
     51 
     52    $this->query = $query; 
    4053  } 
    4154 
     
    7588    return parent::getObjectsForParameters($parameters); 
    7689  } 
     90 
     91  protected function doConvertObjectToArray($object) 
     92  { 
     93    if (isset($this->options['convert']) || method_exists($object, 'toParams')) 
     94    { 
     95      return parent::doConvertObjectToArray($object); 
     96    } 
     97 
     98    $className = $this->options['model']; 
     99 
     100    $parameters = array(); 
     101 
     102    foreach ($this->getRealVariables() as $variable) 
     103    { 
     104      $parameters[$variable] = $object->get($variable); 
     105    } 
     106 
     107    return $parameters; 
     108  } 
    77109} 
  • plugins/sfDoctrinePlugin/trunk/lib/task/sfDoctrineBuildFiltersTask.class.php

    r12649 r13270  
    1717 * @subpackage doctrine 
    1818 * @author     Fabien Potencier <fabien.potencier@symfony-project.com> 
    19  * @version    SVN: $Id: sfDoctrineBuildFiltersTask.class.php 11675 2008-09-19 15:21:38Z fabien $ 
     19 * @version    SVN: $Id: sfDoctrineBuildFiltersTask.class.php 12537 2008-11-01 14:43:27Z fabien $ 
    2020 */ 
    2121class sfDoctrineBuildFiltersTask extends sfDoctrineBaseTask 
     
    6363  { 
    6464    $this->logSection('doctrine', 'generating filter form classes'); 
    65  
    66     $databaseManager = new sfDatabaseManager($this->configuration); 
     65     
    6766    $generatorManager = new sfGeneratorManager($this->configuration); 
    6867    $generatorManager->generate('sfDoctrineFormFilterGenerator', array( 
     
    7170      'filter_dir_name'  => $options['filter-dir-name'], 
    7271    )); 
     72 
     73    $properties = parse_ini_file(sfConfig::get('sf_config_dir').DIRECTORY_SEPARATOR.'properties.ini', true); 
     74 
     75    $constants = array( 
     76      'PROJECT_NAME' => isset($properties['symfony']['name']) ? $properties['symfony']['name'] : 'symfony', 
     77      'AUTHOR_NAME'  => isset($properties['symfony']['author']) ? $properties['symfony']['author'] : 'Your name here' 
     78    ); 
     79 
     80    // customize php and yml files 
     81    $finder = sfFinder::type('file')->name('*.php'); 
     82    $this->getFilesystem()->replaceTokens($finder->in(sfConfig::get('sf_lib_dir').'/filter/'), '##', '##', $constants); 
    7383  } 
    7484} 
  • plugins/sfDoctrinePlugin/trunk/lib/task/sfDoctrineBuildFormsTask.class.php

    r12649 r13270  
    33/* 
    44 * This file is part of the symfony package. 
    5  * (c) Fabien Potencier <fabien.potencier@symfony-project.com> 
    6  * (c) Jonathan H. Wage <jonwage@gmail.com> 
     5 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> 
    76 * 
    87 * For the full copyright and license information, please view the LICENSE 
     
    1817 * @subpackage doctrine 
    1918 * @author     Fabien Potencier <fabien.potencier@symfony-project.com> 
    20  * @author     Jonathan H. Wage <jonwage@gmail.com> 
    21  * @version    SVN: $Id: sfDoctrineBuildFormsTask.class.php 8512 2008-04-17 18:06:12Z fabien $ 
     19 * @version    SVN: $Id: sfDoctrineBuildFormsTask.class.php 12537 2008-11-01 14:43:27Z fabien $ 
    2220 */ 
    2321class sfDoctrineBuildFormsTask extends sfDoctrineBaseTask 
     
    3533    )); 
    3634 
    37     $this->aliases = array('doctrine-build-forms'); 
    3835    $this->namespace = 'doctrine'; 
    3936    $this->name = 'build-forms'; 
     
    4542  [./symfony doctrine:build-forms|INFO] 
    4643 
    47 The task read the schema information in [config/doctrine/*.yml|COMMENT] from  
    48 the project and all installed plugins. 
     44The task read the schema information in [config/*schema.xml|COMMENT] and/or 
     45[config/*schema.yml|COMMENT] from the project and all installed plugins. 
    4946 
    5047The task use the [doctrine|COMMENT] connection as defined in [config/databases.yml|COMMENT]. 
    5148You can use another connection by using the [--connection|COMMENT] option: 
    5249 
    53   [./symfony doctrine:build-forms frontend --connection="name"|INFO] 
     50  [./symfony doctrine:build-forms --connection="name"|INFO] 
    5451 
    55 The model form classes files are created in [lib/form/doctrine|COMMENT]. 
     52The model form classes files are created in [lib/form|COMMENT]. 
    5653 
    57 This task never overrides custom classes in [lib/form/doctrine|COMMENT]. 
    58 It only replaces base classes generated in [lib/form/doctrine/base|COMMENT]. 
     54This task never overrides custom classes in [lib/form|COMMENT]. 
     55It only replaces base classes generated in [lib/form/base|COMMENT]. 
    5956EOF; 
    6057  } 
     
    6764    $this->logSection('doctrine', 'generating form classes'); 
    6865 
    69     $databaseManager = new sfDatabaseManager($this->configuration); 
    70  
    7166    $generatorManager = new sfGeneratorManager($this->configuration); 
    72  
    7367    $generatorManager->generate('sfDoctrineFormGenerator', array( 
    7468      'connection'     => $options['connection'], 
     
    7670      'form_dir_name'  => $options['form-dir-name'], 
    7771    )); 
     72 
     73    $properties = parse_ini_file(sfConfig::get('sf_config_dir').DIRECTORY_SEPARATOR.'properties.ini', true); 
     74 
     75    $constants = array( 
     76      'PROJECT_NAME' => isset($properties['symfony']['name']) ? $properties['symfony']['name'] : 'symfony', 
     77      'AUTHOR_NAME'  => isset($properties['symfony']['author']) ? $properties['symfony']['author'] : 'Your name here' 
     78    ); 
     79 
     80    // customize php and yml files 
     81    $finder = sfFinder::type('file')->name('*.php'); 
     82    $this->getFilesystem()->replaceTokens($finder->in(sfConfig::get('sf_lib_dir').'/form/'), '##', '##', $constants); 
    7883  } 
    7984} 
  • plugins/sfDoctrinePlugin/trunk/lib/task/sfDoctrineGenerateAdminTask.class.php

    r12634 r13270  
    2828    $this->addArguments(array( 
    2929      new sfCommandArgument('application', sfCommandArgument::REQUIRED, 'The application name'), 
    30       new sfCommandArgument('route', sfCommandArgument::REQUIRED, 'The route name'), 
     30      new sfCommandArgument('route_or_model', sfCommandArgument::REQUIRED, 'The route name or the model class'), 
    3131    )); 
    3232 
    3333    $this->addOptions(array( 
     34      new sfCommandOption('module', null, sfCommandOption::PARAMETER_REQUIRED, 'The module name', null), 
    3435      new sfCommandOption('theme', null, sfCommandOption::PARAMETER_REQUIRED, 'The theme name', 'admin'), 
    3536      new sfCommandOption('singular', null, sfCommandOption::PARAMETER_REQUIRED, 'The singular name', null), 
     
    4546The [doctrine:generate-admin|INFO] task generates a Doctrine admin module: 
    4647 
     48  [./symfony doctrine:generate-admin frontend Article|INFO] 
     49 
     50The task creates a module in the [%frontend%|COMMENT] application for the 
     51[%Article%|COMMENT] model. 
     52 
     53The task creates a route for you in the application [routing.yml|COMMENT]. 
     54 
     55You can also generate a Doctrine admin module by passing a route name: 
     56 
    4757  [./symfony doctrine:generate-admin frontend article|INFO] 
    4858 
     
    5060[%article%|COMMENT] route definition found in [routing.yml|COMMENT]. 
    5161 
    52 For the filters to work properly, you need to add a collection route for it: 
     62For the filters and batch actions to work properly, you need to add 
     63collection routes for them: 
    5364 
    54   articles
     65  article
    5566    class: sfDoctrineRouteCollection 
    5667    options: 
    5768      model:              Article 
    58       module:             article 
    59       collection_actions: { filter: post } 
    60  
     69      collection_actions: { filter: post, batch: post } 
    6170EOF; 
    6271  } 
     
    6877  { 
    6978    // get configuration for the given route 
    70     $config = new sfRoutingConfigHandler(); 
    71     $routes = $config->evaluate($this->configuration->getConfigPaths('config/routing.yml')); 
     79    if (false !== ($route = $this->getRouteFromName($arguments['route_or_model']))) 
     80    { 
     81      $arguments['route'] = $route; 
     82      $arguments['route_name'] = $arguments['route_or_model']; 
    7283 
    73     if (!isset($routes[$arguments['route']])) 
    74     { 
    75       throw new sfCommandException(sprintf('The route "%s" does not exist.', $arguments['route'])); 
     84      return $this->generateForRoute($arguments, $options); 
    7685    } 
    7786 
    78     $routeOptions = $routes[$arguments['route']]->getOptions(); 
     87    // is it a model class name 
     88    if (!class_exists($arguments['route_or_model'])) 
     89    { 
     90      throw new sfCommandException(sprintf('The route "%s" does not exist and there is no "%s" class.', $arguments['route_or_model'], $arguments['route_or_model'])); 
     91    } 
    7992 
    80     if (!$routes[$arguments['route']] instanceof sfDoctrineRouteCollection) 
     93    $r = new ReflectionClass($arguments['route_or_model']); 
     94    if (!$r->isSubclassOf('Doctrine_Record')) 
    8195    { 
    82       throw new sfCommandException(sprintf('The route "%s" is not a Doctrine collection route.', $arguments['route'])); 
     96      throw new sfCommandException(sprintf('"%s" is not a Doctrine class.', $arguments['route_or_model'])); 
     97    } 
     98 
     99    // create a route 
     100    $model = $arguments['route_or_model']; 
     101    $name = strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), '\\1_\\2', $model)); 
     102 
     103    $routing = sfConfig::get('sf_app_config_dir').'/routing.yml'; 
     104    $content = file_get_contents($routing); 
     105    $routesArray = sfYaml::load($content); 
     106 
     107    if (!isset($routesArray[$name])) 
     108    { 
     109      $module = $options['module'] ? $options['module'] : $name; 
     110      $content = sprintf(<<<EOF 
     111%s: 
     112  class: sfDoctrineRouteCollection 
     113  options: 
     114    model:               %s 
     115    module:              %s 
     116    prefix_path:         %s 
     117    with_wilcard_routes: true 
     118 
     119 
     120EOF 
     121      , $name, $model, $module, $module).$content; 
     122 
     123      file_put_contents($routing, $content); 
     124    } 
     125 
     126    $arguments['route'] = $this->getRouteFromName($name); 
     127    $arguments['route_name'] = $name; 
     128 
     129    return $this->generateForRoute($arguments, $options); 
     130  } 
     131 
     132  protected function generateForRoute($arguments, $options) 
     133  { 
     134    $routeOptions = $arguments['route']->getOptions(); 
     135 
     136    if (!$arguments['route'] instanceof sfDoctrineRouteCollection) 
     137    { 
     138      throw new sfCommandException(sprintf('The route "%s" is not a Doctrine collection route.', $arguments['route_name'])); 
    83139    } 
    84140 
     
    113169    return $task->run(array($arguments['application'], $module, $model), $taskOptions); 
    114170  } 
     171 
     172  protected function getRouteFromName($name) 
     173  { 
     174    $config = new sfRoutingConfigHandler(); 
     175    $routes = $config->evaluate($this->configuration->getConfigPaths('config/routing.yml')); 
     176 
     177    if (isset($routes[$name])) 
     178    { 
     179      return $routes[$name]; 
     180    } 
     181 
     182    return false; 
     183  } 
    115184} 

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting.
Sensio Labs also supports several large Open-Source projects.