ATWF A Tcl Web Framwork

Check-in [def9de5ecf]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:fixes for using tcl code

git-svn-id: https://atwf.svn.sourceforge.net/svnroot/atwf@610 3ddb7fe1-eb8e-4980-8f37-d9e37380b5db

Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:def9de5ecfe3d389d53216d187c54c17d938a819
User & Date: arnulf@wiedemann-pri.de 2010-12-04 21:53:10
Context
2010-12-04
21:54
fixed typo

git-svn-id: https://atwf.svn.sourceforge.net/svnroot/atwf@611 3ddb7fe1-eb8e-4980-8f37-d9e37380b5db check-in: f3b837a976 user: arnulf@wiedemann-pri.de tags: trunk

21:53
fixes for using tcl code

git-svn-id: https://atwf.svn.sourceforge.net/svnroot/atwf@610 3ddb7fe1-eb8e-4980-8f37-d9e37380b5db check-in: def9de5ecf user: arnulf@wiedemann-pri.de tags: trunk

2010-12-03
21:28
removed debugging output

git-svn-id: https://atwf.svn.sourceforge.net/svnroot/atwf@609 3ddb7fe1-eb8e-4980-8f37-d9e37380b5db check-in: 2c7aae3a98 user: arnulf@wiedemann-pri.de tags: trunk

Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to library/ATWF/Controllers/Routers/Rewrite.tcl.

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
...
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
...
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182

183
184
185


186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
...
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
...
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
...
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355

356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
...
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
    public method setChainNameSeparator {separator}
    public method getChainNameSeparator {}
}

  #============================ addDeaultRoutes ================================
  # Add default routes which are used to mimic basic router behaviour
  # 
  # @return Zend_Controllers::Routers::Rewrite
::itcl::body Controllers::Routers::Rewrite::addDefaultRoutes {} {
    if {![hasRoute default]} {
       set dispatcher [[getFrontController] getDispatcher]
       set request [[getFrontController] getRequest]

       set compat [uplevel #0 ::ATWF::Controllers::Routers::Routings::Module #auto [list [list] $dispatcher $request]]

        set _routes [dict merge [dict create default $compat] $_routes]
    }
    
    return $this;
}

  #============================ addRoute ===========================================
  # Add route to the route chain
  # 
  # If route contains method setRequest(), it is initialized with a request object
  #
................................................................................

  #============================ addRoutes ======================================
  # Add routes to the route chain
  #
  # @param  array $routes Array of routes with names as keys and routes as values
  # @return Zend_Controllers::Routers::Rewrite
::itcl::body Controllers::Routers::Rewrite::addRoutes {routes} {
    foreach ($routes as $name => $route) {
        $this->addRoute($name, $route);
    }
    
    return $this;
}

  #============================ AddConfig ======================================
  # Create routes out of Zend_Config configuration
................................................................................
  # $router->addConfig($config, 'routes');
  #
  # @param  ::ATWF::Config config  Configuration object
  # @param  string      section Name of the config section containing route's definitions
  # @throws ::ATWF::Controllers::Routers::Exception
  # @return ::ATWF::Controllers::Routers::Rewrite
::itcl::body Controllers::Routers::Rewrite::addConfig {config {section {}}} {
    if ($section !== null) {
        if ($config->{$section} === null) {
            return -code error "No route configuration in section '$section'"
        }
        
        $config = $config->{$section};
    }
    
    foreach ($config as $name => $info) {
        $route = $this->_getRouteFromConfig($info);
        
        if ($route instanceof ::ATWF::Controllers::Routers::Route::Chain) {
            if (!isset($info->chain)) {
                return -code error "No chain defined"
            }
            
            if ($info->chain instanceof Zend_Config) {
                $childRouteNames = $info->chain;
            } else {
                $childRouteNames = explode(',', $info->chain);
            } 
                
            foreach ($childRouteNames as $childRouteName) {
                $childRoute = $this->getRoute(trim($childRouteName));
                $route->chain($childRoute);
            }
            
            $this->addRoute($name, $route);
        } elseif (isset($info->chains) && $info->chains instanceof Zend_Config) {

            $this->_addChainRoutesFromConfig($name, $route, $info->chains);
        } else {
            $this->addRoute($name, $route);


        }
    }

    return $this;
}
    
  #============================ _getRouteFromConfig ============================
  # Get a route frm a config instance
  #
  # @param  ::ATWF::Config info
  # @return ::ATWF::Controllers::Routers::Route::Route
::itcl::body Controllers::Routers::Rewrite::_getRouteFromConfig {info} {
    set class [expr {[dict exists $info type] ? [dict get $info type] : ::ATWF::Controllers::Routers::Route}]
    if {!classExists $class} {
        Loader::loadClass $class
    }
          
    set route [call_user_func [dict create $class getInstance] $info]
    
    if {[dict exists $info abstract] && [dict get $info abstract] ne "" && [methodExists $route isAbstract]} {
        $route isAbstract true
................................................................................
  # Add chain routes from a config route
  #
  # @param  string                                   name
  # @param  ::ATWF::Controllers::Routers::Route::Route route
  # @param  ::ATWF::Config                           childRoutesInfo
  # @return void
::itcl::body Controllers::Routers::Rewrite::_addChainRoutesFromConfig {name route childRoutesInfo} {
    foreach ($childRoutesInfo as $childRouteName => $childRouteInfo) {
        if (is_string($childRouteInfo)) {
            $childRouteName = $childRouteInfo;
            $childRoute     = $this->getRoute($childRouteName);
        } else {
            $childRoute = $this->_getRouteFromConfig($childRouteInfo);
        }
        
        if ($route instanceof ::ATWF::Controllers::Routers::Route::Chain) {
            $chainRoute = clone $route;
            $chainRoute->chain($childRoute);
        } else {
            $chainRoute = $route->chain($childRoute);
        }
        
        $chainName = $name . $this->_chainNameSeparator . $childRouteName;
        
        if (isset($childRouteInfo->chains)) {
            $this->_addChainRoutesFromConfig($chainName, $chainRoute, $childRouteInfo->chains);
        } else {
            $this->addRoute($chainName, $chainRoute);
        }
    }
}

  #============================ removeRoute ====================================
  # Remove a route from the route chain
  #
  # @param  string $name Name of the route
  # @throws ::ATWF::Controllers::Routers::Exception
  # @return ::ATWF::Controllers::Routers::Rewrite
::itcl::body Controllers::Routers::Rewrite::removeRoute {name} {
    if (!isset($this->_routes[$name])) {
        return -code error "Route $name is not defined"
    }
    
    unset($this->_routes[$name]);
    
    return $this;
}

  #============================ removeDefaultRoutes ============================
  # Remove all standard default routes
  #
  # @param  ::ATWF::Controllers::Routers::Route::Route Route
  # @return ::ATWF::Controllers::Routers::Rewrite
::itcl::body Controllers::Routers::Rewrite::removeDefaultRoutes {} {
    $this->_useDefaultRoutes = false;
    
    return $this;
}

  #============================ hasRoute =======================================
  # Check if named route exists
  #
  # @param  string $name Name of the route
  # @return boolean
................................................................................
  #============================ getRoute =======================================
  # Retrieve a named route
  #
  # @param string $name Name of the route
  # @throws ::ATWF::Controllers::Routers::Exception
  # @return ::ATWF::Controllers::Routers::Route::Route Route object
::itcl::body Controllers::Routers::Rewrite::getRoute {name} {
    if (!isset($this->_routes[$name])) {
        return -code error "Route $name is not defined"
    }
    
    return $this->_routes[$name];
}

  #============================ getCurrentRoute ================================
  # Retrieve a currently matched route
  #
  # @throws ::ATWF::Controllers::Routers::Exception
  # @return ::ATWF::Controllers::Routers::Route::Route Route object
::itcl::body Controllers::Routers::Rewrite::getCurrentRoute {} {
    if (!isset($this->_currentRoute)) {
        return -code error "Current route is not defined"
    }
    return $this->getRoute($this->_currentRoute);
}

  #============================ getCurrentRouteName ============================
  # Retrieve a name of currently matched route
  #
  # @throws ::ATWF::Controllers::Routers::Exception
  # @return ::ATWF::Controllers::Routers::Route::Route Route object
::itcl::body Controllers::Routers::Rewrite::getCurrentRouteName {} {
    if (!isset($this->_currentRoute)) {
        return -code error "Current route is not defined"
    }
    return $this->_currentRoute;
}

  #============================ getRoutes ======================================
  # Retrieve an array of routes added to the route chain
  #
  # @return array All of the defined routes
::itcl::body Controllers::Routers::Rewrite::getRoutes {} {
    return $this->_routes;
}

  #============================ route ==========================================
  # Find a matching route to the current PATH_INFO and inject
  # returning values to the Request object.
  #
  # @throws ::ATWF::Controllers::Routers::Exception
................................................................................
        addDefaultRoutes
    }

    # Find the matching route
    foreach {name route} $_routes {
        # TODO: Should be an interface method. Hack for 1.0 BC
        if {([$route info function isAbstract] ne "") && [$route isAbstract]} {
            continue;
        }
        
        # TODO: Should be an interface method. Hack for 1.0 BC  
        if {([$route info function getVersion] ne "") || ([$route getVersion] == 1)} {
            set match [$request getPathInfo]
        } else {
            set match $request;
        }
                    

        if {[set params [$route match $match]] ne ""} {
            _setRequestParams $request $params
            set _currentRoute $name;
            break;
        }
    }

    return $request;

}

  #============================ _SetRequestParams ==============================
::itcl::body Controllers::Routers::Rewrite::_setRequestParams {request params} {
    foreach {param value} $params {
        $request setParam $param $value
................................................................................
  # @param  bool $reset Whether to reset to the route defaults ignoring URL params
  # @param  bool $encode Tells to encode URL parts on output
  # @throws ::ATWF::Controllers::Routers::Exception
  # @return string Resulting absolute URL path
::itcl::body Controllers::Routers::Rewrite::assemble {userParams {name {}} {reset  false} {encode  true}} {
    if ($name == null) {
        try {
            $name = $this->getCurrentRouteName();
        } catch (::ATWF::Controllers::Routers::Exception $e) {
            $name = 'default';
        }
    }
    
    $params = array_merge($this->_globalParams, $userParams);
    
    $route = $this->getRoute($name);
    $url   = $route->assemble($params, $reset, $encode);

    if (!preg_match('|^[a-z]+://|', $url)) {
        $url = rtrim($this->getFrontController()->getBaseUrl(), '/') . '/' . $url;
    }

    return $url;
}
    
  #============================ setGlobalParam =================================
  # Set a global parameter
  # 
  # @param  string $name
  # @param  mixed $value
  # @return Zend_Controllers::Routers::Rewrite
::itcl::body Controllers::Routers::Rewrite::setGlobalParam {name value} {
    $this->_globalParams[$name] = $value;

    return $this;
}
    
  #============================ setChainNameSeparator ==========================
  # Set the separator to use with chain names
  # 
  # @param  string $separator The separator to use
  # @return Zend_Controllers::Routers::Rewrite
::itcl::body Controllers::Routers::Rewrite::setChainNameSeparator {separator} {
    $this->_chainNameSeparator = $separator;
    
    return $this;
}
    
  #============================ getChainNameSeparator ==========================
  # Get the separator to use for chain names
  # 
  # @return string
::itcl::body Controllers::Routers::Rewrite::getChainNameSeparator {} {
    return $this->_chainNameSeparator;
}

} ; # END ::ATWF







|










|







 







|
|







 







|




|


|
|

|
|



|
|

|


|
|
|


|
|
>
|
|
<
>
>



|









|







 







|
|
|
|

|


|
|
|

|


|

|
|

|











|



|
<









|

|







 







|



|








|


|








|


|







|







 







|






|


>


|
|



|







 







|

|



|

|
|

|
|


|









|

|








|

|







|



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
...
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
...
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
...
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258

259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
...
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
...
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
...
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
    public method setChainNameSeparator {separator}
    public method getChainNameSeparator {}
}

  #============================ addDeaultRoutes ================================
  # Add default routes which are used to mimic basic router behaviour
  # 
  # @return ::ATWF::Controllers::Routers::Rewrite
::itcl::body Controllers::Routers::Rewrite::addDefaultRoutes {} {
    if {![hasRoute default]} {
       set dispatcher [[getFrontController] getDispatcher]
       set request [[getFrontController] getRequest]

       set compat [uplevel #0 ::ATWF::Controllers::Routers::Routings::Module #auto [list [list] $dispatcher $request]]

        set _routes [dict merge [dict create default $compat] $_routes]
    }
    
    return $this
}

  #============================ addRoute ===========================================
  # Add route to the route chain
  # 
  # If route contains method setRequest(), it is initialized with a request object
  #
................................................................................

  #============================ addRoutes ======================================
  # Add routes to the route chain
  #
  # @param  array $routes Array of routes with names as keys and routes as values
  # @return Zend_Controllers::Routers::Rewrite
::itcl::body Controllers::Routers::Rewrite::addRoutes {routes} {
    foreach {name route} $routes {
        addRoute $name $route
    }
    
    return $this;
}

  #============================ AddConfig ======================================
  # Create routes out of Zend_Config configuration
................................................................................
  # $router->addConfig($config, 'routes');
  #
  # @param  ::ATWF::Config config  Configuration object
  # @param  string      section Name of the config section containing route's definitions
  # @throws ::ATWF::Controllers::Routers::Exception
  # @return ::ATWF::Controllers::Routers::Rewrite
::itcl::body Controllers::Routers::Rewrite::addConfig {config {section {}}} {
    if {$section ne ""} {
        if ($config->{$section} === null) {
            return -code error "No route configuration in section '$section'"
        }
        
        set config [$config->{$section}]
    }
    
    foreach {name my_info} $config {
        set route [_getRouteFromConfig $my_info]
        
        if {[isInstanceOf ::ATWF::Controllers::Routers::Route::Chain $route]} {
            if {![dict exists my_info chain]} {
                return -code error "No chain defined"
            }
            
            if {[isInstanceOf ::ATWF::Config [dict get my_info chain]} {
                set childRouteNames [dict get my_info chain]
            } else {
                set childRouteNames [explode(',' [dict get my_info chain]]
            } 
                
            foreach {idx childRouteName} $childRouteNames {
                set childRoute [getRoute [string trim $childRouteName]]
                dict set route chain$childRoute
            }
            
            addRoute $name $route
        } else {
	    if {[dict exists my_info chains] && [isInstanceOf ::ATWF::Config [dict get my_info chains]]} {
                addChainRoutesFromConfig $name $route [dict get my_info chains]
            } else {

                addRoute$name $route
            }
        }
    }

    return $this
}
    
  #============================ _getRouteFromConfig ============================
  # Get a route frm a config instance
  #
  # @param  ::ATWF::Config info
  # @return ::ATWF::Controllers::Routers::Route::Route
::itcl::body Controllers::Routers::Rewrite::_getRouteFromConfig {info} {
    set class [expr {[dict exists $info type] ? [dict get $info type] : ::ATWF::Controllers::Routers::Route}]
    if {![classExists $class]} {
        Loader::loadClass $class
    }
          
    set route [call_user_func [dict create $class getInstance] $info]
    
    if {[dict exists $info abstract] && [dict get $info abstract] ne "" && [methodExists $route isAbstract]} {
        $route isAbstract true
................................................................................
  # Add chain routes from a config route
  #
  # @param  string                                   name
  # @param  ::ATWF::Controllers::Routers::Route::Route route
  # @param  ::ATWF::Config                           childRoutesInfo
  # @return void
::itcl::body Controllers::Routers::Rewrite::_addChainRoutesFromConfig {name route childRoutesInfo} {
    foreach {childRouteName childRouteInfo} $childRoutesInfo} {
        if {![::itcl::is object $childRouteInfo]} {
            set childRouteName $childRouteInfo;
            set childRoute     [getRoute $childRouteName]
        } else {
            set childRoute [_getRouteFromConfig $childRouteInfo]
        }
        
        if {[isInstanceOf ::ATWF::Controllers::Routers::Route::Chain $route]} {
            set chainRoute [clone $route]
            set chainRoute [chain $childRoute]
        } else {
            set chainRoute [$route chain $childRoute]
        }
        
        set chainName ${name}${_chainNameSeparator}$childRouteName
        
        if {[$childRouteInfo chains] ne ""} {
            addChainRoutesFromConfig $chainName $chainRoute [$childRouteInfo chains]
        } else {
            addRoute $chainName $chainRoute
        }
    }
}

  #============================ removeRoute ====================================
  # Remove a route from the route chain
  #
  # @param  string $name Name of the route
  # @throws ::ATWF::Controllers::Routers::Exception
  # @return ::ATWF::Controllers::Routers::Rewrite
::itcl::body Controllers::Routers::Rewrite::removeRoute {name} {
    if {![dict exists _routes $name]} {
        return -code error "Route $name is not defined"
    }
    
    set routes [dict remove $_routes $name]

    return $this;
}

  #============================ removeDefaultRoutes ============================
  # Remove all standard default routes
  #
  # @param  ::ATWF::Controllers::Routers::Route::Route Route
  # @return ::ATWF::Controllers::Routers::Rewrite
::itcl::body Controllers::Routers::Rewrite::removeDefaultRoutes {} {
    set _useDefaultRoutes false
    
    return $this
}

  #============================ hasRoute =======================================
  # Check if named route exists
  #
  # @param  string $name Name of the route
  # @return boolean
................................................................................
  #============================ getRoute =======================================
  # Retrieve a named route
  #
  # @param string $name Name of the route
  # @throws ::ATWF::Controllers::Routers::Exception
  # @return ::ATWF::Controllers::Routers::Route::Route Route object
::itcl::body Controllers::Routers::Rewrite::getRoute {name} {
    if {![dict exists _routes $name]} {
        return -code error "Route $name is not defined"
    }
    
    return [dict get _routes $name]
}

  #============================ getCurrentRoute ================================
  # Retrieve a currently matched route
  #
  # @throws ::ATWF::Controllers::Routers::Exception
  # @return ::ATWF::Controllers::Routers::Route::Route Route object
::itcl::body Controllers::Routers::Rewrite::getCurrentRoute {} {
    if {$_currentRoute eq ""} {
        return -code error "Current route is not defined"
    }
    return [getRoute $_currentRoute]
}

  #============================ getCurrentRouteName ============================
  # Retrieve a name of currently matched route
  #
  # @throws ::ATWF::Controllers::Routers::Exception
  # @return ::ATWF::Controllers::Routers::Route::Route Route object
::itcl::body Controllers::Routers::Rewrite::getCurrentRouteName {} {
    if {$_currentRoute eq ""} {
        return -code error "Current route is not defined"
    }
    return $_currentRoute
}

  #============================ getRoutes ======================================
  # Retrieve an array of routes added to the route chain
  #
  # @return array All of the defined routes
::itcl::body Controllers::Routers::Rewrite::getRoutes {} {
    return $_routes
}

  #============================ route ==========================================
  # Find a matching route to the current PATH_INFO and inject
  # returning values to the Request object.
  #
  # @throws ::ATWF::Controllers::Routers::Exception
................................................................................
        addDefaultRoutes
    }

    # Find the matching route
    foreach {name route} $_routes {
        # TODO: Should be an interface method. Hack for 1.0 BC
        if {([$route info function isAbstract] ne "") && [$route isAbstract]} {
            continue
        }
        
        # TODO: Should be an interface method. Hack for 1.0 BC  
        if {([$route info function getVersion] ne "") || ([$route getVersion] == 1)} {
            set match [$request getPathInfo]
        } else {
            set match $request
        }
                    
	# for Tcl match should always be [$request getPathInfo] as getVersion returns 1
        if {[set params [$route match $match]] ne ""} {
            _setRequestParams $request $params
            set _currentRoute $name
            break
        }
    }

    return $request

}

  #============================ _SetRequestParams ==============================
::itcl::body Controllers::Routers::Rewrite::_setRequestParams {request params} {
    foreach {param value} $params {
        $request setParam $param $value
................................................................................
  # @param  bool $reset Whether to reset to the route defaults ignoring URL params
  # @param  bool $encode Tells to encode URL parts on output
  # @throws ::ATWF::Controllers::Routers::Exception
  # @return string Resulting absolute URL path
::itcl::body Controllers::Routers::Rewrite::assemble {userParams {name {}} {reset  false} {encode  true}} {
    if ($name == null) {
        try {
            set name [getCurrentRouteName]
        } catch (::ATWF::Controllers::Routers::Exception $e) {
            set name default
        }
    }
    
    set params [array_merge $_globalParams $userParams]
    
    set route [getRoute $name]
    set url   [$routeassemble $params $reset $encode]

    if {![preg_match {^[a-z]+://} $url]} {
        set url [string trimright [[getFrontController] getBaseUrl "/"]"/$url"
    }

    return $url
}
    
  #============================ setGlobalParam =================================
  # Set a global parameter
  # 
  # @param  string $name
  # @param  mixed $value
  # @return Zend_Controllers::Routers::Rewrite
::itcl::body Controllers::Routers::Rewrite::setGlobalParam {name value} {
    dict set _globalParams $name $value

    return $this
}
    
  #============================ setChainNameSeparator ==========================
  # Set the separator to use with chain names
  # 
  # @param  string $separator The separator to use
  # @return Zend_Controllers::Routers::Rewrite
::itcl::body Controllers::Routers::Rewrite::setChainNameSeparator {separator} {
    set _chainNameSeparator $separator
    
    return $this
}
    
  #============================ getChainNameSeparator ==========================
  # Get the separator to use for chain names
  # 
  # @return string
::itcl::body Controllers::Routers::Rewrite::getChainNameSeparator {} {
    return $_chainNameSeparator
}

} ; # END ::ATWF