Skip to content

Commit

Permalink
Refactor code to improve readability and maintainability.
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Poyigi <[email protected]>
  • Loading branch information
sampoyigi committed Sep 9, 2023
1 parent 665334a commit 183d03a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 48 deletions.
2 changes: 1 addition & 1 deletion public/js/app.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion resources/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ window.$ = window.jQuery = require('jquery');
window.Popper = require('@popperjs/core');
window.bootstrap = require('bootstrap');
window.Cookies = require('js-cookie');
window.Alpine = require('alpinejs')
window.moment = require('moment');

window.Swal = require('sweetalert2/dist/sweetalert2.js');
Expand Down
5 changes: 5 additions & 0 deletions src/Flame/Database/DatabaseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Database\DatabaseManager;
use Illuminate\Database\DatabaseServiceProvider as BaseDatabaseServiceProvider;
use Illuminate\Database\DatabaseTransactionsManager;
use Illuminate\Database\Eloquent\Relations\Relation;

class DatabaseServiceProvider extends BaseDatabaseServiceProvider
{
Expand All @@ -30,6 +31,10 @@ public function boot()
parent::boot();

Media::observe(MediaObserver::class);

Relation::morphMap([
'media' => \Igniter\Flame\Database\Attach\Media::class,
]);
}

/**
Expand Down
20 changes: 10 additions & 10 deletions src/Main/Classes/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ public function getPath()
*/
public function getSourcePath()
{
return $this->path.$this->sourcePath;
return $this->path . $this->sourcePath;
}

/**
* @return string
*/
public function getAssetPath()
{
return $this->path.$this->sourcePath.$this->assetPath;
return $this->path . $this->assetPath;
}

/**
Expand All @@ -154,8 +154,8 @@ public function getPathsToPublish()
{
$result = [];
foreach ($this->config['publish-paths'] ?? [] as $path) {
if (File::isDirectory($this->path.$this->sourcePath.$this->assetPath.$path)) {
$result[$this->path.$this->sourcePath.$this->assetPath.$path] = public_path('vendor/'.$this->name);
if (File::isDirectory($this->path . $path)) {
$result[$this->path . $path] = public_path('vendor/' . $this->name);
}
}

Expand Down Expand Up @@ -205,8 +205,8 @@ public function screenshot($name)
{
foreach ($this->getFindInPaths() as $findInPath => $publicPath) {
foreach (ThemeModel::ICON_MIMETYPES as $extension => $mimeType) {
if (File::isFile($findInPath.'/'.$name.'.'.$extension)) {
$this->screenshot = $findInPath.'/'.$name.'.'.$extension;
if (File::isFile($findInPath . '/' . $name . '.' . $extension)) {
$this->screenshot = $findInPath . '/' . $name . '.' . $extension;
break 2;
}
}
Expand All @@ -229,7 +229,7 @@ public function getScreenshotData()
if (file_exists($file = $this->screenshot)) {
$extension = pathinfo($file, PATHINFO_EXTENSION);
if (!array_key_exists($extension, ThemeModel::ICON_MIMETYPES)) {
throw FlashException::error('Invalid theme icon file type in: '.$this->name.'. Only SVG and PNG images are supported');
throw FlashException::error('Invalid theme icon file type in: ' . $this->name . '. Only SVG and PNG images are supported');
}

$mimeType = ThemeModel::ICON_MIMETYPES[$extension];
Expand All @@ -248,11 +248,11 @@ public function isActive()

public function loadThemeFile()
{
if (File::exists($path = $this->getPath().'/theme.php')) {
if (File::exists($path = $this->getPath() . '/theme.php')) {
require $path;
}

if (File::exists($path = $this->getParentPath().'/theme.php')) {
if (File::exists($path = $this->getParentPath() . '/theme.php')) {
require $path;
}
}
Expand Down Expand Up @@ -283,7 +283,7 @@ public function getConfig()
$configCache = [];
$findInPaths = array_reverse(array_keys($this->getFindInPaths()));
foreach ($findInPaths as $findInPath) {
$config = File::exists($path = $findInPath.'/_meta/fields.php')
$config = File::exists($path = $findInPath . '/_meta/fields.php')
? File::getRequire($path) : [];

foreach (array_get($config, 'form', []) as $key => $definitions) {
Expand Down
61 changes: 27 additions & 34 deletions src/Main/Classes/ThemeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ public function addAssetsFromActiveThemeManifest(Assets $manager)
return;
}

if (File::exists($theme->getSourcePath().'/_meta/assets.json')) {
$manager->addFromManifest($theme->getSourcePath().'/_meta/assets.json');
if (File::exists($theme->getSourcePath() . '/_meta/assets.json')) {
$manager->addFromManifest($theme->getSourcePath() . '/_meta/assets.json');
} elseif ($theme->hasParent() && $parent = $theme->getParent()) {
$manager->addFromManifest($parent->getSourcePath().'/_meta/assets.json');
$manager->addFromManifest($parent->getSourcePath() . '/_meta/assets.json');
}
}

Expand Down Expand Up @@ -189,23 +189,16 @@ public function bootTheme(Theme $theme)
Igniter::loadResourcesFrom($path, $theme->getParent()->getName());
}

if ($theme->isActive()) {
if ($pathsToPublish = $theme->getPathsToPublish()) {
foreach (['laravel-assets', 'igniter-assets'] as $group) {
if (!array_key_exists($group, ServiceProvider::$publishGroups)) {
ServiceProvider::$publishGroups[$group] = [];
}

ServiceProvider::$publishGroups[$group] = array_merge(
ServiceProvider::$publishGroups[$group], $pathsToPublish
);
if ($pathsToPublish = $theme->getPathsToPublish()) {
foreach (['laravel-assets', 'igniter-assets'] as $group) {
if (!array_key_exists($group, ServiceProvider::$publishGroups)) {
ServiceProvider::$publishGroups[$group] = [];
}
}

if ($theme->hasParent() && $parent = $theme->getParent()) {
Igniter::loadViewsFrom($parent->getPath().'/'.Page::DIR_NAME, 'igniter.main');
ServiceProvider::$publishGroups[$group] = array_merge(
ServiceProvider::$publishGroups[$group], $pathsToPublish
);
}
Igniter::loadViewsFrom($theme->getPath().'/'.Page::DIR_NAME, 'igniter.main');
}
}

Expand Down Expand Up @@ -274,7 +267,7 @@ public function folders()
}

foreach ($directories as $directory) {
foreach (File::glob($directory.'/*/theme.json') as $path) {
foreach (File::glob($directory . '/*/theme.json') as $path) {
$paths[] = dirname($path);
}
}
Expand Down Expand Up @@ -340,8 +333,8 @@ public function checkParent($themeCode)

public function isLockedPath($path)
{
if (starts_with($path, Igniter::themesPath().'/')) {
$path = substr($path, strlen(Igniter::themesPath().'/'));
if (starts_with($path, Igniter::themesPath() . '/')) {
$path = substr($path, strlen(Igniter::themesPath() . '/'));
}

$themeCode = str_before($path, '/');
Expand Down Expand Up @@ -387,7 +380,7 @@ public function findFile($filename, $themeCode, $base = null)
}

foreach ($base as $folder) {
if (File::isFile($path = $themePath.$folder.$filename)) {
if (File::isFile($path = $themePath . $folder . $filename)) {
return $path;
}
}
Expand Down Expand Up @@ -421,7 +414,7 @@ public function newFile($filePath, $themeCode)
{
$theme = $this->findTheme($themeCode);
[$dirName, $fileName] = $this->getFileNameParts($filePath, $theme);
$path = $theme->getPath().'/'.$dirName.'/'.$fileName;
$path = $theme->getPath() . '/' . $dirName . '/' . $fileName;

if (File::isFile($path)) {
throw new SystemException("Theme template file already exists: $filePath");
Expand Down Expand Up @@ -479,8 +472,8 @@ public function renameFile($filePath, $newFilePath, $themeCode)
throw new SystemException(lang('igniter::system.themes.alert_theme_path_locked'));
}

$oldFilePath = $theme->path.'/'.$dirName.'/'.$fileName;
$newFilePath = $theme->path.'/'.$newDirName.'/'.$newFileName;
$oldFilePath = $theme->path . '/' . $dirName . '/' . $fileName;
$newFilePath = $theme->path . '/' . $newDirName . '/' . $newFileName;

if ($oldFilePath == $newFilePath) {
throw new SystemException("Theme template file already exists: $filePath");
Expand Down Expand Up @@ -600,11 +593,11 @@ public function createChildTheme($model)
}

$childThemeCode = $this->themeModel::generateUniqueCode($model->code);
$childThemePath = Igniter::themesPath().'/'.$childThemeCode;
$childThemePath = Igniter::themesPath() . '/' . $childThemeCode;

$themeConfig = [
'code' => $childThemeCode,
'name' => $parentTheme->label.' [child]',
'name' => $parentTheme->label . ' [child]',
'description' => $parentTheme->description,
];

Expand Down Expand Up @@ -633,12 +626,12 @@ public function createChildTheme($model)
*/
public function getMetaFromFile($path, $throw = true)
{
if (File::exists($metaPath = $path.'/theme.json')) {
if (File::exists($metaPath = $path . '/theme.json')) {
return json_decode(File::get($metaPath), true);
}

if ($throw) {
throw new SystemException('Theme does not have a registration file in: '.$metaPath);
throw new SystemException('Theme does not have a registration file in: ' . $metaPath);
}
}

Expand All @@ -661,11 +654,11 @@ public function getFileNameParts($path, Theme $theme)
protected function validateMetaFile($config, $code)
{
foreach ([
'code',
'name',
'description',
'author',
] as $item) {
'code',
'name',
'description',
'author',
] as $item) {
if (!array_key_exists($item, $config)) {
throw new SystemException(sprintf(
Lang::get('igniter::system.missing.config_key'),
Expand Down Expand Up @@ -698,6 +691,6 @@ protected function writeChildThemeMetaFile($path, $parentTheme, $themeConfig)

File::makeDirectory($path, 0777, false, true);

File::put($path.'/theme.json', json_encode($themeConfig, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
File::put($path . '/theme.json', json_encode($themeConfig, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
}
4 changes: 2 additions & 2 deletions src/Main/Template/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ public static function resolveRouteBinding($value, $field = null)

$page = self::find($value);

throw_unless($page, (new ModelNotFoundException)->setModel(Page::class));
throw_unless($page, (new ModelNotFoundException)->setModel(__CLASS__));

throw_if(!AdminAuth::check() && $page->isHidden, (new ModelNotFoundException)->setModel(Page::class));
throw_if(!AdminAuth::check() && $page->isHidden, (new ModelNotFoundException)->setModel(__CLASS__));

return $page;
}
Expand Down

0 comments on commit 183d03a

Please sign in to comment.