Skip to content

Commit

Permalink
Endpoint LoadScript functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Dec 22, 2014
1 parent c9a7f18 commit b792ac0
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 9 deletions.
11 changes: 11 additions & 0 deletions src/Api/ApiLogicNotFoundException.php
@@ -0,0 +1,11 @@
<?php
/**
*
*
* PHP.Gt (http://php.gt)
* @copyright Copyright Ⓒ 2014 Bright Flair Ltd. (http://brightflair.com)
* @license Apache Version 2.0, January 2004. http://www.apache.org/licenses
*/
namespace Gt\Api;

class ApiLogicNotFoundException extends \Gt\Core\Exception\GtException {}#
56 changes: 48 additions & 8 deletions src/Api/Endpoint.php
Expand Up @@ -19,6 +19,7 @@ class Endpoint {
private $params;

private $scriptPath;
private $scriptNamespace;

/**
* @param string $path The absolute path to the API version directory on disk
Expand All @@ -31,6 +32,8 @@ public function __construct($path, $subPath, $params) {
$this->params = $params;

$this->scriptPath = $this->getScriptPath();

$this->loadScript($this->scriptPath);
var_dump($this);die();
}

Expand All @@ -49,21 +52,17 @@ public function __construct($path, $subPath, $params) {
* the method will be placed after two colons (i.e. /path/to/file.php::myMethod)
*/
private function getScriptPath() {
$method = "";

$fullPath = $this->path . "/" . $this->subPath;
$fullPath = Path::fixCase($fullPath);
var_dump($fullPath);

// $actionName is the name of the API action being done. This may be the
// name of a script, or the name of a method within a Logic class.
$actionName = basename($fullPath);
$ext = "";

var_dump($actionName);

$containerPath = dirname($fullPath);

var_dump($containerPath);

// $containerPath may be a directory containing the $actionName script, or
// a Logic class containing the $actionName as a method.
if(is_dir($containerPath)) {
Expand All @@ -83,11 +82,52 @@ private function getScriptPath() {
}
else {
// TODO: Handle Logic class with $actionName method.
// $method = "somethingHere";
}

var_dump($ext);
die("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
$return = "$fullPath.$ext";

if(!empty($method)) {
$return .= "::$method";
}

return $return;
}

private function loadScript($scriptPath) {
$pathInfo = pathinfo(strtok($scriptPath, ":"));
$method = strtok(":");

switch ($pathInfo["extension"]) {
case "php":
// With no method passed in, the path must be to an API Logic class
// with a go() method.
$namespace = Path::getNamespace($scriptPath);

// Check class exists within autoloader (no need to load it yet).
if(!class_exists($namespace, true)) {
throw new ApiLogicNotFoundException($namespace);
}

if(empty($method)) {
$this->scriptNamespace = $namespace;
}
else {
// With a method passed in, the path must be to an API Logic class
// with a method of name $method.
$this->scriptNamespace = $namespace
}
break;

case "sql":
throw new \Gt\Core\Exception\NotImplementedException();
break;

default:
throw new InvalidScriptTypeException($pathInfo["extension"]);
break;
}
var_dump($this->scriptPath);die("123");
}

}#
11 changes: 11 additions & 0 deletions src/Api/InvalidScriptTypeException.php
@@ -0,0 +1,11 @@
<?php
/**
*
*
* PHP.Gt (http://php.gt)
* @copyright Copyright Ⓒ 2014 Bright Flair Ltd. (http://brightflair.com)
* @license Apache Version 2.0, January 2004. http://www.apache.org/licenses
*/
namespace Gt\Api;

class InvalidScriptTypeException extends \Gt\Core\Exception\GtException {}#
35 changes: 35 additions & 0 deletions src/Core/Path.php
Expand Up @@ -170,4 +170,39 @@ public static function fixCase($path,
return $result;
}

/**
* From a given PHP script, return the correct fully qualified namespace from
* its location on disc, according to PSR-4.
*
* @param string $path Absolute path to PHP script
*
* @return string|bool A string containing the fully qualified namespace, or
* false if file cannot be found
*/
public static function getNamespace($path) {
$path = self::fixCase($path);

if(!file_exists($path)) {
return false;
}

$src = self::get(self::SRC);
if(strpos($path, $src) !== 0) {
return false;
}

$subPath = substr($path, strlen($src));
$pathInfo = pathinfo($subPath);

if(!empty($pathInfo["dirname"])
&& !empty($pathInfo["filename"])) {
$path = $pathInfo["dirname"];
$path .= "/" . $pathInfo["filename"];

return "\\" . APP_NAMESPACE . str_replace("/", "\\", $path);
}

return false;
}

}#
4 changes: 3 additions & 1 deletion src/Core/Start.php
Expand Up @@ -38,7 +38,9 @@ public function __construct($uri) {
"No App namespace defined. Are you sure you have a config.ini?");
}

$this->addAppAutoloader($appNamespace);
define("APP_NAMESPACE", $appNamespace);

$this->addAppAutoloader(APP_NAMESPACE);

$production = $config["app"]->production;
$this->setupEnvironment($config);
Expand Down
11 changes: 11 additions & 0 deletions src/Logic/ApiLogic.php
@@ -0,0 +1,11 @@
<?php
/**
*
* PHP.Gt (http://php.gt)
* @copyright Copyright Ⓒ 2014 Bright Flair Ltd. (http://brightflair.com)
* @license Apache Version 2.0, January 2004. http://www.apache.org/licenses
*/
namespace Gt\Logic;

abstract class ApiLogic extends Logic {
}#

0 comments on commit b792ac0

Please sign in to comment.