-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b091fcb
Showing
133 changed files
with
33,970 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
<?php | ||
|
||
namespace IgniteKit\Backports\Database\Capsule; | ||
|
||
use PDO; | ||
use IgniteKit\Backports\Container\Container; | ||
use IgniteKit\Backports\Database\DatabaseManager; | ||
use IgniteKit\Backports\Contracts\Events\Dispatcher; | ||
use IgniteKit\Backports\Support\Traits\CapsuleManagerTrait; | ||
use IgniteKit\Backports\Database\Eloquent\Model as Eloquent; | ||
use IgniteKit\Backports\Database\Connectors\ConnectionFactory; | ||
|
||
class Manager | ||
{ | ||
use CapsuleManagerTrait; | ||
|
||
/** | ||
* The database manager instance. | ||
* | ||
* @var \IgniteKit\Backports\Database\DatabaseManager | ||
*/ | ||
protected $manager; | ||
|
||
/** | ||
* Create a new database capsule manager. | ||
* | ||
* @param \IgniteKit\Backports\Container\Container|null $container | ||
* @return void | ||
*/ | ||
public function __construct(Container $container = null) | ||
{ | ||
$this->setupContainer($container ?: new Container); | ||
|
||
// Once we have the container setup, we will setup the default configuration | ||
// options in the container "config" binding. This will make the database | ||
// manager work correctly out of the box without extreme configuration. | ||
$this->setupDefaultConfiguration(); | ||
|
||
$this->setupManager(); | ||
} | ||
|
||
/** | ||
* Setup the default database configuration options. | ||
* | ||
* @return void | ||
*/ | ||
protected function setupDefaultConfiguration() | ||
{ | ||
$this->container['config']['database.fetch'] = PDO::FETCH_OBJ; | ||
|
||
$this->container['config']['database.default'] = 'default'; | ||
} | ||
|
||
/** | ||
* Build the database manager instance. | ||
* | ||
* @return void | ||
*/ | ||
protected function setupManager() | ||
{ | ||
$factory = new ConnectionFactory($this->container); | ||
|
||
$this->manager = new DatabaseManager($this->container, $factory); | ||
} | ||
|
||
/** | ||
* Get a connection instance from the global manager. | ||
* | ||
* @param string|null $connection | ||
* @return \IgniteKit\Backports\Database\Connection | ||
*/ | ||
public static function connection($connection = null) | ||
{ | ||
return static::$instance->getConnection($connection); | ||
} | ||
|
||
/** | ||
* Get a fluent query builder instance. | ||
* | ||
* @param string $table | ||
* @param string|null $connection | ||
* @return \IgniteKit\Backports\Database\Query\Builder | ||
*/ | ||
public static function table($table, $connection = null) | ||
{ | ||
return static::$instance->connection($connection)->table($table); | ||
} | ||
|
||
/** | ||
* Get a schema builder instance. | ||
* | ||
* @param string|null $connection | ||
* @return \IgniteKit\Backports\Database\Schema\Builder | ||
*/ | ||
public static function schema($connection = null) | ||
{ | ||
return static::$instance->connection($connection)->getSchemaBuilder(); | ||
} | ||
|
||
/** | ||
* Get a registered connection instance. | ||
* | ||
* @param string|null $name | ||
* @return \IgniteKit\Backports\Database\Connection | ||
*/ | ||
public function getConnection($name = null) | ||
{ | ||
return $this->manager->connection($name); | ||
} | ||
|
||
/** | ||
* Register a connection with the manager. | ||
* | ||
* @param array $config | ||
* @param string $name | ||
* @return void | ||
*/ | ||
public function addConnection(array $config, $name = 'default') | ||
{ | ||
$connections = $this->container['config']['database.connections']; | ||
|
||
$connections[$name] = $config; | ||
|
||
$this->container['config']['database.connections'] = $connections; | ||
} | ||
|
||
/** | ||
* Bootstrap Eloquent so it is ready for usage. | ||
* | ||
* @return void | ||
*/ | ||
public function bootEloquent() | ||
{ | ||
Eloquent::setConnectionResolver($this->manager); | ||
|
||
// If we have an event dispatcher instance, we will go ahead and register it | ||
// with the Eloquent ORM, allowing for model callbacks while creating and | ||
// updating "model" instances; however, it is not necessary to operate. | ||
if ($dispatcher = $this->getEventDispatcher()) { | ||
Eloquent::setEventDispatcher($dispatcher); | ||
} | ||
} | ||
|
||
/** | ||
* Set the fetch mode for the database connections. | ||
* | ||
* @param int $fetchMode | ||
* @return $this | ||
*/ | ||
public function setFetchMode($fetchMode) | ||
{ | ||
$this->container['config']['database.fetch'] = $fetchMode; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get the database manager instance. | ||
* | ||
* @return \IgniteKit\Backports\Database\DatabaseManager | ||
*/ | ||
public function getDatabaseManager() | ||
{ | ||
return $this->manager; | ||
} | ||
|
||
/** | ||
* Get the current event dispatcher instance. | ||
* | ||
* @return \IgniteKit\Backports\Contracts\Events\Dispatcher|null | ||
*/ | ||
public function getEventDispatcher() | ||
{ | ||
if ($this->container->bound('events')) { | ||
return $this->container['events']; | ||
} | ||
} | ||
|
||
/** | ||
* Set the event dispatcher instance to be used by connections. | ||
* | ||
* @param \IgniteKit\Backports\Contracts\Events\Dispatcher $dispatcher | ||
* @return void | ||
*/ | ||
public function setEventDispatcher(Dispatcher $dispatcher) | ||
{ | ||
$this->container->instance('events', $dispatcher); | ||
} | ||
|
||
/** | ||
* Dynamically pass methods to the default connection. | ||
* | ||
* @param string $method | ||
* @param array $parameters | ||
* @return mixed | ||
*/ | ||
public static function __callStatic($method, $parameters) | ||
{ | ||
return static::connection()->$method(...$parameters); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
<?php | ||
|
||
namespace IgniteKit\Backports\Database\Concerns; | ||
|
||
use IgniteKit\Backports\Container\Container; | ||
use IgniteKit\Backports\Pagination\Paginator; | ||
use IgniteKit\Backports\Pagination\LengthAwarePaginator; | ||
|
||
trait BuildsQueries | ||
{ | ||
/** | ||
* Chunk the results of the query. | ||
* | ||
* @param int $count | ||
* @param callable $callback | ||
* @return bool | ||
*/ | ||
public function chunk($count, callable $callback) | ||
{ | ||
$this->enforceOrderBy(); | ||
|
||
$page = 1; | ||
|
||
do { | ||
// We'll execute the query for the given page and get the results. If there are | ||
// no results we can just break and return from here. When there are results | ||
// we will call the callback with the current chunk of these results here. | ||
$results = $this->forPage($page, $count)->get(); | ||
|
||
$countResults = $results->count(); | ||
|
||
if ($countResults == 0) { | ||
break; | ||
} | ||
|
||
// On each chunk result set, we will pass them to the callback and then let the | ||
// developer take care of everything within the callback, which allows us to | ||
// keep the memory low for spinning through large result sets for working. | ||
if ($callback($results, $page) === false) { | ||
return false; | ||
} | ||
|
||
unset($results); | ||
|
||
$page++; | ||
} while ($countResults == $count); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Execute a callback over each item while chunking. | ||
* | ||
* @param callable $callback | ||
* @param int $count | ||
* @return bool | ||
*/ | ||
public function each(callable $callback, $count = 1000) | ||
{ | ||
return $this->chunk($count, function ($results) use ($callback) { | ||
foreach ($results as $key => $value) { | ||
if ($callback($value, $key) === false) { | ||
return false; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Execute the query and get the first result. | ||
* | ||
* @param array $columns | ||
* @return \IgniteKit\Backports\Database\Eloquent\Model|object|static|null | ||
*/ | ||
public function first($columns = ['*']) | ||
{ | ||
return $this->take(1)->get($columns)->first(); | ||
} | ||
|
||
/** | ||
* Apply the callback's query changes if the given "value" is true. | ||
* | ||
* @param mixed $value | ||
* @param callable $callback | ||
* @param callable|null $default | ||
* @return mixed|$this | ||
*/ | ||
public function when($value, $callback, $default = null) | ||
{ | ||
if ($value) { | ||
return $callback($this, $value) ?: $this; | ||
} elseif ($default) { | ||
return $default($this, $value) ?: $this; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Pass the query to a given callback. | ||
* | ||
* @param callable $callback | ||
* @return \IgniteKit\Backports\Database\Query\Builder | ||
*/ | ||
public function tap($callback) | ||
{ | ||
return $this->when(true, $callback); | ||
} | ||
|
||
/** | ||
* Apply the callback's query changes if the given "value" is false. | ||
* | ||
* @param mixed $value | ||
* @param callable $callback | ||
* @param callable|null $default | ||
* @return mixed|$this | ||
*/ | ||
public function unless($value, $callback, $default = null) | ||
{ | ||
if (! $value) { | ||
return $callback($this, $value) ?: $this; | ||
} elseif ($default) { | ||
return $default($this, $value) ?: $this; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Create a new length-aware paginator instance. | ||
* | ||
* @param \IgniteKit\Backports\Support\Collection $items | ||
* @param int $total | ||
* @param int $perPage | ||
* @param int $currentPage | ||
* @param array $options | ||
* @return \IgniteKit\Backports\Pagination\LengthAwarePaginator | ||
*/ | ||
protected function paginator($items, $total, $perPage, $currentPage, $options) | ||
{ | ||
return Container::getInstance()->makeWith(LengthAwarePaginator::class, compact( | ||
'items', 'total', 'perPage', 'currentPage', 'options' | ||
)); | ||
} | ||
|
||
/** | ||
* Create a new simple paginator instance. | ||
* | ||
* @param \IgniteKit\Backports\Support\Collection $items | ||
* @param int $perPage | ||
* @param int $currentPage | ||
* @param array $options | ||
* @return \IgniteKit\Backports\Pagination\Paginator | ||
*/ | ||
protected function simplePaginator($items, $perPage, $currentPage, $options) | ||
{ | ||
return Container::getInstance()->makeWith(Paginator::class, compact( | ||
'items', 'perPage', 'currentPage', 'options' | ||
)); | ||
} | ||
} |
Oops, something went wrong.