Skip to content

Commit

Permalink
Merge pull request #170 from mychidarko/v3.x-dev
Browse files Browse the repository at this point in the history
V3.x dev
  • Loading branch information
mychidarko authored Dec 25, 2022
2 parents 573dca9 + 9454288 commit 2a524c5
Showing 1 changed file with 47 additions and 47 deletions.
94 changes: 47 additions & 47 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct(array $userSettings = [])

if (!Anchor\CSRF::verify()) {
$csrfError = Anchor\CSRF::errors()['token'];
Http\Response::status(400);
Http\Headers::resetStatus(400);
echo Exception\General::csrf($csrfError);
exit();
}
Expand Down Expand Up @@ -156,26 +156,24 @@ private function setupDefaultContainer()
return new \Leaf\Http\Headers();
});

if ($this->config('log.enabled')) {
if (class_exists('Leaf\Log')) {
// Default log writer
$this->container->singleton('logWriter', function ($c) {
$logWriter = Config::get('log.writer');
if ($this->config('log.enabled') && class_exists('Leaf\Log')) {
// Default log writer
$this->container->singleton('logWriter', function ($c) {
$logWriter = Config::get('log.writer');

$file = $this->config('log.dir') . $this->config('log.file');
$file = $this->config('log.dir') . $this->config('log.file');

return is_object($logWriter) ? $logWriter : new \Leaf\LogWriter($file, $this->config('log.open') ?? true);
});
return is_object($logWriter) ? $logWriter : new \Leaf\LogWriter($file, $this->config('log.open') ?? true);
});

// Default log
$this->container->singleton('log', function ($c) {
$log = new \Leaf\Log($c->logWriter);
$log->enabled($this->config('log.enabled'));
$log->level($this->config('log.level'));
// Default log
$this->container->singleton('log', function ($c) {
$log = new \Leaf\Log($c->logWriter);
$log->enabled($this->config('log.enabled'));
$log->level($this->config('log.level'));

return $log;
});
}
return $log;
});
}

// Default mode
Expand All @@ -189,16 +187,6 @@ private function setupDefaultContainer()
$mode = _env('LEAF_MODE');
}

if (isset($_ENV['LEAF_MODE'])) {
$mode = $_ENV['LEAF_MODE'];
} else {
$envMode = getenv('LEAF_MODE');

if ($envMode !== false) {
$mode = $envMode;
}
}

Config::set([
'mode' => $mode,
'app' => [
Expand Down Expand Up @@ -282,6 +270,36 @@ public function setResponseClass($class)
});
}

/**
* Evade CORS errors
*
* @param $options Config for cors
*/
public function cors($options = [])
{
if (class_exists('Leaf\Http\Cors')) {
Http\Cors::config($options);
} else {
trigger_error('Cors module not found! Run `leaf install cors` or `composer require leafs/cors` to install the CORS module. This is required to configure CORS.');
}
}

/**
* Create a route handled by websocket (requires Eien module)
*
* @param string $name The url of the route
* @param callable $callback The callback function
* @uses package Eien module
* @see https://leafphp.dev/modules/eien/
*/
public function ws(string $name, callable $callback)
{
Config::set('eien.events', array_merge(
Config::get('eien.events') ?? [],
[$name => $callback]
));
}

/********************************************************************************
* Logging
*******************************************************************************/
Expand Down Expand Up @@ -393,26 +411,8 @@ public static function halt($status, $message = '')
ob_clean();
}

Http\Headers::status($status);
Http\Response::markup($message);

exit();
}

/**
* Evade CORS errors
*
* Cors handler
*
* @param $options Config for cors
*/
public function cors($options = [])
{
if (class_exists('Leaf\Http\Cors')) {
Http\Cors::config($options);
} else {
trigger_error('Cors module not found! Run `composer require leafs/cors` to install the CORS module. This is required to configure CORS.');
}
Http\Headers::resetStatus($status);
response()->exit($message, $status);
}

/**
Expand Down

0 comments on commit 2a524c5

Please sign in to comment.