Skip to content

Commit

Permalink
Fix handling php errors
Browse files Browse the repository at this point in the history
  • Loading branch information
vanchelo committed Oct 10, 2015
1 parent 35aef57 commit 96a2b81
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 40 deletions.
17 changes: 6 additions & 11 deletions src/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
4 changes: 3 additions & 1 deletion src/ConsoleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
31 changes: 5 additions & 26 deletions src/Controller/ControllerBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

abstract class ControllerBase extends Controller
{
protected $restful = false;

/**
* Check access rights
*
Expand All @@ -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
*
Expand All @@ -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();
}
}
5 changes: 3 additions & 2 deletions src/Controller/ExecuteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

class ExecuteController extends ControllerBase
{
protected $restful = true;

/**
* @return array
*/
public function indexAction()
{
$code = $this->request->getPost('code');
Expand Down

0 comments on commit 96a2b81

Please sign in to comment.