Changeset 2

Show
Ignore:
Timestamp:
05/04/05 03:46:40 (4 years ago)
Author:
bob
Message:

implement zimba's MultipleControllerFactory? from Mojavi forums
http://forum.mojavi.org/index.php?showtopic=958&hl=xmlrpc
closes #1

Location:
trunk/source
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/source/agavi/config/FactoryConfigHandler.class.php

    r1 r2  
    4646  public function & execute ($config) 
    4747  { 
    48  
    49     // available list of factories 
    50     $factories = array('request', 'storage', 'user', 'security_filter'); 
    51  
     48    // available list of controllers 
     49    $required_controllers = array('Controller'); 
     50   
     51    // available list of factories 
     52    $required_factories = array('request', 'storage', 'user', 'security_filter'); 
     53   
    5254    // set our required categories list and initialize our handler 
    53     $categories = array('required_categories' => $factories); 
    54  
     55    $categories = array('required_categories' => $required_controllers); 
    5556    $this->initialize($categories); 
    56  
     57   
    5758    // parse the ini 
    5859    $ini = $this->parseIni($config); 
    59  
     60   
     61    // Revers the order of the controllers 
     62    $ini = array_reverse($ini, true); 
     63   
    6064    // init our data and includes arrays 
    61     $includes  = array(); 
    62     $inits     = array(); 
    63     $instances = array(); 
    64  
    65     // let's do our fancy work 
    66     foreach ($factories as $factory) 
    67     { 
    68  
    69       // see if the factory exists 
    70       if (!isset($ini[$factory])) 
    71       { 
    72  
    73         // factory hasn't been registered 
    74         $error = 'Configuration file "%s" must register "%s" factory'; 
    75         $error = sprintf($config, $factory); 
    76  
    77         throw new ParseException($error); 
    78  
    79       } 
    80  
    81       // get factory keys 
    82       $keys = $ini[$factory]; 
    83  
    84       if (!isset($keys['class'])) 
    85       { 
    86  
    87         // missing class key 
    88         $error = 'Configuration file "%s" specifies category ' . 
    89              '"%s" with missing class key'; 
    90         $error = sprintf($error, $config, $factory); 
    91  
    92         throw new ParseException($error); 
    93  
    94       } 
    95  
    96       $class = $keys['class']; 
    97  
    98       if (isset($keys['file'])) 
    99       { 
    100  
    101         // we have a file to include 
    102         $file =& $keys['file']; 
    103         $file =  $this->replaceConstants($file); 
    104         $file =  $this->replacePath($file); 
    105  
    106         if (!is_readable($file)) 
    107         { 
    108  
    109             // factory file doesn't exist 
    110             $error = 'Configuration file "%s" specifies class ' . 
    111                  '"%s" with nonexistent or unreadablefile ' . 
    112                  '"%s"'; 
    113             $error = sprintf($error, $config, $class, $file); 
    114  
    115             throw new ParseException($error); 
    116  
     65    $controllers = array(); 
     66   
     67    // check that every controller has the right paramers 
     68    foreach($ini as $controllerName => $factories) { 
     69      // init our data and includes arrays 
     70      $includes  = array(); 
     71      $inits     = array(); 
     72      $instances = array(); 
     73   
     74      // Build all classes   
     75      foreach($required_factories as $factory) { 
     76        if (!array_key_exists($factory, $factories)) { 
     77          $error = 'Configuration file "%s" is missing "%s" key in "%s" category'; 
     78          $error = sprintf($error, $config, $factory, $controllerName); 
     79          throw new ParseException($error); 
     80        } 
     81   
     82        // Get class name 
     83        $class = $factories[$factory]; 
     84   
     85        // parse parameters 
     86        $parameters = ParameterParser::parse($factories, $factory .'.param'); 
     87   
     88        // append new data 
     89        switch ($factory) { 
     90          case 'request': 
     91   
     92            // append instance creation 
     93            $tmp = "\t\$this->request = " .  "Request::newInstance('%s');"; 
     94            $instances[] = sprintf($tmp, $class); 
     95   
     96            // append instance initialization 
     97            $tmp = "\t\$this->request->initialize(\$this->context, " .  "%s);"; 
     98            $inits[] = sprintf($tmp, $parameters); 
     99   
     100            break; 
     101          case 'security_filter': 
     102   
     103            // append creation/initialization in one swipe 
     104            $tmp = "\n\tif (MO_USE_SECURITY)\n\t{\n" . 
     105                   "\t\t\$this->securityFilter = " . 
     106                   "SecurityFilter::newInstance('%s');\n" . 
     107                   "\t\t\$this->securityFilter->initialize(" . 
     108                   "\$this->context, %s);\n\t}\n"; 
     109            $inits[] = sprintf($tmp, $class, $parameters); 
     110   
     111            break; 
     112          case 'storage': 
     113   
     114            // append instance creation 
     115            $tmp = "\t\$this->storage = " . 
     116                   "Storage::newInstance('%s');"; 
     117            $instances[] = sprintf($tmp, $class); 
     118   
     119            // append instance initialization 
     120            $tmp = "\t\$this->storage->initialize(\$this->context, " . "%s);"; 
     121            $inits[] = sprintf($tmp, $parameters); 
     122   
     123            break; 
     124          case 'user': 
     125   
     126            // append instance creation 
     127            $tmp = "\t\$this->user = User::newInstance('%s');"; 
     128            $instances[] = sprintf($tmp, $class); 
     129   
     130            // append instance initialization 
     131            $tmp = "\t\$this->user->initialize(\$this->context, %s);"; 
     132            $inits[] = sprintf($tmp, $parameters); 
     133            break; 
     134          default: 
     135           continue; 
    117136        } 
    118137 
    119         // append our data 
    120         $tmp        = "require_once('%s');"; 
    121         $includes[] = sprintf($tmp, $file); 
    122  
     138        if (isset($factories[$factory.'.file'])) { 
     139          // we have a file to include 
     140          $file =& $factories[$factory.'.file']; 
     141          $file =  $this->replaceConstants($file); 
     142          $file =  $this->replacePath($file); 
     143   
     144          if (!is_readable($file)) { 
     145     
     146            // factory file doesn't exist 
     147            $error = 'Configuration file "%s" specifies class ' . 
     148                     '"%s" with nonexistent or unreadablefile ' . 
     149                     '"%s"'; 
     150            $error = sprintf($error, $config, $class, $file); 
     151   
     152            throw new ParseException($error); 
     153     
     154          } 
     155     
     156          // append our data 
     157          $tmp        = "\trequire_once('%s');"; 
     158          $includes[] = sprintf($tmp, $file); 
     159     
     160        } 
    123161      } 
    124  
    125       // parse parameters 
    126       $parameters = ParameterParser::parse($keys); 
    127  
    128       // append new data 
    129       switch ($factory) 
    130       { 
    131  
    132         case 'request': 
    133  
    134             // append instance creation 
    135             $tmp         = "\$this->request = " . 
    136                        "Request::newInstance('%s');"; 
    137             $instances[] = sprintf($tmp, $class); 
    138  
    139             // append instance initialization 
    140             $tmp     = "\$this->request->initialize(\$this->context, " . 
    141                    "%s);"; 
    142             $inits[] = sprintf($tmp, $parameters); 
    143  
    144             break; 
    145  
    146         case 'security_filter': 
    147  
    148             // append creation/initialization in one swipe 
    149             $tmp     = "\nif (MO_USE_SECURITY)\n{\n" . 
    150                    "\t\$this->securityFilter = " . 
    151                    "SecurityFilter::newInstance('%s');\n" . 
    152                    "\t\$this->securityFilter->initialize(" . 
    153                    "\$this->context, %s);\n}\n"; 
    154             $inits[] = sprintf($tmp, $class, $parameters); 
    155  
    156             break; 
    157  
    158         case 'storage': 
    159  
    160             // append instance creation 
    161             $tmp         = "\$this->storage = " . 
    162                        "Storage::newInstance('%s');"; 
    163             $instances[] = sprintf($tmp, $class); 
    164  
    165             // append instance initialization 
    166             $tmp     = "\$this->storage->initialize(\$this->context, " . 
    167                    "%s);"; 
    168             $inits[] = sprintf($tmp, $parameters); 
    169  
    170             break; 
    171  
    172         case 'user': 
    173  
    174             // append instance creation 
    175             $tmp         = "\$this->user = User::newInstance('%s');"; 
    176             $instances[] = sprintf($tmp, $class); 
    177  
    178             // append instance initialization 
    179             $tmp     = "\$this->user->initialize(\$this->context, %s);"; 
    180             $inits[] = sprintf($tmp, $parameters); 
    181  
    182       } 
    183  
     162     
     163      // context creation 
     164      $context = "\t\$this->context = new Context(%s, %s, %s, %s, %s);"; 
     165      $context = sprintf($context, '$this', '$this->request', '$this->user', '$this->storage', 
     166      '$this->databaseManager'); 
     167     
     168      $tmp = "if (\$this instanceof $controllerName)\n{\n%s\n%s\n%s\n%s\n\treturn;\n}"; 
     169      $controllers[] = sprintf($tmp, implode("\n", $includes), 
     170      implode("\n", $instances), $context, implode("\n", $inits)); 
    184171    } 
    185  
    186     // context creation 
    187     $context = "\$this->context = new Context(%s, %s, %s, %s, %s);"; 
    188     $context = sprintf($context, '$this', '$this->request', 
    189                '$this->user', '$this->storage', 
    190                '$this->databaseManager'); 
    191  
     172   
    192173    // compile data 
    193174    $retval = "<?php\n" . 
    194           "// auth-generated by FactoryConfigHandler\n" . 
    195           "// date: %s\n%s\n%s\n%s\n%s\n?>"; 
     175    "// auth-generated by FactoryConfigHandler\n" . 
     176    "// date: %s\n%s\n?>"; 
    196177    $retval = sprintf($retval, date('m/d/Y H:i:s'), 
    197               implode("\n", $includes), implode("\n", $instances), 
    198               $context, implode("\n", $inits)); 
     178    implode("\n", $controllers)); 
    199179 
    200180    return $retval; 
  • trunk/source/webapp/config/factories.ini

    r1 r2  
    77; | LICENSE file online at http://www.agavi.org.                               | 
    88; +----------------------------------------------------------------------------+ 
    9 ; | AGAVI FACTORY CONFIGURATION                                               | 
     9; | AGAVI FACTORY CONFIGURATION                                                | 
    1010; | -------------------------------------------------------------------------- | 
    11 ; | This configuration has 4 categories, all of which are required:            | 
     11; | This configuration uses a root category called Controller.                 | 
     12; | If you want to set a particular parameter to another Controller, create a  | 
     13; | new category. Resolution works in order of inheritance.                    | 
     14; | -------------------------------------------------------------------------- | 
     15; | REQUIRED KEYS:                                                             | 
    1216; |                                                                            | 
    1317; | 1. request         - The Request class implementation.                     | 
     
    1620; | 4. security_filter - The SecurityFilter class implementation.              | 
    1721; |                                                                            | 
    18 ; | NOTE: The order of the factory specification is important. Do not reorder  | 
    19 ; |       them!                                                                | 
    20 ; | -------------------------------------------------------------------------- | 
    21 ; | REQUIRED KEYS:                                                             | 
    22 ; |                                                                            | 
    23 ; | 1. class - The class name providing the custom implementation.             | 
    24 ; |                                                                            | 
    2522; | OPTIONAL KEYS:                                                             | 
    2623; |                                                                            | 
    27 ; | 1. file  - The filesystem path to the class file. If the path is relative, | 
    28 ; |            it will be relative to the MO_WEBAPP_DIR Agavi application     | 
    29 ; |            setting.                                                        | 
     24; | 1. requiredkey.file  - The filesystem path to the class file. If the path | 
     25; |            is relative, it will be relative to the MO_WEBAPP_DIR Agavi     | 
     26; |            application setting.                                            | 
    3027; | -------------------------------------------------------------------------- | 
    3128; | PARAMETER KEYS:                                                            | 
     
    3532; | be passed.                                                                 | 
    3633; |                                                                            | 
    37 ; |     param.<name> = "<value>"                                               | 
     34; |     requiredkey.param.<name> = "<value>"                                   | 
    3835; |                                                                            | 
    3936; | Parameter keys can also be used to pass an array of values instead of a    | 
    4037; | single value.                                                              | 
    4138; |                                                                            | 
    42 ; |     param.<name>.1 = "<value1>"                                            | 
    43 ; |     param.<name>.2 = "<value2>"                                            | 
     39; |     requiredkey.param.<name>.1 = "<value1>"                                | 
     40; |     requiredkey.param.<name>.2 = "<value2>"                                | 
    4441; |                                                                            | 
    4542; | For a list of available parameters for a class, browse the class source    | 
     
    5653; +----------------------------------------------------------------------------+ 
    5754 
    58 ; +----------------------------------------------------------------------------+ 
    59 ; | Specify which Request implementation to use.                               | 
    60 ; +----------------------------------------------------------------------------+ 
    61 [request] 
     55[Controller] 
     56request          = "WebRequest" 
     57storage          = "SessionStorage" 
     58user             = "BasicSecurityUser" 
     59security_filter  = "BasicSecurityFilter" 
    6260 
    63     class = "WebRequest" 
    64  
    65 ; +----------------------------------------------------------------------------+ 
    66 ; | Specify which Storage implementation to use.                               | 
    67 ; +----------------------------------------------------------------------------+ 
    68 [storage] 
    69  
    70     class = "SessionStorage" 
    71  
    72 ; +----------------------------------------------------------------------------+ 
    73 ; | Specify which User implementation to use.                                  | 
    74 ; +----------------------------------------------------------------------------+ 
    75 [user] 
    76  
    77     class = "BasicSecurityUser" 
    78  
    79 ; +----------------------------------------------------------------------------+ 
    80 ; | Specify which SecurityFilter implementation to use.                        | 
    81 ; +----------------------------------------------------------------------------+ 
    82 [security_filter] 
    83  
    84     class = "BasicSecurityFilter" 
     61[ConsoleController] 
     62request          = "ConsoleRequest" 
     63storage          = "SessionStorage" 
     64user             = "BasicSecurityUser" 
     65security_filter  = "BasicSecurityFilter"