Skip to content

Commit

Permalink
Implement ability to define custom prefixed table names & models
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkomarev authored and cmgmyr committed Feb 25, 2016
1 parent d6e1333 commit 7cc2928
Show file tree
Hide file tree
Showing 23 changed files with 450 additions and 128 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@
"Cmgmyr\\Messenger\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Cmgmyr\\Messenger\\Test\\": "tests/"
}
},
"minimum-stability": "stable"
}
37 changes: 37 additions & 0 deletions src/Cmgmyr/Messenger/MessengerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Cmgmyr\Messenger;

use Cmgmyr\Messenger\Models\Message;
use Cmgmyr\Messenger\Models\Models;
use Cmgmyr\Messenger\Models\Participant;
use Cmgmyr\Messenger\Models\Thread;
use Illuminate\Support\ServiceProvider;

class MessengerServiceProvider extends ServiceProvider
Expand All @@ -17,6 +21,9 @@ public function boot()
base_path('vendor/cmgmyr/messenger/src/config/config.php') => config_path('messenger.php'),
base_path('vendor/cmgmyr/messenger/src/migrations') => base_path('database/migrations'),
]);

$this->setMessengerModels();
$this->setUserModel();
}

/**
Expand All @@ -30,4 +37,34 @@ public function register()
base_path('vendor/cmgmyr/messenger/src/config/config.php'), 'messenger'
);
}

private function setMessengerModels()
{
$config = $this->app->make('config');

Models::setMessageModel($config->get('messenger.message_model', Message::class));
Models::setThreadModel($config->get('messenger.thread_model', Thread::class));
Models::setParticipantModel($config->get('messenger.participant_model', Participant::class));

Models::setTables([
'messages' => $config->get('messenger.messages_table', Models::message()->getTable()),
'participants' => $config->get('messenger.participants_table', Models::participant()->getTable()),
'threads' => $config->get('messenger.threads_table', Models::thread()->getTable()),
]);
}

private function setUserModel()
{
$config = $this->app->make('config');

$model = $config->get('auth.providers.users.model', function () use ($config) {
return $config->get('auth.model', $config->get('messenger.user_model'));
});

Models::setUserModel($model);

Models::setTables([
'users' => (new $model)->getTable(),
]);
}
}
18 changes: 14 additions & 4 deletions src/Cmgmyr/Messenger/Models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Cmgmyr\Messenger\Models;

use App\User;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Support\Facades\Config;

class Message extends Eloquent
{
Expand Down Expand Up @@ -37,14 +37,24 @@ class Message extends Eloquent
'body' => 'required',
];

/**
* {@inheritDoc}
*/
public function __construct(array $attributes = [])
{
$this->table = Models::table('messages');

parent::__construct($attributes);
}

/**
* Thread relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function thread()
{
return $this->belongsTo(Config::get('messenger.thread_model'), 'thread_id', 'id');
return $this->belongsTo(Models::classname(Thread::class), 'thread_id', 'id');
}

/**
Expand All @@ -54,7 +64,7 @@ public function thread()
*/
public function user()
{
return $this->belongsTo(Config::get('messenger.user_model'), 'user_id');
return $this->belongsTo(Models::classname(User::class), 'user_id');
}

/**
Expand All @@ -64,7 +74,7 @@ public function user()
*/
public function participants()
{
return $this->hasMany(Config::get('messenger.participant_model'), 'thread_id', 'thread_id');
return $this->hasMany(Models::classname(Participant::class), 'thread_id', 'thread_id');
}

/**
Expand Down
164 changes: 164 additions & 0 deletions src/Cmgmyr/Messenger/Models/Models.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?php

namespace Cmgmyr\Messenger\Models;

use App\User;

class Models
{
/**
* Map for the messenger's models.
*
* @var array
*/
protected static $models = [];

/**
* Map for the messenger's tables.
*
* @var array
*/
protected static $tables = [];

/**
* Set the model to be used for threads.
*
* @param string $model
*/
public static function setMessageModel($model)
{
static::$models[Message::class] = $model;
}

/**
* Set the model to be used for participants.
*
* @param string $model
* @return void
*/
public static function setParticipantModel($model)
{
static::$models[Participant::class] = $model;
}

/**
* Set the model to be used for threads.
*
* @param string $model
* @return void
*/
public static function setThreadModel($model)
{
static::$models[Thread::class] = $model;
}

/**
* Set the model to be used for users.
*
* @param string $model
* @return void
*/
public static function setUserModel($model)
{
static::$models[User::class] = $model;
}

/**
* Set custom table names.
*
* @param array $map
* @return void
*/
public static function setTables(array $map)
{
static::$tables = array_merge(static::$tables, $map);
}

/**
* Get a custom table name mapping for the given table.
*
* @param string $table
* @return string
*/
public static function table($table)
{
if (isset(static::$tables[$table])) {
return static::$tables[$table];
}

return $table;
}

/**
* Get the classname mapping for the given model.
*
* @param string $model
* @return string
*/
public static function classname($model)
{
if (isset(static::$models[$model])) {
return static::$models[$model];
}

return $model;
}

/**
* Get an instance of the messages model.
*
* @param array $attributes
* @return \Cmgmyr\Messenger\Models\Message
*/
public static function message(array $attributes = [])
{
return static::make(Message::class, $attributes);
}

/**
* Get an instance of the participants model.
*
* @param array $attributes
* @return \Cmgmyr\Messenger\Models\Participant
*/
public static function participant(array $attributes = [])
{
return static::make(Participant::class, $attributes);
}

/**
* Get an instance of the threads model.
*
* @param array $attributes
* @return \Cmgmyr\Messenger\Models\Thread
*/
public static function thread(array $attributes = [])
{
return static::make(Thread::class, $attributes);
}

/**
* Get an instance of the user model.
*
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
*/
public static function user(array $attributes = [])
{
return static::make(User::class, $attributes);
}

/**
* Get an instance of the given model.
*
* @param string $model
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
*/
protected static function make($model, array $attributes = [])
{
$model = static::classname($model);

return new $model($attributes);
}
}
16 changes: 13 additions & 3 deletions src/Cmgmyr/Messenger/Models/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Cmgmyr\Messenger\Models;

use App\User;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Config;

class Participant extends Eloquent
{
Expand All @@ -31,14 +31,24 @@ class Participant extends Eloquent
*/
protected $dates = ['created_at', 'updated_at', 'deleted_at', 'last_read'];

/**
* {@inheritDoc}
*/
public function __construct(array $attributes = [])
{
$this->table = Models::table('participants');

parent::__construct($attributes);
}

/**
* Thread relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function thread()
{
return $this->belongsTo(Config::get('messenger.thread_model'), 'thread_id', 'id');
return $this->belongsTo(Models::classname(Thread::class), 'thread_id', 'id');
}

/**
Expand All @@ -48,6 +58,6 @@ public function thread()
*/
public function user()
{
return $this->belongsTo(Config::get('messenger.user_model'), 'user_id');
return $this->belongsTo(Models::classname(User::class), 'user_id');
}
}
Loading

0 comments on commit 7cc2928

Please sign in to comment.