Development

Changeset 13745

You must first sign up to be able to contribute.

Changeset 13745

Show
Ignore:
Timestamp:
12/04/08 22:40:13 (5 years ago)
Author:
Kris.Wallsmith
Message:

sfViewableModelPlugin: Added automatic, smart extension of model objects.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfViewableModelPlugin/trunk/lib/addon/sfViewableModelPropelBehavior.class.php

    r13744 r13745  
    1111class sfViewableModelPropelBehavior 
    1212{ 
     13  /** 
     14   * Extends a model with this behavior. 
     15   *  
     16   * @param string $model 
     17   */ 
     18  static function extendModel($model) 
     19  { 
     20    if (!is_subclass_of($model, 'BaseObject')) 
     21    { 
     22      throw new InvalidArgumentException(sprintf('The class "%s" is not a subclass of BaseObject.', $model)); 
     23    } 
     24 
     25    try 
     26    { 
     27      sfPropelBehavior::add($model, array('viewable')); 
     28    } 
     29    catch (Exception $e) 
     30    { 
     31      // behavior has already been added 
     32    } 
     33  } 
     34 
    1335  /** 
    1436   * Removes the supplied object from the cache. 
  • plugins/sfViewableModelPlugin/trunk/lib/util/sfViewableModelToolkit.class.php

    r13744 r13745  
    8383      { 
    8484        case 'propel': 
    85           return $value instanceof BaseObject; 
     85          $ret = $value instanceof BaseObject; 
     86          break; 
    8687        case 'doctrine': 
    87           return $value instanceof Doctrine_Record; 
     88          $ret = $value instanceof Doctrine_Record; 
     89          break; 
    8890        default: 
    8991          throw new LogicException('ORM is neither Propel nor Doctrine. Please connect to "sf_viewable_model_plugin.is_model_object" and add logic for your ORM.'); 
     
    9193    } 
    9294 
    93     return false; 
     95    if ($ret) 
     96    { 
     97      self::extendModel($value); 
     98    } 
     99 
     100    return $ret; 
     101  } 
     102 
     103  /** 
     104   * Extends the supplied model with an ORM behavior. 
     105   *  
     106   * @param string|object $model 
     107   */ 
     108  static public function extendModel($model) 
     109  { 
     110    if (is_object($model)) 
     111    { 
     112      $model = get_class($model); 
     113    } 
     114 
     115    $event = sfProjectConfiguration::getActive()->getEventDispatcher()->notifyUntil(new sfEvent($model, 'sf_viewable_model_plugin.extend_model')); 
     116    if ($event->isProcessed()) 
     117    { 
     118      return; 
     119    } 
     120 
     121    switch (sfConfig::get('sf_orm')) 
     122    { 
     123      case 'propel': 
     124        sfViewableModelPropelBehavior::extendModel($model); 
     125        break; 
     126      case 'doctrine': 
     127        throw new Exception('Doctrine version not implemented.'); 
     128        break; 
     129      default: 
     130        throw new LogicException('ORM is neither Propel nor Doctrine. Please connect to "sf_viewable_model_plugin.extend_model" and add logic for your ORM.'); 
     131    } 
    94132  } 
    95133} 
  • plugins/sfViewableModelPlugin/trunk/lib/view/sfViewableModelViewCacheManager.class.php

    r13744 r13745  
    3030    { 
    3131      $this->viewableModelCache = include $this->viewableModelCacheFile; 
     32    } 
     33 
     34    // extend cached model classes 
     35    foreach (array_keys($this->viewableModelCache) as $key) 
     36    { 
     37      $parts = explode('//', $key); 
     38      sfViewableModelToolkit::extendModel($parts[0]); 
    3239    } 
    3340 
     
    139146    } 
    140147 
    141     $key = get_class($model).'_'.$pk; 
     148    $key = get_class($model).'//'.$pk; 
    142149 
    143150    return $key;