Skip to content

Commit b091fcb

Browse files
committed
init
0 parents  commit b091fcb

File tree

133 files changed

+33970
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+33970
-0
lines changed

Capsule/Manager.php

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
<?php
2+
3+
namespace IgniteKit\Backports\Database\Capsule;
4+
5+
use PDO;
6+
use IgniteKit\Backports\Container\Container;
7+
use IgniteKit\Backports\Database\DatabaseManager;
8+
use IgniteKit\Backports\Contracts\Events\Dispatcher;
9+
use IgniteKit\Backports\Support\Traits\CapsuleManagerTrait;
10+
use IgniteKit\Backports\Database\Eloquent\Model as Eloquent;
11+
use IgniteKit\Backports\Database\Connectors\ConnectionFactory;
12+
13+
class Manager
14+
{
15+
use CapsuleManagerTrait;
16+
17+
/**
18+
* The database manager instance.
19+
*
20+
* @var \IgniteKit\Backports\Database\DatabaseManager
21+
*/
22+
protected $manager;
23+
24+
/**
25+
* Create a new database capsule manager.
26+
*
27+
* @param \IgniteKit\Backports\Container\Container|null $container
28+
* @return void
29+
*/
30+
public function __construct(Container $container = null)
31+
{
32+
$this->setupContainer($container ?: new Container);
33+
34+
// Once we have the container setup, we will setup the default configuration
35+
// options in the container "config" binding. This will make the database
36+
// manager work correctly out of the box without extreme configuration.
37+
$this->setupDefaultConfiguration();
38+
39+
$this->setupManager();
40+
}
41+
42+
/**
43+
* Setup the default database configuration options.
44+
*
45+
* @return void
46+
*/
47+
protected function setupDefaultConfiguration()
48+
{
49+
$this->container['config']['database.fetch'] = PDO::FETCH_OBJ;
50+
51+
$this->container['config']['database.default'] = 'default';
52+
}
53+
54+
/**
55+
* Build the database manager instance.
56+
*
57+
* @return void
58+
*/
59+
protected function setupManager()
60+
{
61+
$factory = new ConnectionFactory($this->container);
62+
63+
$this->manager = new DatabaseManager($this->container, $factory);
64+
}
65+
66+
/**
67+
* Get a connection instance from the global manager.
68+
*
69+
* @param string|null $connection
70+
* @return \IgniteKit\Backports\Database\Connection
71+
*/
72+
public static function connection($connection = null)
73+
{
74+
return static::$instance->getConnection($connection);
75+
}
76+
77+
/**
78+
* Get a fluent query builder instance.
79+
*
80+
* @param string $table
81+
* @param string|null $connection
82+
* @return \IgniteKit\Backports\Database\Query\Builder
83+
*/
84+
public static function table($table, $connection = null)
85+
{
86+
return static::$instance->connection($connection)->table($table);
87+
}
88+
89+
/**
90+
* Get a schema builder instance.
91+
*
92+
* @param string|null $connection
93+
* @return \IgniteKit\Backports\Database\Schema\Builder
94+
*/
95+
public static function schema($connection = null)
96+
{
97+
return static::$instance->connection($connection)->getSchemaBuilder();
98+
}
99+
100+
/**
101+
* Get a registered connection instance.
102+
*
103+
* @param string|null $name
104+
* @return \IgniteKit\Backports\Database\Connection
105+
*/
106+
public function getConnection($name = null)
107+
{
108+
return $this->manager->connection($name);
109+
}
110+
111+
/**
112+
* Register a connection with the manager.
113+
*
114+
* @param array $config
115+
* @param string $name
116+
* @return void
117+
*/
118+
public function addConnection(array $config, $name = 'default')
119+
{
120+
$connections = $this->container['config']['database.connections'];
121+
122+
$connections[$name] = $config;
123+
124+
$this->container['config']['database.connections'] = $connections;
125+
}
126+
127+
/**
128+
* Bootstrap Eloquent so it is ready for usage.
129+
*
130+
* @return void
131+
*/
132+
public function bootEloquent()
133+
{
134+
Eloquent::setConnectionResolver($this->manager);
135+
136+
// If we have an event dispatcher instance, we will go ahead and register it
137+
// with the Eloquent ORM, allowing for model callbacks while creating and
138+
// updating "model" instances; however, it is not necessary to operate.
139+
if ($dispatcher = $this->getEventDispatcher()) {
140+
Eloquent::setEventDispatcher($dispatcher);
141+
}
142+
}
143+
144+
/**
145+
* Set the fetch mode for the database connections.
146+
*
147+
* @param int $fetchMode
148+
* @return $this
149+
*/
150+
public function setFetchMode($fetchMode)
151+
{
152+
$this->container['config']['database.fetch'] = $fetchMode;
153+
154+
return $this;
155+
}
156+
157+
/**
158+
* Get the database manager instance.
159+
*
160+
* @return \IgniteKit\Backports\Database\DatabaseManager
161+
*/
162+
public function getDatabaseManager()
163+
{
164+
return $this->manager;
165+
}
166+
167+
/**
168+
* Get the current event dispatcher instance.
169+
*
170+
* @return \IgniteKit\Backports\Contracts\Events\Dispatcher|null
171+
*/
172+
public function getEventDispatcher()
173+
{
174+
if ($this->container->bound('events')) {
175+
return $this->container['events'];
176+
}
177+
}
178+
179+
/**
180+
* Set the event dispatcher instance to be used by connections.
181+
*
182+
* @param \IgniteKit\Backports\Contracts\Events\Dispatcher $dispatcher
183+
* @return void
184+
*/
185+
public function setEventDispatcher(Dispatcher $dispatcher)
186+
{
187+
$this->container->instance('events', $dispatcher);
188+
}
189+
190+
/**
191+
* Dynamically pass methods to the default connection.
192+
*
193+
* @param string $method
194+
* @param array $parameters
195+
* @return mixed
196+
*/
197+
public static function __callStatic($method, $parameters)
198+
{
199+
return static::connection()->$method(...$parameters);
200+
}
201+
}

Concerns/BuildsQueries.php

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
3+
namespace IgniteKit\Backports\Database\Concerns;
4+
5+
use IgniteKit\Backports\Container\Container;
6+
use IgniteKit\Backports\Pagination\Paginator;
7+
use IgniteKit\Backports\Pagination\LengthAwarePaginator;
8+
9+
trait BuildsQueries
10+
{
11+
/**
12+
* Chunk the results of the query.
13+
*
14+
* @param int $count
15+
* @param callable $callback
16+
* @return bool
17+
*/
18+
public function chunk($count, callable $callback)
19+
{
20+
$this->enforceOrderBy();
21+
22+
$page = 1;
23+
24+
do {
25+
// We'll execute the query for the given page and get the results. If there are
26+
// no results we can just break and return from here. When there are results
27+
// we will call the callback with the current chunk of these results here.
28+
$results = $this->forPage($page, $count)->get();
29+
30+
$countResults = $results->count();
31+
32+
if ($countResults == 0) {
33+
break;
34+
}
35+
36+
// On each chunk result set, we will pass them to the callback and then let the
37+
// developer take care of everything within the callback, which allows us to
38+
// keep the memory low for spinning through large result sets for working.
39+
if ($callback($results, $page) === false) {
40+
return false;
41+
}
42+
43+
unset($results);
44+
45+
$page++;
46+
} while ($countResults == $count);
47+
48+
return true;
49+
}
50+
51+
/**
52+
* Execute a callback over each item while chunking.
53+
*
54+
* @param callable $callback
55+
* @param int $count
56+
* @return bool
57+
*/
58+
public function each(callable $callback, $count = 1000)
59+
{
60+
return $this->chunk($count, function ($results) use ($callback) {
61+
foreach ($results as $key => $value) {
62+
if ($callback($value, $key) === false) {
63+
return false;
64+
}
65+
}
66+
});
67+
}
68+
69+
/**
70+
* Execute the query and get the first result.
71+
*
72+
* @param array $columns
73+
* @return \IgniteKit\Backports\Database\Eloquent\Model|object|static|null
74+
*/
75+
public function first($columns = ['*'])
76+
{
77+
return $this->take(1)->get($columns)->first();
78+
}
79+
80+
/**
81+
* Apply the callback's query changes if the given "value" is true.
82+
*
83+
* @param mixed $value
84+
* @param callable $callback
85+
* @param callable|null $default
86+
* @return mixed|$this
87+
*/
88+
public function when($value, $callback, $default = null)
89+
{
90+
if ($value) {
91+
return $callback($this, $value) ?: $this;
92+
} elseif ($default) {
93+
return $default($this, $value) ?: $this;
94+
}
95+
96+
return $this;
97+
}
98+
99+
/**
100+
* Pass the query to a given callback.
101+
*
102+
* @param callable $callback
103+
* @return \IgniteKit\Backports\Database\Query\Builder
104+
*/
105+
public function tap($callback)
106+
{
107+
return $this->when(true, $callback);
108+
}
109+
110+
/**
111+
* Apply the callback's query changes if the given "value" is false.
112+
*
113+
* @param mixed $value
114+
* @param callable $callback
115+
* @param callable|null $default
116+
* @return mixed|$this
117+
*/
118+
public function unless($value, $callback, $default = null)
119+
{
120+
if (! $value) {
121+
return $callback($this, $value) ?: $this;
122+
} elseif ($default) {
123+
return $default($this, $value) ?: $this;
124+
}
125+
126+
return $this;
127+
}
128+
129+
/**
130+
* Create a new length-aware paginator instance.
131+
*
132+
* @param \IgniteKit\Backports\Support\Collection $items
133+
* @param int $total
134+
* @param int $perPage
135+
* @param int $currentPage
136+
* @param array $options
137+
* @return \IgniteKit\Backports\Pagination\LengthAwarePaginator
138+
*/
139+
protected function paginator($items, $total, $perPage, $currentPage, $options)
140+
{
141+
return Container::getInstance()->makeWith(LengthAwarePaginator::class, compact(
142+
'items', 'total', 'perPage', 'currentPage', 'options'
143+
));
144+
}
145+
146+
/**
147+
* Create a new simple paginator instance.
148+
*
149+
* @param \IgniteKit\Backports\Support\Collection $items
150+
* @param int $perPage
151+
* @param int $currentPage
152+
* @param array $options
153+
* @return \IgniteKit\Backports\Pagination\Paginator
154+
*/
155+
protected function simplePaginator($items, $perPage, $currentPage, $options)
156+
{
157+
return Container::getInstance()->makeWith(Paginator::class, compact(
158+
'items', 'perPage', 'currentPage', 'options'
159+
));
160+
}
161+
}

0 commit comments

Comments
 (0)