Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/App/Adapters/WebAppAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function start(): ?int

[$request, $response] = (new MiddlewareManager($matchedRoute))->applyMiddlewares(request(), response());

if ($this->setupViewCache()->serveCachedView(route_uri() ?? '', $response)) {
if ($this->setupViewCache()->serveCachedView(request()->getUri() ?? '', $response)) {
stop();
}

Expand Down
25 changes: 0 additions & 25 deletions src/App/Helpers/misc.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,31 +82,6 @@ function is_debug_mode(): bool
return filter_var(config()->get('app.debug'), FILTER_VALIDATE_BOOLEAN);
}

/**
* Gets the caller class
*/
function get_caller_class(int $index = 2): ?string
{
$caller = debug_backtrace();
$caller = $caller[$index];

return $caller['class'] ?? null;
}

/**
* Gets the caller function
*/
function get_caller_function(int $index = 2): ?string
{
$trace = debug_backtrace();

if (!isset($trace[$index])) {
return null;
}

return $trace[$index]['function'];
}

/**
* Exports the variable
* @param mixed $var
Expand Down
4 changes: 2 additions & 2 deletions src/Console/Commands/OpenApiCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ public function exec(): void
return;
}

if (route_group_exists('openapi', $module) && $this->fs->exists($modulePath . DS . 'resources' . DS . 'openApi' . DS . 'spec.json')) {
if (request()->routeGroupExists('openapi', $module) && $this->fs->exists($modulePath . DS . 'resources' . DS . 'openApi' . DS . 'spec.json')) {
$this->error('The Open API specifications already installed for `' . ucfirst($module) . '` module');
return;
}

if (!route_group_exists('openapi', $module)) {
if (!request()->routeGroupExists('openapi', $module)) {
$routeContent = $this->fs->get($routes);

if ($routeContent !== false) {
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public function getBaseUrl(bool $withModulePrefix = false): string
{
$baseUrl = config()->get('app.base_url');

$prefix = route_prefix();
$prefix = $this->getRoutePrefix();
$modulePrefix = ($withModulePrefix && !in_array($prefix, [null, '', '0'], true)) ? '/' . $prefix : '';

if ($baseUrl) {
Expand Down
126 changes: 126 additions & 0 deletions src/Http/Traits/Request/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

namespace Quantum\Http\Traits\Request;

use Quantum\Router\Route as RouterRoute;
use Quantum\Router\RouteCollection;
use Quantum\Router\MatchedRoute;
use Quantum\Di\Di;
use Closure;

/**
* Trait Route
Expand All @@ -36,4 +40,126 @@ public function getMatchedRoute(): ?MatchedRoute
return $this->route;
}

/**
* @return array<int|string, mixed>|null
*/
public function getCurrentMiddlewares(): ?array
{
return $this->route ? $this->route->getRoute()->getMiddlewares() : null;
}

public function getCurrentModule(): ?string
{
return $this->route ? $this->route->getRoute()->getModule() : null;
}

public function getCurrentController(): ?string
{
return $this->route ? $this->route->getRoute()->getController() : null;
}

public function getCurrentAction(): ?string
{
return $this->route ? $this->route->getRoute()->getAction() : null;
}

public function getRouteCallback(): ?Closure
{
return $this->route ? $this->route->getRoute()->getClosure() : null;
}

public function getCurrentRoutePattern(): ?string
{
return $this->route ? $this->route->getRoute()->getPattern() : null;
}

public function getCompiledRoutePattern(): ?string
{
return $this->route ? $this->route->getRoute()->getCompiledPattern() : null;
}

/**
* @return array<string, mixed>
*/
public function getRouteParams(): array
{
return $this->route ? $this->route->getParams() : [];
}

/**
* @return mixed|null
*/
public function getRouteParam(string $name)
{
$params = $this->getRouteParams();

return $params[$name] ?? null;
}

/**
* @return array<string, mixed>|null
*/
public function getRouteCacheSettings(): ?array
{
return $this->route ? $this->route->getRoute()->getCache() : null;
}

public function getRouteName(): ?string
{
return $this->route ? $this->route->getRoute()->getName() : null;
}

public function getRoutePrefix(): ?string
{
return $this->route ? $this->route->getRoute()->getPrefix() : null;
}

public function findRouteByName(string $name, string $module): ?RouterRoute
{
if (!Di::isRegistered(RouteCollection::class)) {
return null;
}

$collection = Di::get(RouteCollection::class);

foreach ($collection->all() as $route) {
if (
$route->getName() !== null &&
strcasecmp($route->getName(), $name) === 0 &&
strcasecmp((string) $route->getModule(), $module) === 0
) {
return $route;
}
}

return null;
}

public function routeGroupExists(string $name, string $module): bool
{
if (!Di::isRegistered(RouteCollection::class)) {
return false;
}

$collection = Di::get(RouteCollection::class);

foreach ($collection->all() as $route) {
if (
$route->getGroup() !== null &&
strcasecmp($route->getGroup(), $name) === 0 &&
strcasecmp((string) $route->getModule(), $module) === 0
) {
return true;
}
}

return false;
}

public function getModuleBaseNamespace(): string
{
return environment()->isTesting()
? 'Quantum\\Tests\\_root\\modules'
: 'Modules';
}
}
2 changes: 1 addition & 1 deletion src/Lang/Factories/LangFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private function getLangFromUrlSegment(array $supported): ?string
{
$segmentIndex = (int) config()->get('lang.url_segment');

if (!in_array(route_prefix(), [null, '', '0'], true) && $segmentIndex === 1) {
if (!in_array(request()->getRoutePrefix(), [null, '', '0'], true) && $segmentIndex === 1) {
$segmentIndex++;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Lang/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function loadTranslations(): void
$loaded = true;
}

$moduleDir = modules_dir() . DS . current_module() . DS . 'resources' . DS . 'lang' . DS . $this->lang;
$moduleDir = modules_dir() . DS . request()->getCurrentModule() . DS . 'resources' . DS . 'lang' . DS . $this->lang;
$moduleFiles = fs()->glob($moduleDir . DS . '*.php');

if (is_array($moduleFiles) && count($moduleFiles)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Loader/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(?string $pathPrefix = null, ?string $fileName = null
$this->pathPrefix = $pathPrefix;
$this->fileName = $fileName;
$this->hierarchical = $hierarchical;
$this->module = $module ?: current_module();
$this->module = $module ?: request()->getCurrentModule();
$this->exceptionMessage = $exceptionMessage ?: 'File `' . $pathPrefix . DS . $fileName . '` not found!';
}

Expand Down
7 changes: 4 additions & 3 deletions src/Middleware/MiddlewareManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Quantum\Router\MatchedRoute;
use Quantum\Http\Response;
use Quantum\Http\Request;
use ReflectionException;

/**
* Class MiddlewareManager
Expand Down Expand Up @@ -50,7 +51,7 @@ public function __construct(MatchedRoute $matchedRoute)
/**
* Apply Middlewares
* @return array{0: Request, 1: Response}
* @throws MiddlewareException|BaseException
* @throws MiddlewareException|BaseException|ReflectionException
*/
public function applyMiddlewares(Request $request, Response $response): array
{
Expand All @@ -70,11 +71,11 @@ public function applyMiddlewares(Request $request, Response $response): array

/**
* Loads and gets the current middleware instance
* @throws MiddlewareException|BaseException
* @throws MiddlewareException|BaseException|ReflectionException
*/
private function getMiddleware(Request $request, Response $response): QtMiddleware
{
$middlewareClass = module_base_namespace() . '\\' . $this->module . '\\Middlewares\\' . current($this->middlewares);
$middlewareClass = request()->getModuleBaseNamespace() . '\\' . $this->module . '\\Middlewares\\' . current($this->middlewares);

if (!class_exists($middlewareClass)) {
throw MiddlewareException::middlewareNotFound($middlewareClass);
Expand Down
8 changes: 7 additions & 1 deletion src/Module/ModuleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ private function copyDirectory(string $src, string $dst, bool $processTemplates,
return $copiedFiles;
}

/**
* @throws DiException|ReflectionException
*/
private function processTemplates(string $srcPath, string &$dstPath): void
{
$dstPath = str_replace('.php.tpl', '.php', $dstPath);
Expand All @@ -194,10 +197,13 @@ private function processTemplates(string $srcPath, string &$dstPath): void
$this->fs->put($dstPath, $processedContent);
}

/**
* @throws DiException|ReflectionException
*/
private function replacePlaceholders(string $content): string
{
$placeholders = [
'{{MODULE_NAMESPACE}}' => module_base_namespace() . '\\' . $this->getModuleName(),
'{{MODULE_NAMESPACE}}' => request()->getModuleBaseNamespace() . '\\' . $this->getModuleName(),
'{{MODULE_NAME}}' => $this->getModuleName(),
];

Expand Down
2 changes: 1 addition & 1 deletion src/Paginator/Traits/PaginatorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public function getPagination(bool $withBaseUrl = false, ?int $pageItemsCount =
*/
protected function getUri(bool $withBaseUrl = false): string
{
$routeUrl = preg_replace('/([?&](page|per_page)=\d+)/', '', route_uri() ?? '');
$routeUrl = preg_replace('/([?&](page|per_page)=\d+)/', '', request()->getUri() ?? '');
$routeUrl = preg_replace('/&/', '?', $routeUrl ?? '', 1);
$url = $routeUrl;

Expand Down
2 changes: 1 addition & 1 deletion src/Renderer/Adapters/HtmlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function render(string $view, array $params = []): string
*/
private function getViewFilePath(string $view): string
{
$moduleViewPath = modules_dir() . DS . current_module() . DS . 'Views' . DS . $view . '.php';
$moduleViewPath = modules_dir() . DS . request()->getCurrentModule() . DS . 'Views' . DS . $view . '.php';
$sharedViewPath = base_dir() . DS . 'shared' . DS . 'views' . DS . $view . '.php';

return $this->fs->exists($moduleViewPath) ? $moduleViewPath : $sharedViewPath;
Expand Down
2 changes: 1 addition & 1 deletion src/Renderer/Adapters/TwigAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function render(string $view, array $params = []): string
*/
private function getLoader(string $view): FilesystemLoader
{
$moduleViewPath = modules_dir() . DS . current_module() . DS . 'Views' . DS . $view . '.php';
$moduleViewPath = modules_dir() . DS . request()->getCurrentModule() . DS . 'Views' . DS . $view . '.php';
$sharedViewPath = base_dir() . DS . 'shared' . DS . 'views' . DS . $view . '.php';

if ($this->fs->exists($moduleViewPath)) {
Expand Down
2 changes: 1 addition & 1 deletion src/ResourceCache/ViewCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private function getCacheDir(): string

$viewCacheDir = base_dir() . DS . $configCacheDir . DS . 'views';

if ($module = current_module()) {
if ($module = request()->getCurrentModule()) {
$viewCacheDir = base_dir() . DS . $configCacheDir . DS . 'views' . DS . strtolower($module);
}

Expand Down
Loading
Loading