Yauheni1
unread,Jan 6, 2012, 6:08:01 PM1/6/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to GluePHP
Changes:
1) Single exception to catch on execution(Invalid parameter just to
make sure that class is used right).
2) Class can be used in CLI mode.
3) Class is fully testable.
<?php
class glue
{
public function __construct($request, $method)
{
$this->_request = $request;
$this->_method = $method;
}
public function exec()
{
foreach ($this->_urls as $regex => $class) {
$regex = str_replace('/', '\/', $regex);
$regex = '^' . $regex . '\/?$';
if (!preg_match("/$regex/i", $this->_request, $matches)) {
continue;
}
if (class_exists($class)) {
$obj = new $class;
if (method_exists($obj, $this->_method)) {
return $obj->{$this->_method}($matches);;
}
}
}
throw new UnexpectedValueException("URL, $path, not found.");
}
public function stick($urls)
{
if (!is_array($urls)) {
throw new InvalidArgumentException('Urls should be passed
as an array.');
}
$this->_urls = $urls;
return $this;
}
protected $_urls = array();
protected $_method;
protected $_request;
}
$urls = array(
'/' => 'index',
);
class index {
function GET() {
echo "Hello, World!" . PHP_EOL;
}
}
$app = new glue(
'/', //$_SERVER['REQUEST_URI']
'GET' //strtolower($_SERVER['REQUEST_METHOD'])
);
$app->stick($urls)->exec();