Skip to content

Commit

Permalink
lunch time effort
Browse files Browse the repository at this point in the history
  • Loading branch information
rmahoney-bl committed Nov 8, 2016
1 parent 8648efd commit 9e9de27
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@
"role" : "Developer"
}
],
"minimum-stability ": "stable",
"require": {
"php" : ">=7.0.0",
"nikic/fast-route": "1.0.1",
"opine/interfaces": "^1.0",
"opine/config" : "^3.0",
"opine/container" : "^2.0",
"opine/bundle" : "^2.0.17"
"opine/bundle" : "^2.0.17",
"opine/user" : "^1.0.1"
},
"require-dev": {
"phpunit/phpunit": "5.5.0"
},
"version": "3.0.14",
"autoload": {
"psr-4": {
"Opine\\Route\\": "src/"
Expand Down
1 change: 1 addition & 0 deletions config/containers/test-container.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
imports:
- package-container.yml
- ../../vendor/opine/bundle/config/containers/package-container.yml
- ../../vendor/opine/user/config/containers/package-container.yml

services:
controller:
Expand Down
2 changes: 1 addition & 1 deletion docker/compose.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
docker run --rm -v "$(pwd)/../":/app composer/composer install
docker run --rm -v "$(pwd)/../":/app composer/composer install
43 changes: 30 additions & 13 deletions src/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function after($callback)
return $this;
}

private function actionPrepare($callback)
private function actionPrepare(string $callback)
{
$this->stringToCallback($callback);
$this->arrayToService($callback);
Expand Down Expand Up @@ -186,11 +186,7 @@ private function stringToCallback(&$callback)
if (!is_string($callback)) {
return;
}
if (substr_count($callback, '@') == 1) {
$callback = explode('@', $callback);
} else {
throw new \Opine\Route\Exception('Invalid callback: '.$callback);
}
$callback = explode('@', $callback, 2);
}

private function arrayToService(&$callback)
Expand Down Expand Up @@ -248,15 +244,18 @@ private function dispatcher()

private function filterParse(Array &$callable, &$beforeActions = [], &$afterActions = [], &$activity)
{
$parts = explode('|', $callable);
$options = json_decode($parts[1]);
$callable = $parts[0];
if (substr_count($callable[1], '|') == 0) {
return;
}
$parts = explode('|', $callable[1]);
$options = json_decode($parts[1], true);
$callable = [$callable[0], $parts[0]];

if (isset($options['before'])) {
$beforeActions[] = $options['before'];
$beforeActions[] = $this->actionPrepare($options['before']);
}
if (isset($options['after'])) {
$afterActions[] = $options['after'];
$afterActions[] = $this->actionPrepare($options['after']);
}
if (isset($options['activity'])) {
$activity = $options['activity'];
Expand Down Expand Up @@ -293,7 +292,24 @@ public function execute(Array $callable, Array $parameters = [], Array $beforeAc
{
$beforeActions = array_merge($this->before, $beforeActionsIn);
$afterActions = array_merge($this->after, $afterActionsIn);
$this->filterParse($callable, $beforeActions, $afterActions);
$activity = null;
$this->filterParse($callable, $beforeActions, $afterActions, $activity);

// handle activity
if (!empty($activity)) {
$userService = $this->container->get('userService');
$authorized = $userService->checkActivity($activity);
if (!$authorized['authorized']) {
$redirect = '/';
if (!empty($authorized['redirect'])) {
$redirect = $authorized['redirect'];
}
http_response_code(302);
header('Location: '. $redirect);
return false;
}
}

foreach ($beforeActions as $before) {
if (!is_object($before[0])) {
$before[0] = new $before[0]();
Expand Down Expand Up @@ -384,7 +400,8 @@ public function runNamed($name, Array $parameters = [])
$beforeActions = [];
$afterActions = [];
$action = $this->namedRoutes[$name];
$this->filterParse($action, $beforeActions, $afterActions);
$activity = null;
$this->filterParse($action, $beforeActions, $afterActions, $activity);
if (count($parameters) == 0) {
$this->execute($action, $parameters, $beforeActions, $afterActions);
} else {
Expand Down
2 changes: 1 addition & 1 deletion tests/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ private function initializeRoutes()

public function testRouteWithStringController()
{
$header = '';
$this->initializeRoutes();
$header = '';
$response = $this->route->run('GET', '/sample', $header);
$this->assertTrue($response == 'SAMPLE' && $header == 200);
}
Expand Down

0 comments on commit 9e9de27

Please sign in to comment.