Skip to content

Commit 6bac86c

Browse files
committed
added config, models, providers
1 parent 7443249 commit 6bac86c

22 files changed

Lines changed: 425 additions & 116 deletions

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
],
2424
"require": {
2525
"php": "^8.4",
26-
"yuges-code/laravel-package": "^1.0"
26+
"yuges-code/laravel-package": "^1.0",
27+
"yuges-code/laravel-sluggable": "^1.0"
2728
},
2829
"require-dev": {
2930
"orchestra/testbench": "^10.0"
@@ -46,7 +47,7 @@
4647
"extra": {
4748
"laravel": {
4849
"providers": [
49-
"Yuges\\Groupable\\Providers\\TemplateServiceProvider"
50+
"Yuges\\Groupable\\Providers\\GroupableServiceProvider"
5051
]
5152
}
5253
},

config/groupable.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,34 @@
33
// Config for yuges/groupable
44

55
return [
6-
# code...
6+
/*
7+
* FQCN (Fully Qualified Class Name) of the models to use for groups
8+
*/
9+
'models' => [
10+
'group' => [
11+
'key' => Yuges\Package\Enums\KeyType::BigInteger,
12+
'table' => 'groups',
13+
'class' => Yuges\Groupable\Models\Group::class,
14+
'observer' => Yuges\Groupable\Observers\GroupObserver::class,
15+
],
16+
'groupable' => [
17+
'table' => 'groupables',
18+
'key' => Yuges\Package\Enums\KeyType::BigInteger,
19+
'class' => Yuges\Groupable\Models\Groupable::class,
20+
'allowed' => [
21+
'classes' => [
22+
# models...
23+
],
24+
],
25+
'observer' => Yuges\Groupable\Observers\GroupableObserver::class,
26+
],
27+
],
28+
29+
'permissions' => [],
30+
31+
'actions' => [
32+
// 'sync' => Yuges\Topicable\Actions\SyncTopicAction::class,
33+
// 'attach' => Yuges\Topicable\Actions\AttachTopicAction::class,
34+
// 'detach' => Yuges\Topicable\Actions\DetachTopicAction::class,
35+
],
736
];
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
use Yuges\Package\Enums\KeyType;
4+
use Yuges\Groupable\Models\Group;
5+
use Yuges\Groupable\Config\Config;
6+
use Yuges\Package\Database\Schema\Schema;
7+
use Yuges\Package\Database\Schema\Blueprint;
8+
use Yuges\Package\Database\Migrations\Migration;
9+
10+
return new class extends Migration
11+
{
12+
public function __construct()
13+
{
14+
$this->table = Config::getGroupClass(Group::class)::getTableName();
15+
}
16+
17+
public function up(): void
18+
{
19+
if (Schema::hasTable($this->table)) {
20+
return;
21+
}
22+
23+
Schema::create($this->table, function (Blueprint $table) {
24+
$table->key(Config::getGroupKeyType(KeyType::BigInteger));
25+
});
26+
27+
Schema::table($this->table, function (Blueprint $table) {
28+
$table
29+
->foreignIdFor(Config::getGroupClass(Group::class), 'parent_id')
30+
->nullable()
31+
->constrained()
32+
->cascadeOnUpdate()
33+
->cascadeOnDelete();
34+
35+
$table->string('slug')->unique();
36+
$table->string('name')->unique();
37+
$table->string('type')->nullable();
38+
39+
$table->order();
40+
41+
$table->timestamps();
42+
});
43+
}
44+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use Yuges\Package\Enums\KeyType;
4+
use Yuges\Groupable\Models\Group;
5+
use Yuges\Groupable\Config\Config;
6+
use Yuges\Groupable\Models\Groupable;
7+
use Yuges\Package\Database\Schema\Schema;
8+
use Yuges\Package\Database\Schema\Blueprint;
9+
use Yuges\Package\Database\Migrations\Migration;
10+
11+
return new class extends Migration
12+
{
13+
public function __construct()
14+
{
15+
$this->table = Config::getGroupableClass(Groupable::class)::getTableName();
16+
}
17+
18+
public function up(): void
19+
{
20+
if (Schema::hasTable($this->table)) {
21+
return;
22+
}
23+
24+
Schema::create($this->table, function (Blueprint $table) {
25+
$table
26+
->foreignIdFor(Config::getGroupClass(Group::class))
27+
->constrained()
28+
->cascadeOnUpdate()
29+
->cascadeOnDelete();
30+
31+
$table->keyMorphs(Config::getGroupableKeyType(KeyType::BigInteger), 'groupable');
32+
33+
$table->unique(['group_id', 'groupable_id', 'groupable_type']);
34+
35+
$table->timestamps();
36+
});
37+
}
38+
};

database/migrations/create_groups_table.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/Config/Config.php

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,67 @@
11
<?php
22

3-
namespace Vendor\Template\Config;
3+
namespace Yuges\Groupable\Config;
44

5-
class Config
5+
use Yuges\Package\Enums\KeyType;
6+
use Yuges\Groupable\Models\Group;
7+
use Illuminate\Support\Collection;
8+
use Yuges\Groupable\Models\Groupable;
9+
use Yuges\Groupable\Observers\GroupObserver;
10+
use Yuges\Groupable\Observers\GroupableObserver;
11+
12+
class Config extends \Yuges\Package\Config\Config
613
{
7-
/** @return class-string<Template> */
8-
public static function getTemplateClass()
14+
const string NAME = 'groupable';
15+
16+
public static function getGroupTable(mixed $default = null): string
17+
{
18+
return self::get('models.group.table', $default);
19+
}
20+
21+
/** @return class-string<Group> */
22+
public static function getGroupClass(mixed $default = null): string
23+
{
24+
return self::get('model.group.class', $default);
25+
}
26+
27+
public static function getGroupKeyType(mixed $default = null): KeyType
28+
{
29+
return self::get('models.group.key', $default);
30+
}
31+
32+
/** @return class-string<GroupObserver> */
33+
public static function getGroupObserverClass(mixed $default = null): string
34+
{
35+
return self::get('models.group.observer', $default);
36+
}
37+
38+
public static function getGroupableTable(mixed $default = null): string
39+
{
40+
return self::get('models.groupable.table', $default);
41+
}
42+
43+
/** @return class-string<Groupable> */
44+
public static function getGroupableClass(mixed $default = null): string
45+
{
46+
return self::get('models.groupable.class', $default);
47+
}
48+
49+
public static function getGroupableKeyType(mixed $default = null): KeyType
950
{
51+
return self::get('models.groupable.key', $default);
52+
}
1053

54+
/** @return Collection<array-key, class-string<Groupable>> */
55+
public static function getGroupableAllowedClasses(mixed $default = null): Collection
56+
{
57+
return Collection::make(
58+
self::get('models.groupable.allowed.classes', $default)
59+
);
1160
}
1261

13-
# code...
62+
/** @return class-string<GroupableObserver> */
63+
public static function getGroupableObserverClass(mixed $default = null): string
64+
{
65+
return self::get('models.Groupable.observer', $default);
66+
}
1467
}

src/Exceptions/InvalidGroup.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Yuges\Groupable\Exceptions;
4+
5+
use Exception;
6+
use TypeError;
7+
use Yuges\Groupable\Models\Group;
8+
9+
class InvalidGroup extends Exception
10+
{
11+
public static function doesNotImplementGroup(string $class): TypeError
12+
{
13+
$group = Group::class;
14+
15+
return new TypeError("Group class `{$class}` must implement `{$group}`");
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Yuges\Groupable\Exceptions;
4+
5+
use Exception;
6+
use TypeError;
7+
use Yuges\Groupable\Models\Groupable;
8+
9+
class InvalidGroupable extends Exception
10+
{
11+
public static function doesNotImplementGroupable(string $class): TypeError
12+
{
13+
$groupable = Groupable::class;
14+
15+
return new TypeError("Groupable class `{$class}` must implement `{$groupable}`");
16+
}
17+
}

src/Exceptions/InvalidTemplate.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/Models/Group.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Yuges\Groupable\Models;
4+
5+
use Yuges\Package\Models\Model;
6+
use Yuges\Groupable\Config\Config;
7+
use Yuges\Sluggable\Traits\HasSlug;
8+
use Yuges\Sluggable\Options\SlugOptions;
9+
use Yuges\Sluggable\Interfaces\Sluggable;
10+
use Illuminate\Database\Eloquent\Factories\HasFactory;
11+
12+
/**
13+
* @property string $name
14+
* @property string $slug
15+
* @property null|string $type
16+
*/
17+
class Group extends Model implements Sluggable
18+
{
19+
use HasFactory, HasSlug;
20+
21+
protected $table = 'groups';
22+
23+
protected $guarded = ['id'];
24+
25+
public function getTable(): string
26+
{
27+
return Config::getGroupTable() ?? $this->table;
28+
}
29+
30+
public function sluggable(): SlugOptions
31+
{
32+
$options = new SlugOptions();
33+
34+
$options->column = 'slug';
35+
$options->separator = '-';
36+
$options->source = ['name'];
37+
$options->union = [];
38+
39+
return $options;
40+
}
41+
}

0 commit comments

Comments
 (0)