Skip to content

Commit

Permalink
feat(multisite): add Multisite models
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaucau committed Dec 18, 2024
1 parent baa1077 commit 6201895
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 0 deletions.
78 changes: 78 additions & 0 deletions src/Models/Network/Blog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* Copyright © Dimitri BOUTEILLE (https://github.com/dimitriBouteille)
* See LICENSE.txt for license details.
*
* Author: Dimitri BOUTEILLE <[email protected]>
*/

namespace Dbout\WpOrm\Models\Network;

use Carbon\Carbon;
use Dbout\WpOrm\Orm\AbstractModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;

/**
* @property-read int $blog_id
* @property int $site_id
* @property string $domain
* @property string $path
* @property Carbon $registered
* @property Carbon $last_updated
* @property bool $public
* @property bool $archived
* @property bool $mature
* @property bool $spam
* @property bool $deleted
* @property int $lang_id
*
* @property-read Site $site
* @property-read BlogVersion|null $version
*/
class Blog extends AbstractModel
{
public const CREATED_AT = self::REGISTERED;
public const UPDATED_AT = self::LAST_UPDATED;
final public const BLOG_ID = 'blog_id';
final public const SITE_ID = 'site_id';
final public const DOMAIN = 'domain';
final public const PATH = 'path';
final public const REGISTERED = 'registered';
final public const LAST_UPDATED = 'last_updated';
final public const PUBLIC = 'public';
final public const ARCHIVED = 'archived';
final public const MATURE = 'mature';
final public const SPAM = 'spam';
final public const DELETED = 'deleted';
final public const LANG_ID = 'lang_id';

protected $primaryKey = self::BLOG_ID;

protected bool $useBasePrefix = true;

protected $casts = [
self::BLOG_ID => 'int',
self::SITE_ID => 'int',
self::REGISTERED => 'datetime',
self::LAST_UPDATED => 'datetime',
self::PUBLIC => 'bool',
self::ARCHIVED => 'bool',
self::MATURE => 'bool',
self::SPAM => 'bool',
self::DELETED => 'bool',
self::LANG_ID => 'int',
];

protected $table = 'blogs';

public function site(): BelongsTo
{
return $this->belongsTo(Site::class, self::SITE_ID);
}

public function version(): HasOne
{
return $this->hasOne(BlogVersion::class, BlogVersion::BLOG_ID);
}
}
44 changes: 44 additions & 0 deletions src/Models/Network/BlogVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Copyright © Dimitri BOUTEILLE (https://github.com/dimitriBouteille)
* See LICENSE.txt for license details.
*
* Author: Dimitri BOUTEILLE <[email protected]>
*/

namespace Dbout\WpOrm\Models\Network;

use Carbon\Carbon;
use Dbout\WpOrm\Orm\AbstractModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
* @property-read int $blog_id
* @property string $db_version
* @property Carbon $last_updated
*
* @property-read Blog $blog
*/
class BlogVersion extends AbstractModel
{
public const CREATED_AT = null;
public const UPDATED_AT = self::LAST_UPDATED;
final public const BLOG_ID = 'blog_id';
final public const DB_VERSION = 'db_version';
final public const LAST_UPDATED = 'last_updated';

protected bool $useBasePrefix = true;

protected $table = 'blog_versions';

protected $primaryKey = self::BLOG_ID;

protected $casts = [
self::LAST_UPDATED => 'datetime',
];

public function blog(): BelongsTo
{
return $this->belongsTo(Blog::class, self::BLOG_ID);
}
}
49 changes: 49 additions & 0 deletions src/Models/Network/RegistrationLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Copyright © Dimitri BOUTEILLE (https://github.com/dimitriBouteille)
* See LICENSE.txt for license details.
*
* Author: Dimitri BOUTEILLE <[email protected]>
*/

namespace Dbout\WpOrm\Models\Network;

use Carbon\Carbon;
use Dbout\WpOrm\Orm\AbstractModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
* @property-read int $ID
* @property string $email
* @property string $IP
* @property int $blog_id
* @property Carbon $date_registered
*
* @property-read Blog|null $blog
*/
class RegistrationLog extends AbstractModel
{
public const CREATED_AT = self::DATE_REGISTERED;
public const UPDATED_AT = null;
final public const ID = 'ID';
final public const EMAIL = 'email';
final public const IP = 'IP';
final public const BLOG_ID = 'blog_id';
final public const DATE_REGISTERED = 'date_registered';

protected bool $useBasePrefix = true;

protected $table = 'registration_log';

protected $primaryKey = self::ID;

protected $casts = [
self::BLOG_ID => 'int',
self::DATE_REGISTERED => 'datetime',
];

public function blog(): BelongsTo
{
return $this->belongsTo(Blog::class, self::BLOG_ID);
}
}
63 changes: 63 additions & 0 deletions src/Models/Network/Signup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Copyright © Dimitri BOUTEILLE (https://github.com/dimitriBouteille)
* See LICENSE.txt for license details.
*
* Author: Dimitri BOUTEILLE <[email protected]>
*/

namespace Dbout\WpOrm\Models\Network;

use Carbon\Carbon;
use Dbout\WpOrm\Models\User;
use Dbout\WpOrm\Orm\AbstractModel;
use Illuminate\Database\Eloquent\Relations\HasOne;

/**
* @property-read int $signup_id
* @property string $domain
* @property string $path
* @property string $title
* @property string $user_login
* @property string $user_email
* @property Carbon $registered
* @property Carbon $activated
* @property bool $active
* @property string $activation_key
* @property string|null $meta
*
* @property-read User|null $user
*/
class Signup extends AbstractModel
{
public const CREATED_AT = self::REGISTERED;
public const UPDATED_AT = null;
final public const SIGNUP_ID = 'signup_id';
final public const DOMAIN = 'domain';
final public const PATH = 'path';
final public const TITLE = 'title';
final public const USER_LOGIN = 'user_login';
final public const USER_EMAIL = 'user_email';
final public const REGISTERED = 'registered';
final public const ACTIVATED = 'activated';
final public const ACTIVE = 'active';
final public const ACTIVATION_KEY = 'activation_key';
final public const META = 'meta';

protected bool $useBasePrefix = true;

protected $table = 'signups';

protected $primaryKey = self::SIGNUP_ID;

protected $casts = [
self::REGISTERED => 'datetime',
self::ACTIVATED => 'datetime',
self::ACTIVE => 'bool',
];

public function user(): HasOne
{
return $this->hasOne(User::class, User::EMAIL, self::USER_EMAIL);
}
}
50 changes: 50 additions & 0 deletions src/Models/Network/Site.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Copyright © Dimitri BOUTEILLE (https://github.com/dimitriBouteille)
* See LICENSE.txt for license details.
*
* Author: Dimitri BOUTEILLE <[email protected]>
*/

namespace Dbout\WpOrm\Models\Network;

use Dbout\WpOrm\Concerns\HasMetas;
use Dbout\WpOrm\MetaMappingConfig;
use Dbout\WpOrm\Orm\AbstractModel;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
* @property-read int $id
* @property string $domain
* @property string $path
*
* @property-read Collection<SiteMeta> $metas
* @property-read Collection<Blog> $blogs
*/
class Site extends AbstractModel
{
use HasMetas;

public const CREATED_AT = null;
public const UPDATED_AT = null;
final public const ID = 'id';
final public const DOMAIN = 'domain';
final public const PATH = 'path';

protected bool $useBasePrefix = true;

protected $table = 'site';

protected $primaryKey = self::ID;

public function blogs(): HasMany
{
return $this->hasMany(Blog::class, Blog::SITE_ID);
}

public function getMetaConfigMapping(): MetaMappingConfig
{
return new MetaMappingConfig(SiteMeta::class, SiteMeta::SITE_ID);
}
}
37 changes: 37 additions & 0 deletions src/Models/Network/SiteMeta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Copyright © Dimitri BOUTEILLE (https://github.com/dimitriBouteille)
* See LICENSE.txt for license details.
*
* Author: Dimitri BOUTEILLE <[email protected]>
*/

namespace Dbout\WpOrm\Models\Network;

use Dbout\WpOrm\Models\Meta\AbstractMeta;
use Illuminate\Database\Eloquent\Relations\HasOne;

/**
* @property-read int $meta_id
* @property int $site_id
* @property string|null $meta_key
* @property mixed|null $meta_value
*
* @property-read Site $site
*/
class SiteMeta extends AbstractMeta
{
final public const META_ID = 'meta_id';
final public const SITE_ID = 'site_id';

protected bool $useBasePrefix = true;

protected $table = 'sitemeta';

protected $primaryKey = self::META_ID;

public function site(): HasOne
{
return $this->hasOne(Site::class, Site::ID, self::SITE_ID);
}
}

0 comments on commit 6201895

Please sign in to comment.