Development

Changeset 22976

You must first sign up to be able to contribute.

Changeset 22976

Show
Ignore:
Timestamp:
10/12/09 23:56:20 (4 years ago)
Author:
rande
Message:

[swOptimizeRoutesPlugin] complete Apache REST support, clean up some code, add option to prefix url and path

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/swOptimizeRoutesPlugin/trunk/lib/output/swApacheOutputHandler.class.php

    r22847 r22976  
    2626  { 
    2727 
    28     $output = ""; 
     28    $output = array( 
     29      'HEAD' => array(), 
     30      'PUT' => array(), 
     31      'DELETE' => array(), 
     32      'POST' => array(), 
     33      'GET'  => array(), 
     34      'OTHER' => array(), 
     35    ); 
     36     
    2937    foreach($this->routing->getRoutes() as $name => $route) 
    3038    { 
     
    7987      } 
    8088 
     89      $methods = $this->getMethods($requirements['sf_method']); 
     90 
    8191      $url = $this->fixUrl($route, $url, $sf_format); 
    8292      $condition .= $this->renderCondition($name, $url, $sf_format); 
    8393 
    84       $output .= $condition."\n"; 
     94      foreach($methods as $method) 
     95      { 
     96 
     97        $output[$method][] = $condition; 
     98      } 
     99    } 
     100 
     101    $final_output = ""; 
     102     
     103    if( count($output['OTHER']) > 0) 
     104    { 
     105      $final_output .= implode("\n", $output['OTHER']); 
     106      $final_output .= "\n\n"; 
     107    } 
     108 
     109    if( count($output['PUT']) > 0) 
     110    { 
     111      $final_output .= 'RewriteCond %{REQUEST_METHOD} "GET"'; 
     112      $final_output .= "\n"; 
     113      $final_output .= implode("\n", $output['GET']); 
     114 
     115      $final_output .= "\n\n"; 
    85116    } 
    86117     
    87     return $output; 
     118    if( count($output['HEAD']) > 0) 
     119    { 
     120      $final_output .= 'RewriteCond %{REQUEST_METHOD} "HEAD"'; 
     121      $final_output .= "\n"; 
     122      $final_output .= implode("\n", $output['HEAD']); 
     123 
     124      $final_output .= "\n\n"; 
     125    } 
     126     
     127    if( count($output['DELETE']) > 0) 
     128    { 
     129      $final_output .= 'RewriteCond %{REQUEST_METHOD} "DELETE"'; 
     130      $final_output .= "\n"; 
     131      $final_output .= implode("\n", $output['DELETE']); 
     132 
     133      $final_output .= "\n\n"; 
     134    } 
     135     
     136    if( count($output['GET']) > 0) 
     137    { 
     138      $final_output .= 'RewriteCond %{REQUEST_METHOD} "GET"'; 
     139      $final_output .= "\n"; 
     140      $final_output .= implode("\n", $output['GET']); 
     141 
     142      $final_output .= "\n\n"; 
     143    } 
     144 
     145    if( count($output['POST']) > 0) 
     146    { 
     147      $final_output .= 'RewriteCond %{REQUEST_METHOD} "POST"'; 
     148      $final_output .= "\n"; 
     149      $final_output .= implode("\n", $output['POST']); 
     150 
     151      $final_output .= "\n\n"; 
     152    } 
     153 
     154    return $final_output; 
    88155  } 
    89156 
     
    91158  { 
    92159     
    93     return sprintf('RewriteRule ^%-60s %s?sf_route=%s [QSA,L]', 
     160    return sprintf('RewriteRule ^%s%-60s %s?sf_route=%s [QSA,L]', 
     161      $this->url_prefix, 
    94162      $url, 
    95163      $this->path_prefix, 
     
    99167  } 
    100168 
     169 
    101170  // NOTE to handle .:sf_format option : (\.([^/\.])+$|$) 
    102  
    103171  public function fixUrl($route, $url, $sf_format) 
    104172  { 
  • plugins/swOptimizeRoutesPlugin/trunk/lib/output/swBaseOutputHandler.class.php

    r22847 r22976  
    2121  protected 
    2222    $routing = null, 
     23    $url_prefix = '', 
    2324    $path_prefix = '/index.php'; 
    2425 
     
    3031   * 
    3132   */ 
    32   public function __construct(sfRouting $routing
     33  public function __construct(sfRouting $routing, $options = array()
    3334  { 
     35     
    3436    $this->routing = $routing; 
     37    $this->path_prefix = $options['path_prefix']; 
     38    $this->url_prefix = $options['url_prefix']; 
     39  } 
     40 
     41  public function getMethods($methods) 
     42  { 
     43 
     44    if($methods === null) 
     45    { 
     46      $methods = array('OTHER'); 
     47    } 
     48 
     49    if(!is_array($methods)) 
     50    { 
     51      $methods = array($methods); 
     52    } 
     53 
     54    // clean the sf_format, as the sf_format 
     55    // is just simulated 
     56    $final_methods = array(); 
     57    $simulated_formats = array('PUT', 'DELETE', 'HEAD'); 
     58    foreach($methods as $method) 
     59    { 
     60      $method = strtoupper($method); 
     61 
     62      $final_methods[] = $method; 
     63      if( in_array($method, $simulated_formats) && !in_array('POST', $methods)) 
     64      { 
     65        $final_methods[] = 'POST'; 
     66      } 
     67    } 
     68 
     69 
     70    return $final_methods; 
    3571  } 
    3672 
  • plugins/swOptimizeRoutesPlugin/trunk/lib/swOptimizePatternRouting.class.php

    r22872 r22976  
    4141    } 
    4242 
    43     throw new sfException('The route cannot be match by your server'); 
     43    throw new sfException('The route cannot be match by the server'); 
    4444     
    4545    return parent::findRoute($url); 
     
    7878 
    7979      $class = $route_information['route_class']; 
    80       $data  = gzinflate($route_information['route_serialization']); 
    81 //      $data  = $route_information['route_serialization']; 
    82  
    83  
    84       $options = array(); 
     80      $data  = $route_information['route_serialization']; 
    8581 
    8682      // set array in a config file 
    8783      // or add options to call the event dispatcher 
     84      $options = array(); 
    8885      if(in_array($class, array('sfDoctrineRoute', 'sfPropelRoute'))) 
    8986      { 
     
    9188        $options['type'] = 'object'; 
    9289      } 
    93        
     90 
    9491      $route = new $class('/foo', array(), array(), $options); 
    9592      $route->unserialize($data); 
     
    9794      $this->routes[$name] = $route; 
    9895 
    99       if (true || $this->options['logging']) 
     96      if ($this->options['logging']) 
    10097      { 
    10198        $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Load route on demand "%s" (%s)', get_class($route), $name, $route->getPattern())))); 
     
    152149  public function loadConfiguration() 
    153150  { 
    154 //    $time = microtime(true); 
    155  
    156151    if ($this->options['load_configuration'] 
    157152      && $config =  $this->getConfiguration()->getConfigCache()->checkConfig('config/routing.yml', true)) 
     
    161156      $this->dispatcher->notify(new sfEvent($this, 'routing.load_configuration')); 
    162157    } 
    163  
    164 //    echo  microtime(true) - $time; 
    165      
    166158  } 
    167159} 
  • plugins/swOptimizeRoutesPlugin/trunk/lib/swOptimizeRoutingConfigHandler.class.php

    r22847 r22976  
    3636    $export = array( 
    3737      'route_class' => get_class($route_instance), 
    38       'route_serialization' => gzdeflate($route_instance->serialize()) 
    39 //      'route_serialization' => $route_instance->serialize() 
     38      'route_serialization' => $route_instance->serialize() 
    4039    ); 
    4140 
    4241    $data[] = sprintf('\'%s\' => %s,', $name, var_export($export, 1)); 
    4342  } 
     43 
    4444 
    4545  public function execute($configFiles)