Making it testable and $_Server independent?

45 views
Skip to first unread message

Yauheni1

unread,
Jan 6, 2012, 6:08:01 PM1/6/12
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();

Joe

unread,
Jan 8, 2012, 4:00:10 PM1/8/12
to GluePHP
Hi there,

> 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.

Nice changes.

If you use BitBucket, you could fork the Glue repo and host this
version in your own repo.

Thanks,
Joe
Reply all
Reply to author
Forward
0 new messages