Skip to content

Commit

Permalink
Merge pull request #144 from lara-zeus/queries
Browse files Browse the repository at this point in the history
improve queries
  • Loading branch information
atmonshi authored Sep 1, 2023
2 parents 83d687b + 28ce4ba commit edd33fa
Show file tree
Hide file tree
Showing 23 changed files with 68 additions and 55 deletions.
2 changes: 1 addition & 1 deletion database/seeders/SkySeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class SkySeeder extends Seeder
{
public function run()
public function run(): void
{
SkyPlugin::get()->getTagModel()::create(['name' => ['en' => 'laravel', 'ar' => 'لارافل'], 'type' => 'category']);
SkyPlugin::get()->getTagModel()::create(['name' => ['en' => 'talks', 'ar' => 'اخبار'], 'type' => 'category']);
Expand Down
3 changes: 1 addition & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ parameters:
level: 6
paths:
- src
- config
- database
- tests
tmpDir: build/phpstan
checkOctaneCompatibility: true
checkModelProperties: true
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

$filament = app('filament');

if (app('filament')->hasPlugin('zeus-sky')) {
if (! defined('__PHPSTAN_RUNNING__') && app('filament')->hasPlugin('zeus-sky')) {
Route::prefix(SkyPlugin::get()->getSkyPrefix())
->middleware(SkyPlugin::get()->getMiddleware())
->group(function () {
Expand Down
4 changes: 2 additions & 2 deletions src/Classes/BoltParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace LaraZeus\Sky\Classes;

use Illuminate\Support\Facades\Blade;
use LaraZeus\Bolt\BoltPlugin;

class BoltParser
{
Expand All @@ -14,7 +13,8 @@ public function __invoke(string $content): string
preg_match('/<bolt>(.*?)<\/bolt>/s', $content, $bolt);
if (is_array($bolt) && isset($bolt[1])) {
$formSlug = trim($bolt[1]);
$checkForm = BoltPlugin::getModel('Form')::where('slug', $formSlug)->first();
// @phpstan-ignore-next-line
$checkForm = \LaraZeus\Bolt\BoltPlugin::getModel('Form')::where('slug', $formSlug)->first();
if ($checkForm !== null) {
$boltComponent = Blade::render('<livewire:bolt.fill-form inline="true" slug="' . $formSlug . '" />');
$content = str_replace('<bolt>' . $formSlug . '</bolt>', $boltComponent, $content);
Expand Down
2 changes: 1 addition & 1 deletion src/Classes/RenderNavItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class RenderNavItem
{
public static function render($item, $class = '')
public static function render(array $item, string $class = ''): string
{
if ($item['type'] === 'page-link') {
$page = SkyPlugin::get()->getPostModel()::page()->find($item['data']['page_id']) ?? '';
Expand Down
4 changes: 1 addition & 3 deletions src/Console/PublishCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ class PublishCommand extends Command

/**
* Execute the console command.
*
* @return void
*/
public function handle()
public function handle(): void
{
// publish Sky files
$this->callSilent('vendor:publish', ['--tag' => 'zeus-sky-migrations', '--force' => $this->option('force')]);
Expand Down
6 changes: 2 additions & 4 deletions src/Console/migrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ class migrateCommand extends Command

/**
* Execute the console command.
*
* @return void
*/
public function handle()
public function handle(): void
{
$posts = SkyPlugin::get()->getPostModel()::get();
foreach ($posts as $post) {
Expand Down Expand Up @@ -55,7 +53,7 @@ public function handle()
}
}

public function isJson($string)
public function isJson(string $string): bool
{
json_decode($string);

Expand Down
3 changes: 2 additions & 1 deletion src/Http/Livewire/Faq.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace LaraZeus\Sky\Http\Livewire;

use Illuminate\View\View;
use LaraZeus\Sky\SkyPlugin;
use Livewire\Component;

class Faq extends Component
{
public function render()
public function render(): View
{
seo()
->site(config('zeus.site_title', 'Laravel'))
Expand Down
8 changes: 5 additions & 3 deletions src/Http/Livewire/LibrarTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@

namespace LaraZeus\Sky\Http\Livewire;

use Illuminate\View\View;
use LaraZeus\Sky\Models\Tag;
use LaraZeus\Sky\SkyPlugin;
use Livewire\Component;

class LibrarTag extends Component
{
public $tag;
public Tag $tag;

public function mount($slug)
public function mount(string $slug): void
{
$this->tag = SkyPlugin::get()->getTagModel()::findBySlug($slug, 'library');

abort_if($this->tag === null, 404);
}

public function render()
public function render(): View
{
seo()
->site(config('zeus.site_title', 'Laravel'))
Expand Down
3 changes: 2 additions & 1 deletion src/Http/Livewire/Library.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace LaraZeus\Sky\Http\Livewire;

use Illuminate\View\View;
use LaraZeus\Sky\SkyPlugin;
use Livewire\Component;

class Library extends Component
{
public function render()
public function render(): View
{
seo()
->site(config('zeus.site_title', 'Laravel'))
Expand Down
7 changes: 4 additions & 3 deletions src/Http/Livewire/LibraryItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

namespace LaraZeus\Sky\Http\Livewire;

use Illuminate\View\View;
use LaraZeus\Sky\SkyPlugin;
use Livewire\Component;

class LibraryItem extends Component
{
public $item;
public \LaraZeus\Sky\Models\Library $item;

public function mount($slug)
public function mount(string $slug): void
{
$this->item = SkyPlugin::get()->getLibraryModel()::where('slug', $slug)->firstOrFail();
}

public function render()
public function render(): View
{
seo()
->site(config('zeus.site_title', 'Laravel'))
Expand Down
7 changes: 4 additions & 3 deletions src/Http/Livewire/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

namespace LaraZeus\Sky\Http\Livewire;

use Illuminate\View\View;
use LaraZeus\Sky\SkyPlugin;
use Livewire\Component;

class Page extends Component
{
public $page;
public \LaraZeus\Sky\Models\Post $page;

public function mount($slug)
public function mount(string $slug): void
{
$this->page = SkyPlugin::get()->getPostModel()::where('slug', $slug)->page()->firstOrFail();
}

public function render()
public function render(): View
{
$this->setSeo();

Expand Down
7 changes: 4 additions & 3 deletions src/Http/Livewire/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

namespace LaraZeus\Sky\Http\Livewire;

use Illuminate\View\View;
use LaraZeus\Sky\SkyPlugin;
use Livewire\Component;

class Post extends Component
{
public $post;
public \LaraZeus\Sky\Models\Post $post;

public function mount($slug)
public function mount(string $slug): void
{
$this->post = SkyPlugin::get()->getPostModel()::where('slug', $slug)->firstOrFail();
}

public function render()
public function render(): View
{
$this->setSeo();

Expand Down
23 changes: 15 additions & 8 deletions src/Http/Livewire/Posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,30 @@

namespace LaraZeus\Sky\Http\Livewire;

use Illuminate\View\View;
use LaraZeus\Sky\SkyPlugin;
use Livewire\Component;

class Posts extends Component
{
use SearchHelpers;

public function render()
public function render(): View
{
$search = request('search');
$category = request('category');

$posts = SkyPlugin::get()->getPostModel()::NotSticky()
->search($search)
->with(['tags', 'author', 'media'])
->forCategory($category)
->published()
->orderBy('published_at', 'desc')
->get();

$pages = SkyPlugin::get()->getPostModel()::page()
->search($search)
->with(['tags', 'author', 'media'])
->forCategory($category)
->orderBy('published_at', 'desc')
->whereNull('parent_id')
Expand All @@ -33,6 +36,7 @@ public function render()

$recent = SkyPlugin::get()->getPostModel()::posts()
->published()
->with(['tags', 'author', 'media'])
->limit(SkyPlugin::get()->getRecentPostsLimit())
->orderBy('published_at', 'desc')
->get();
Expand All @@ -46,13 +50,16 @@ public function render()
->withUrl()
->twitter();

return view(app('skyTheme') . '.home')->with([
'posts' => $posts,
'pages' => $pages,
'recent' => $recent,
'tags' => SkyPlugin::get()->getTagModel()::withCount('postsPublished')->where('type', 'category')->get(),
'stickies' => SkyPlugin::get()->getPostModel()::sticky()->published()->get(),
])
return view(app('skyTheme') . '.home')
->with([
'posts' => $posts,
'pages' => $pages,
'recent' => $recent,
'tags' => SkyPlugin::get()->getTagModel()::withCount('postsPublished')
->where('type', 'category')
->get(),
'stickies' => SkyPlugin::get()->getPostModel()::with(['author', 'media'])->sticky()->published()->get(),
])
->layout(config('zeus.layout'));
}
}
12 changes: 7 additions & 5 deletions src/Http/Livewire/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

namespace LaraZeus\Sky\Http\Livewire;

use Illuminate\View\View;
use LaraZeus\Sky\Models\Tag;
use LaraZeus\Sky\SkyPlugin;
use Livewire\Component;

class Tags extends Component
{
public $type;
public string $type;

public $slug;
public string $slug;

public $tag;
public Tag $tag;

public function mount($type, $slug)
public function mount(string $type, string $slug): void
{
$this->type = $type;
$this->slug = $slug;
Expand All @@ -22,7 +24,7 @@ public function mount($type, $slug)
abort_if($this->tag === null, 404);
}

public function render()
public function render(): View
{
seo()
->site(config('zeus.site_title', 'Laravel'))
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Faq.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

class Faq extends Model
{
use HasTranslations;
use HasTags;
use HasTranslations;

public array $translatable = [
'question',
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Library.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class Library extends Model implements HasMedia
{
use HasFactory;
use HasTags;
use InteractsWithMedia;
use HasTranslations;
use InteractsWithMedia;

public array $translatable = [
'title',
Expand Down
4 changes: 2 additions & 2 deletions src/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
*/
class Post extends Model implements HasMedia
{
use SoftDeletes;
use HasFactory;
use HasTags;
use HasTranslations;
use InteractsWithMedia;
use PostScope;
use HasTranslations;
use SoftDeletes;

public array $translatable = [
'title',
Expand Down
5 changes: 2 additions & 3 deletions src/Models/PostScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace LaraZeus\Sky\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;

trait PostScope
{
Expand Down Expand Up @@ -88,13 +87,13 @@ function ($query) use ($category) {
/**
* @param Builder<Post> $query
*/
public function scopeSearch(Builder $query, $term): Builder
public function scopeSearch(Builder $query, ?string $term): Builder
{
if ($term !== null) {
return $query->where(
function ($query) use ($term) {
foreach (['title', 'slug', 'content', 'description'] as $attribute) {
$query->orWhere(DB::raw("lower({$attribute})"), 'like', "%{$term}%");
$query->orWhere($attribute, 'like', "%{$term}%");
}

return $query;
Expand Down
2 changes: 1 addition & 1 deletion src/Models/PostStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PostStatus extends Model
{
use \Sushi\Sushi;

public function getRows()
public function getRows(): array
{
return [
['name' => 'publish', 'label' => __('Publish'), 'class' => 'px-2 py-0.5 text-xs rounded-xl text-success-700 bg-success-500/10', 'icon' => 'heroicon-o-check-badge'],
Expand Down
1 change: 1 addition & 0 deletions src/Models/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* @property string $slug
* @property string $type
* @property string $name
* @property string $description
*
* @method Builder|static published()
*/
Expand Down
Loading

0 comments on commit edd33fa

Please sign in to comment.