Skip to content

Commit

Permalink
Merge pull request #2 from plank/1-navigation-model
Browse files Browse the repository at this point in the history
Add base model linking functionality
  • Loading branch information
m-triassi authored Oct 24, 2023
2 parents 7a3e1c6 + 253470f commit 1c472c6
Show file tree
Hide file tree
Showing 30 changed files with 691 additions and 53 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"illuminate/contracts": "^10.0"
},
"require-dev": {
"doctrine/dbal": "^3.7",
"laravel/pint": "^1.0",
"nunomaduro/collision": "^7.8",
"nunomaduro/larastan": "^2.0.1",
Expand Down
17 changes: 13 additions & 4 deletions config/frontdesk.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
<?php

/*
* You can place your custom package configuration in here.
*/
return [
use Plank\Frontdesk\Models\Hyperlink;
use Plank\Frontdesk\Models\Menu;
use Plank\Frontdesk\Tests\Database\Factories\HyperlinkFactory;
use Plank\Frontdesk\Tests\Database\Factories\MenuFactory;

return [
'models' => [
'hyperlink' => Hyperlink::class,
'menu' => Menu::class
],
'factories' => [
'hyperlink' => HyperlinkFactory::class,
'menu' => MenuFactory::class
]
];
38 changes: 38 additions & 0 deletions database/migrations/create_hyperlinks_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
public function up()
{
Schema::create('hyperlinks', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('parent_id')->nullable();
$table->morphs('linkable');
$table->foreignId('menu_id')
->nullable()
->references('id')
->on('menus');
$table->string('title');
$table->unsignedInteger('order')->nullable();
$table->string('destination')->nullable(); // if this is null the link isn't clickable
$table->timestamps();
});

Schema::table('hyperlinks', function (Blueprint $table) {
$table->foreign('parent_id')
->references("id")
->on('hyperlinks');
// make the morph nullable in-case we want to link externally
$table->string('linkable_type')->nullable()->change();
$table->unsignedBigInteger('linkable_id')->nullable()->change();
});
}

public function down()
{
Schema::dropIfExists('hyperlinks');
}
};
23 changes: 23 additions & 0 deletions database/migrations/create_menuables_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up()
{
Schema::create('menuables', function (Blueprint $table) {
$table->id();
$table->foreignId('menu_id')->references('id')->on('menus');
$table->morphs('menuable');
$table->integer('order')->nullable();
});
}

public function down()
{
Schema::dropIfExists('menuables');
}
};
22 changes: 22 additions & 0 deletions database/migrations/create_menus_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up()
{
Schema::create('menus', function (Blueprint $table) {
$table->id();
$table->string('identifier');
$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists('menus');
}
};
38 changes: 19 additions & 19 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
backupGlobals="false"
bootstrap="vendor/autoload.php"
colors="true"
processIsolation="false"
stopOnFailure="false"
executionOrder="random"
failOnWarning="true"
failOnRisky="true"
failOnEmptyTestSuite="true"
beStrictAboutOutputDuringTests="true"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false"
>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
backupGlobals="false"
bootstrap="vendor/autoload.php"
colors="true"
processIsolation="false"
stopOnFailure="false"
executionOrder="random"
failOnWarning="true"
failOnRisky="true"
failOnEmptyTestSuite="true"
beStrictAboutOutputDuringTests="true"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false">
<testsuites>
<testsuite name="Plank Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
Expand All @@ -33,4 +28,9 @@
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
23 changes: 23 additions & 0 deletions src/Concerns/HasMenus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Plank\Frontdesk\Concerns;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Plank\Frontdesk\Models\Menu;
/**
* @mixin \Illuminate\Database\Eloquent\Model
*
* @property-read Collection<Menu> $menus
*/
trait HasMenus
{
/**
* @return MorphToMany
*/
public function menus(): MorphToMany
{
return $this->morphToMany(Menu::class, 'menuable')
->orderByPivot('order');
}
}
19 changes: 19 additions & 0 deletions src/Concerns/IsLinkable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Plank\Frontdesk\Concerns;

use Illuminate\Database\Eloquent\Relations\MorphMany;
use Plank\Frontdesk\Models\Hyperlink;

/**
* @mixin \Illuminate\Database\Eloquent\Model
*
* @property-read \Illuminate\Database\Eloquent\Collection<Hyperlink> $hyperlinks
*/
trait IsLinkable
{
public function hyperlinks(): MorphMany
{
return $this->morphMany(Hyperlink::class, 'linkable');
}
}
16 changes: 16 additions & 0 deletions src/Contracts/AggregatesLinks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Plank\Frontdesk\Contracts;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
* @property-read Collection<Model&LinksToContent> $hyperlinks
* Could add $navs to this interface if you decide to implement it
*/
interface AggregatesLinks
{
public function hyperlinks(): HasMany;
}
21 changes: 21 additions & 0 deletions src/Contracts/Linkable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Plank\Frontdesk\Contracts;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;
/**
* @property-read Collection<Model&LinksToContent> $hyperlinks
* @property-read string $linkTitle
* @property-read string $linkUrl
*/
interface Linkable
{
public function hyperlinks(): MorphMany;

public function linkTitle(): Attribute;

public function linkUrl(): Attribute;
}
31 changes: 31 additions & 0 deletions src/Contracts/LinksToContent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Plank\Frontdesk\Contracts;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Plank\Frontdesk\Models\Menu;

/**
* @property int|string $id
* @property int|string $parent_id
* @property string $title
* @property string $destination
* @property-read Collection<Linkable> $linkable
* @property-read Collection<Menu> $menu
* @property-read Model&LinksToContent|null $parent
* @property-read Collection<Model&LinksToContent> $children
*/
interface LinksToContent
{
public function linkable(): MorphTo;

public function menu(): BelongsTo;

public function parent(): BelongsTo;

public function children(): HasMany;
}
17 changes: 17 additions & 0 deletions src/Contracts/Menuable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Plank\Frontdesk\Contracts;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;


/**
* @property-read Collection<Model&AggregatesLinks> $menus
* Could add $navs to this interface if you decide to implement it
*/
interface Menuable
{
public function menus(): MorphToMany;
}
18 changes: 0 additions & 18 deletions src/Facades/Frontdesk.php

This file was deleted.

8 changes: 0 additions & 8 deletions src/Frontdesk.php

This file was deleted.

6 changes: 5 additions & 1 deletion src/FrontdeskServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public function configurePackage(Package $package): void
$package
->name('frontdesk')
->hasConfigFile()
->hasMigration('create_links_table');
->hasMigrations([
'create_menus_table',
'create_menuables_table',
'create_hyperlinks_table',
]);
}
}
Loading

0 comments on commit 1c472c6

Please sign in to comment.