Skip to content

Commit

Permalink
Merge conflict fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gamemovies committed Feb 14, 2016
2 parents 9b1c4de + 92535d2 commit 03520de
Show file tree
Hide file tree
Showing 25 changed files with 485 additions and 78 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"
}
6 changes: 6 additions & 0 deletions nitpick.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"ignore": [
"tests/*",
"src/migrations/*"
]
}
12 changes: 9 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,20 @@ Add the service provider to `config/app.php` under `providers`:

Publish Assets

php artisan vendor:publish --provider="Cmgmyr\Messenger\MessengerServiceProvider"
php artisan vendor:publish --provider="Cmgmyr\Messenger\MessengerServiceProvider"

Update config file to reference your User Model:

config/messenger.php
config/messenger.php

Create a `users` table if you do not have one already. If you need one, simply use [this example](https://github.com/cmgmyr/laravel-messenger/tree/master/src/Cmgmyr/Messenger/examples/create_users_table.php) as a starting point, then migrate.

**(Optional)** Define names of database tables in package config file if you don't want to use default ones:

'messages_table' => 'messenger_messages',
'participants_table' => 'messenger_participants',
'threads_table' => 'messenger_threads',

Migrate your database:

php artisan migrate
Expand All @@ -62,7 +68,7 @@ Add the trait to your user model:
use Cmgmyr\Messenger\Traits\Messagable;

class User extends Model {
use Messagable;
use Messagable;
}


Expand Down
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 03520de

Please sign in to comment.