From 96a2b81603302c8a973c0f008b018d84962aa145 Mon Sep 17 00:00:00 2001 From: vanchelo Date: Sat, 10 Oct 2015 15:04:36 +0300 Subject: [PATCH] Fix handling php errors --- src/Console.php | 17 ++++++--------- src/ConsoleService.php | 4 +++- src/Controller/ControllerBase.php | 31 +++++----------------------- src/Controller/ExecuteController.php | 5 +++-- 4 files changed, 17 insertions(+), 40 deletions(-) diff --git a/src/Console.php b/src/Console.php index 0afcd37..ecc96d0 100644 --- a/src/Console.php +++ b/src/Console.php @@ -93,31 +93,26 @@ public function getProfile() * Executes a code and returns current profile. * * @param string $code - * - * @return array */ public function execute($code) { // Execute the code ob_start(); - $console_execute_start = microtime(true); - $estatus = @eval($code); - $console_execute_end = microtime(true); + $startTime = microtime(true); + @eval($code); + $endTime = microtime(true); $output = ob_get_contents(); ob_end_clean(); - // Retrieve an error - if ($estatus === false) { - $this->addProfile('error', error_get_last()); - } - // Extend the profile $this->addProfile([ - 'time' => round(($console_execute_end - $console_execute_start) * 1000, 2), + 'time' => round(($endTime - $startTime) * 10000, 3), 'output' => $output, 'output_size' => strlen($output), ]); + $this->addProfile('error', error_get_last()); + return $this->getProfile(); } diff --git a/src/ConsoleService.php b/src/ConsoleService.php index 874e92f..882e46f 100644 --- a/src/ConsoleService.php +++ b/src/ConsoleService.php @@ -45,7 +45,9 @@ protected function registerRoutes() protected function registerConsoleService() { - $this->di['console'] = 'Vanchelo\Console\Console'; + $this->di['console'] = function () { + return new Console(); + }; } protected function registerViewService() diff --git a/src/Controller/ControllerBase.php b/src/Controller/ControllerBase.php index ddd1f6a..ea8ed15 100644 --- a/src/Controller/ControllerBase.php +++ b/src/Controller/ControllerBase.php @@ -8,8 +8,6 @@ abstract class ControllerBase extends Controller { - protected $restful = false; - /** * Check access rights * @@ -24,24 +22,6 @@ public function beforeExecuteRoute(Dispatcher $dispatcher) } } - public function initialize() - { - $this->response->setContentType('text/html', 'UTF-8'); - if ($this->restful) { - $this->setJsonResponse(); - } - } - - /** - * Call this func to set json response enabled - */ - public function setJsonResponse() - { - $this->view->disable(); - - $this->response->setContentType('application/json', 'UTF-8'); - } - /** * After route executed event * @@ -51,14 +31,13 @@ public function afterExecuteRoute(Dispatcher $dispatcher) { $data = $dispatcher->getReturnedValue(); - if ($this->restful) { - if (is_array($data)) { - $data = json_encode($data, JSON_UNESCAPED_UNICODE); - } - + if (is_array($data)) { + $this->response->setJsonContent($data, JSON_UNESCAPED_UNICODE); + } elseif (is_scalar($data)) { + $this->response->setContent($data); } - $this->response->setContent($data); $this->response->send(); + exit(); } } \ No newline at end of file diff --git a/src/Controller/ExecuteController.php b/src/Controller/ExecuteController.php index b567fec..8a70e02 100644 --- a/src/Controller/ExecuteController.php +++ b/src/Controller/ExecuteController.php @@ -4,8 +4,9 @@ class ExecuteController extends ControllerBase { - protected $restful = true; - + /** + * @return array + */ public function indexAction() { $code = $this->request->getPost('code');