Skip to content

Commit

Permalink
Merge branch 'release/1.0.0-beta.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Gisleburt committed Feb 28, 2015
2 parents 98029f0 + 46ed406 commit 0f3f1fb
Show file tree
Hide file tree
Showing 13 changed files with 360 additions and 385 deletions.
72 changes: 8 additions & 64 deletions src/Controller.php
Expand Up @@ -20,18 +20,10 @@ class Controller
* Endpoints that should not be publicly listed
* @var string[]
*/
private $hiddenEndpoints = [
private $hiddenMethods = [
'getIndexEndpoint' => true, // Value not used
];

/**
* Controllers that should not be publicly listed
* @var string
*/
private $hiddenControllers = [

];

/**
* The status object that represents an HTTP status
* @var Status
Expand Down Expand Up @@ -78,12 +70,12 @@ protected function setStatusCode($statusCode)
* @return $this
* @throws Exception
*/
protected function hideEndpointMethod($methodName)
protected function hideMethod($methodName)
{
if (!method_exists($this, $methodName)) {
throw new Exception(500, "The method '$methodName' does not exist in ".get_called_class());
}
$this->hiddenEndpoints[$methodName] = true;
$this->hiddenMethods[$methodName] = true;
return $this;
}

Expand All @@ -93,13 +85,12 @@ protected function hideEndpointMethod($methodName)
* @return bool
* @throws Exception
*/
public function isEndpointMethodHidden($methodName)
public function isMethodHidden($methodName)
{
if (!method_exists($this, $methodName)) {
throw new Exception(500, "The method '$methodName' does not exist in ".get_called_class());
}
return is_array($this->hiddenEndpoints)
&& isset($this->hiddenEndpoints[$methodName]);
return isset($this->hiddenMethods[$methodName]);
}

/**
Expand All @@ -108,60 +99,13 @@ public function isEndpointMethodHidden($methodName)
* @return $this
* @throws Exception
*/
protected function showEndpointMethod($methodName)
{
if (!method_exists($this, $methodName)) {
throw new Exception(500, "The method '$methodName' does not exist in ".get_called_class());
}
if ($this->isEndpointMethodHidden($methodName)) {
unset($this->hiddenEndpoints[$methodName]);
}
return $this;
}

/**
* Hide a controller
* @param $methodName
* @return $this
* @throws Exception
*/
protected function hideControllerMethod($methodName)
{
if (!method_exists($this, $methodName)) {
throw new Exception(500, "The method '$methodName' does not exist in ".get_called_class());
}
$this->hiddenControllers[$methodName] = true;
return $this;
}

/**
* Is a controller currently hidden
* @param $methodName
* @return bool
* @throws Exception
*/
public function isControllerMethodHidden($methodName)
{
if (!method_exists($this, $methodName)) {
throw new Exception(500, "The method '$methodName' does not exist in ".get_called_class());
}
return is_array($this->hiddenControllers)
&& isset($this->hiddenControllers[$methodName]);
}

/**
* Show a hidden controller
* @param $methodName
* @return $this
* @throws Exception
*/
protected function showControllerMethod($methodName)
protected function showMethod($methodName)
{
if (!method_exists($this, $methodName)) {
throw new Exception(500, "The method '$methodName' does not exist in ".get_called_class());
}
if ($this->isControllerMethodHidden($methodName)) {
unset($this->hiddenControllers[$methodName]);
if ($this->isMethodHidden($methodName)) {
unset($this->hiddenMethods[$methodName]);
}
return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exception.php
Expand Up @@ -86,7 +86,7 @@ public function jsonSerialize()
'code' => $this->getCode(),
];
$class = __CLASS__;
if($this->getPrevious() instanceof $class) {
if ($this->getPrevious() instanceof $class) {
$serialized['previous'] = $this->getPrevious();
}
return $serialized;
Expand Down
43 changes: 43 additions & 0 deletions src/Request.php
Expand Up @@ -136,6 +136,7 @@ protected function getRequestedUri($override = null)
*/
protected function useActualParameters()
{
$this->setParameters($this->urlToParameters());
$this->setParameters($_REQUEST);
$this->setParameters($this->parseHeader($_SERVER));
$this->setParameters($this->stringToObject($this->readBody()));
Expand Down Expand Up @@ -175,6 +176,31 @@ protected function readBody()
return @file_get_contents('php://input');
}

/**
* Turns a url string into an array of parameters
* @param string $url
* @return array
* @SuppressWarnings(PHPMD.Superglobals)
*/
public function urlToParameters($url = null)
{
$urlParameters = [];
if (is_null($url)) {
$url = array_key_exists('REQUEST_URI', $_SERVER)
? $_SERVER['REQUEST_URI']
: '';
}
$url = is_null($url) ? parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) : $url;
$urlParts = explode('/', $url);
reset($urlParts); // Note, the first entry will always be blank
$key = next($urlParts);
while (($value = next($urlParts)) !== false) {
$urlParameters[$key] = $value;
$key = $value;
}
return $urlParameters;
}

/**
* Tries to turn a string of data into an object. Accepts json, xml or a php serialised object
* Failing all else it will return a standard class with the string attached to data
Expand Down Expand Up @@ -227,9 +253,26 @@ public function getParameter($key, $default = null)
if (array_key_exists($key, $this->parameters)) {
return $this->parameters[$key];
}
// We can also flatten out the variable names to see if they exist
$flatKey = $this->flatten($key);
foreach ($this->parameters as $index => $value) {
if ($flatKey == $this->flatten($index)) {
return $value;
}
}
return $default;
}

/**
* Flatten a variable name by removing all non alpha numeric characters and making it lower case
* @param $name
* @return string
*/
protected function flatten($name)
{
return strtolower(preg_replace('/[\W_-]/', '', $name));
}

/**
* Returns all parameters. Does not return header or body parameters, maybe it should
* @return array
Expand Down
7 changes: 4 additions & 3 deletions src/Router.php
Expand Up @@ -105,7 +105,7 @@ public function getEndpoints(Controller $controller)
$methods = get_class_methods($controller);
foreach ($methods as $classMethod) {
if (preg_match('/([a-z]+)([A-Z]\w+)Endpoint$/', $classMethod, $parts)) {
if (!$controller->isEndpointMethodHidden($classMethod)) {
if (!$controller->isMethodHidden($classMethod)) {
$method = strtolower($parts[1]);
$endpoint = $this->camelcaseToHyphenated($parts[2]);
if (!array_key_exists($method, $endpoints)) {
Expand All @@ -129,7 +129,7 @@ public function getControllers(Controller $controller)
$controllers = [];
foreach ($methods as $method) {
if (preg_match('/(\w+)Controller$/', $method, $parts)) {
if (!$controller->isControllerMethodHidden($method)) {
if (!$controller->isMethodHidden($method)) {
$controllers[] = $this->camelcaseToHyphenated($parts[1]);
}
}
Expand Down Expand Up @@ -228,12 +228,13 @@ public function getMethodDocumentation(Controller $controller, $method)
$parameters = array();
$nMatches = preg_match_all('/@param (\S+) \$?(\S+) ?([\S ]+)?/', $doc, $results);
for ($i = 0; $i < $nMatches; $i++) {
$parameterName = $this->camelcaseToHyphenated($results[2][$i]);
$parameter = new \stdClass();
$parameter->type = $results[1][$i];
if ($results[3][$i]) {
$parameter->description = $results[3][$i];
}
$parameters[$results[2][$i]] = $parameter;
$parameters[$parameterName] = $parameter;
}

return [
Expand Down

0 comments on commit 0f3f1fb

Please sign in to comment.