Skip to content

Commit

Permalink
Merge pull request #22 from cuongnd88/v1.1.9
Browse files Browse the repository at this point in the history
update Request & Controller
  • Loading branch information
cuongdinhngo authored Nov 1, 2020
2 parents e997570 + f4e5d0d commit 2b9c0c7
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 18 deletions.
7 changes: 3 additions & 4 deletions src/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ class Controller
* Controller construct
* @param Request|null $request
*/
public function __construct(Request $request = null)
public function __construct()
{
$this->request = $request ?? new Request();
$this->container = new Container();
$this->requestMethod = Globals::method();
}
Expand Down Expand Up @@ -98,8 +97,8 @@ public function callMethod($method)
}

$methodReflection = (new ReflectionObject($this))->getMethod($method);
$result = $methodReflection->invoke($this);

$dependencies = $this->container->getDependencies($methodReflection->getParameters());
$result = $methodReflection->invokeArgs($this, $dependencies);
if ($result === false) {
throw new ControllerException(ControllerException::ERR_MSG_ACTION_FAIL);
}
Expand Down
117 changes: 103 additions & 14 deletions src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
namespace Atom\Http;

use Atom\Http\Globals;
use ArrayAccess;

class Request
class Request implements ArrayAccess
{
/**
* Request
* @var array
*/
public $request;
public $request = [];

/**
* URI
Expand Down Expand Up @@ -52,16 +53,7 @@ public function __construct()
$this->get = Globals::get();
$this->post = Globals::post();
$this->files = Globals::files();
}

/**
* Create request
* @return $this
*/
public function create()
{
$this->request = $this->collectParameters();
return $this;
$this->request = (array) $this->collectParameters();
}

/**
Expand All @@ -70,7 +62,7 @@ public function create()
*/
public function all()
{
return (array) $this->collectParameters();
return $this->request;
}

/**
Expand Down Expand Up @@ -124,7 +116,8 @@ public function getRawData()
if (json_last_error() === JSON_ERROR_NONE) {
return $data;
}
return [$content];
parse_str($content, $data);
return $data;
}

/**
Expand Down Expand Up @@ -183,4 +176,100 @@ public function compileParameters($params)
parse_str($params, $compileParams);
return $compileParams;
}

/**
* Get a data by key
*
* @param string The key data to retrieve
* @access public
*/
public function &__get ($key) {
return $this->request[$key];
}

/**
* Assigns a value to the specified data
*
* @param string The data key to assign the value to
* @param mixed The value to set
* @access public
*/
public function __set($key,$value) {
$this->request[$key] = $value;
}

/**
* Whether or not an data exists by key
*
* @param string An data key to check for
* @access public
* @return boolean
* @abstracting ArrayAccess
*/
public function __isset ($key) {
return isset($this->request[$key]);
}

/**
* Unsets an data by key
*
* @param string The key to unset
* @access public
*/
public function __unset($key) {
unset($this->request[$key]);
}

/**
* Assigns a value to the specified offset
*
* @param string The offset to assign the value to
* @param mixed The value to set
* @access public
* @abstracting ArrayAccess
*/
public function offsetSet($offset,$value) {
if (is_null($offset)) {
$this->request[] = $value;
} else {
$this->request[$offset] = $value;
}
}

/**
* Whether or not an offset exists
*
* @param string An offset to check for
* @access public
* @return boolean
* @abstracting ArrayAccess
*/
public function offsetExists($offset) {
return isset($this->request[$offset]);
}

/**
* Unsets an offset
*
* @param string The offset to unset
* @access public
* @abstracting ArrayAccess
*/
public function offsetUnset($offset) {
if ($this->offsetExists($offset)) {
unset($this->request[$offset]);
}
}

/**
* Returns the value at specified offset
*
* @param string The offset to retrieve
* @access public
* @return mixed
* @abstracting ArrayAccess
*/
public function offsetGet($offset) {
return $this->offsetExists($offset) ? $this->request[$offset] : null;
}
}

0 comments on commit 2b9c0c7

Please sign in to comment.