Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

Begin work on proper MyBB Application and Extension system #272

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
204 changes: 204 additions & 0 deletions app/Database/AbstractModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
<?php

/**
* @author MyBB Group
* @version 2.0.0
* @package mybb/core
* @license http://www.mybb.com/licenses/bsd3 BSD-3
*/

namespace MyBB\Core\Database;

use MyBB\Core\Events\ConfigureModelDates;
use MyBB\Core\Events\ConfigureModelDefaultAttributes;
use MyBB\Core\Events\GetModelRelationship;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\Relations\Relation;
use LogicException;

/**
* Base model class, building on Eloquent.
*
* Adds the ability for custom relations to be added to a model during runtime.
* These relations behave in the same way that you would expect; they can be
* queried, eager loaded, and accessed as an attribute.
*/
abstract class AbstractModel extends Eloquent
{
/**
* Indicates if the model should be timestamped. Turn off by default.
*
* @var bool
*/
public $timestamps = false;

/**
* An array of callbacks to be run once after the model is saved.
*
* @var callable[]
*/
protected $afterSaveCallbacks = [];

/**
* An array of callbacks to be run once after the model is deleted.
*
* @var callable[]
*/
protected $afterDeleteCallbacks = [];

/**
* {@inheritdoc}
*/
public static function boot()
{
parent::boot();

static::saved(function (AbstractModel $model) {
foreach ($model->releaseAfterSaveCallbacks() as $callback) {
$callback($model);
}
});

static::deleted(function (AbstractModel $model) {
foreach ($model->releaseAfterDeleteCallbacks() as $callback) {
$callback($model);
}
});
}

/**
* {@inheritdoc}
*/
public function __construct(array $attributes = [])
{
$defaults = [];

static::$dispatcher->fire(
new ConfigureModelDefaultAttributes($this, $defaults)
);

$this->attributes = $defaults;

parent::__construct($attributes);
}

/**
* Get the attributes that should be converted to dates.
*
* @return array
*/
public function getDates()
{
static $dates = [];

$class = get_class($this);

if (! isset($dates[$class])) {
static::$dispatcher->fire(
new ConfigureModelDates($this, $this->dates)
);

$dates[$class] = $this->dates;
}

return $dates[$class];
}

/**
* Get an attribute from the model. If nothing is found, attempt to load
* a custom relation method with this key.
*
* @param string $key
* @return mixed
*/
public function getAttribute($key)
{
if (! is_null($value = parent::getAttribute($key))) {
return $value;
}

// If a custom relation with this key has been set up, then we will load
// and return results from the query and hydrate the relationship's
// value on the "relationships" array.
if (! $this->relationLoaded($key) && ($relation = $this->getCustomRelation($key))) {
if (! $relation instanceof Relation) {
throw new LogicException(
'Relationship method must return an object of type '.Relation::class
);
}

return $this->relations[$key] = $relation->getResults();
}
}

/**
* Get a custom relation object.
*
* @param string $name
* @return mixed
*/
protected function getCustomRelation($name)
{
return static::$dispatcher->until(
new GetModelRelationship($this, $name)
);
}

/**
* Register a callback to be run once after the model is saved.
*
* @param callable $callback
* @return void
*/
public function afterSave($callback)
{
$this->afterSaveCallbacks[] = $callback;
}

/**
* Register a callback to be run once after the model is deleted.
*
* @param callable $callback
* @return void
*/
public function afterDelete($callback)
{
$this->afterDeleteCallbacks[] = $callback;
}

/**
* @return callable[]
*/
public function releaseAfterSaveCallbacks()
{
$callbacks = $this->afterSaveCallbacks;

$this->afterSaveCallbacks = [];

return $callbacks;
}

/**
* @return callable[]
*/
public function releaseAfterDeleteCallbacks()
{
$callbacks = $this->afterDeleteCallbacks;

$this->afterDeleteCallbacks = [];

return $callbacks;
}

/**
* {@inheritdoc}
*/
public function __call($method, $arguments)
{
if ($relation = $this->getCustomRelation($method)) {
return $relation;
}

return parent::__call($method, $arguments);
}
}
163 changes: 163 additions & 0 deletions app/Database/DatabaseMigrationRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php

/**
* @author MyBB Group
* @version 2.0.0
* @package mybb/core
* @license http://www.mybb.com/licenses/bsd3 BSD-3
*/

namespace MyBB\Core\Database;

use Illuminate\Database\ConnectionResolverInterface as Resolver;

class DatabaseMigrationRepository implements MigrationRepositoryInterface
{
/**
* The database connection resolver instance.
*
* @var \Illuminate\Database\ConnectionResolverInterface
*/
protected $resolver;

/**
* The name of the migration table.
*
* @var string
*/
protected $table;

/**
* The name of the database connection to use.
*
* @var string
*/
protected $connection;

/**
* Create a new database migration repository instance.
*
* @param \Illuminate\Database\ConnectionResolverInterface $resolver
* @param string $table
*/
public function __construct(Resolver $resolver, $table)
{
$this->table = $table;
$this->resolver = $resolver;
}

/**
* Get the ran migrations.
*
* @return array
*/
public function getRan($extension = null)
{
return $this->table()
->where('extension', $extension)
->orderBy('migration', 'asc')
->lists('migration');
}

/**
* Log that a migration was run.
*
* @param string $file
* @param string $extension
* @return void
*/
public function log($file, $extension = null)
{
$record = ['migration' => $file, 'extension' => $extension];

$this->table()->insert($record);
}

/**
* Remove a migration from the log.
*
* @param string $file
* @param string $extension
* @return void
*/
public function delete($file, $extension = null)
{
$query = $this->table()->where('migration', $file);

if (is_null($extension)) {
$query->whereNull('extension');
} else {
$query->where('extension', $extension);
}

$query->delete();
}

/**
* Create the migration repository data store.
*
* @return void
*/
public function createRepository()
{
$schema = $this->getConnection()->getSchemaBuilder();

$schema->create($this->table, function ($table) {
$table->string('migration');
$table->string('extension')->nullable();
});
}

/**
* Determine if the migration repository exists.
*
* @return bool
*/
public function repositoryExists()
{
$schema = $this->getConnection()->getSchemaBuilder();

return $schema->hasTable($this->table);
}

/**
* Get a query builder for the migration table.
*
* @return \Illuminate\Database\Query\Builder
*/
protected function table()
{
return $this->getConnection()->table($this->table);
}

/**
* Get the connection resolver instance.
*
* @return \Illuminate\Database\ConnectionResolverInterface
*/
public function getConnectionResolver()
{
return $this->resolver;
}

/**
* Resolve the database connection instance.
*
* @return \Illuminate\Database\Connection
*/
public function getConnection()
{
return $this->resolver->connection($this->connection);
}

/**
* Set the information source to gather data.
*
* @param string $name
* @return void
*/
public function setSource($name)
{
$this->connection = $name;
}
}
Loading