sarga/packages/Webkul/Theme/src/ThemeViewFinder.php

116 lines
3.3 KiB
PHP
Raw Normal View History

2020-03-19 11:09:29 +00:00
<?php
2018-08-17 05:48:21 +00:00
namespace Webkul\Theme;
use Webkul\Theme\Facades\Themes;
use Illuminate\Support\Arr;
2020-08-10 13:55:51 +00:00
use Illuminate\Support\Str;
2018-08-17 05:48:21 +00:00
use Illuminate\View\FileViewFinder;
class ThemeViewFinder extends FileViewFinder
{
2020-03-05 13:37:08 +00:00
/**
2018-08-17 05:48:21 +00:00
* Override findNamespacedView() to add "resources/themes/theme_name/views/..." paths
*
* @param string $name
* @return string
*/
protected function findNamespacedView($name)
{
// Extract the $view and the $namespace parts
list($namespace, $view) = $this->parseNamespaceSegments($name);
2020-03-19 11:09:29 +00:00
if (request()->route() !== null && ! Str::contains(request()->route()->uri, config('app.admin_url') . '/')) {
$paths = $this->addThemeNamespacePaths($namespace);
2018-08-17 05:48:21 +00:00
2020-02-28 13:51:16 +00:00
try {
return $this->findInPaths($view, $paths);
} catch(\Exception $e) {
2020-08-18 09:15:53 +00:00
if ($namespace !== 'shop') {
2020-02-28 13:51:16 +00:00
if (strpos($view, 'shop.') !== false) {
$view = str_replace('shop.', 'shop.' . Themes::current()->code . '.', $view);
}
}
return $this->findInPaths($view, $paths);
}
} else {
2020-08-10 13:55:51 +00:00
$themes = app('themes');
$themes->set(config('themes.admin-default'));
$paths = $this->addThemeNamespacePaths($namespace);
try {
return $this->findInPaths($view, $paths);
} catch(\Exception $e) {
if ($namespace != 'admin') {
if (strpos($view, 'admin.') !== false) {
$view = str_replace('admin.', 'admin.' . Themes::current()->code . '.', $view);
}
}
return $this->findInPaths($view, $paths);
}
}
2018-08-17 05:48:21 +00:00
}
2020-03-05 13:37:08 +00:00
/**
* @param string $namespace
* @return array
*/
2018-08-17 05:48:21 +00:00
public function addThemeNamespacePaths($namespace)
{
2019-01-15 11:54:41 +00:00
if (! isset($this->hints[$namespace])) {
2018-08-17 05:48:21 +00:00
return [];
}
$paths = $this->hints[$namespace];
$searchPaths = array_diff($this->paths, Themes::getLaravelViewPaths());
foreach (array_reverse($searchPaths) as $path) {
$newPath = base_path() . '/' . $path;
$paths = Arr::prepend($paths, $newPath);
}
return $paths;
}
/**
* Override replaceNamespace() to add path for custom error pages "resources/themes/theme_name/views/errors/..."
*
2020-03-19 11:09:29 +00:00
* @param string $namespace
2018-08-17 05:48:21 +00:00
* @param string|array $hints
* @return void
*/
public function replaceNamespace($namespace, $hints)
{
$this->hints[$namespace] = (array) $hints;
2020-03-19 11:09:29 +00:00
2018-08-17 05:48:21 +00:00
// Overide Error Pages
if ($namespace == 'errors' || $namespace == 'mails') {
$searchPaths = array_diff($this->paths, Themes::getLaravelViewPaths());
$addPaths = array_map(function ($path) use ($namespace) {
return base_path() . '/' . "$path/$namespace";
}, $searchPaths);
$this->prependNamespace($namespace, $addPaths);
}
}
/**
* Set the array of paths where the views are being searched.
*
* @param array $paths
2020-03-05 13:37:08 +00:00
* @return void
2018-08-17 05:48:21 +00:00
*/
public function setPaths($paths)
{
$this->paths = $paths;
2020-02-28 13:51:16 +00:00
2018-08-17 05:48:21 +00:00
$this->flush();
}
}