Skip to content

Commit 36ae3f9

Browse files
committed
feat: wip of service provider and tests
1 parent 75118a3 commit 36ae3f9

File tree

8 files changed

+124
-16
lines changed

8 files changed

+124
-16
lines changed

Makefile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ endif
3232
MAKE_LOGFILE = /tmp/wayofdev-laravel-cycle-orm-adapter.log
3333
MAKE_CMD_COLOR := $(BLUE)
3434

35-
# https://phpstan.org/user-guide/output-format
36-
export PHPSTAN_OUTPUT_FORMAT ?= github
37-
3835
help:
3936
@echo 'Management commands for package:'
4037
@echo 'Usage:'

config/cycle.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
app_path(),
1717
],
1818

19-
'databases' => [
19+
'database' => [
2020
'default' => 'default',
2121

22+
'aliases' => [],
23+
2224
'databases' => [
2325
'default' => [
2426
'connection' => 'sqlite',

src/Bridge/Laravel/Providers/CycleServiceProvider.php

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,16 @@
55
namespace WayOfDev\Cycle\Bridge\Laravel\Providers;
66

77
use Cycle\Database\Config\DatabaseConfig;
8+
use Cycle\Database\DatabaseManager;
9+
use Cycle\Database\DatabaseProviderInterface;
810
use Illuminate\Support\ServiceProvider;
11+
use WayOfDev\Cycle\Config;
12+
use WayOfDev\Cycle\Contracts\Config\Repository as ConfigRepository;
13+
use WayOfDev\Cycle\Contracts\EntityManager;
14+
use WayOfDev\Cycle\Entity\Manager;
915

1016
final class CycleServiceProvider extends ServiceProvider
1117
{
12-
public function register(): void
13-
{
14-
$this->mergeConfigFrom(
15-
__DIR__ . '/../../../../config/cycle.php',
16-
'cycle'
17-
);
18-
}
19-
2018
public function boot(): void
2119
{
2220
if ($this->app->runningInConsole()) {
@@ -28,18 +26,59 @@ public function boot(): void
2826
}
2927
}
3028

29+
public function register(): void
30+
{
31+
$this->mergeConfigFrom(
32+
__DIR__ . '/../../../../config/cycle.php',
33+
'cycle'
34+
);
35+
36+
$this->registerAdapterConfig();
37+
$this->registerDatabaseConfig();
38+
$this->registerDatabaseManager();
39+
$this->registerEntityManager();
40+
}
41+
3142
private function registerConsoleCommands(): void
3243
{
3344
$this->commands([
45+
// ...
3446
]);
3547
}
3648

49+
private function registerAdapterConfig(): void
50+
{
51+
$this->app->singleton(ConfigRepository::class, static function (): ConfigRepository {
52+
return Config::fromArray(
53+
config('cycle')
54+
);
55+
});
56+
}
57+
3758
private function registerDatabaseConfig(): void
3859
{
3960
$this->app->singleton(DatabaseConfig::class, static function (): DatabaseConfig {
4061
return new DatabaseConfig(
41-
// sjuda podatj nastrojki iz konfiga !!!
62+
config('cycle.database')
63+
);
64+
});
65+
}
66+
67+
private function registerDatabaseManager(): void
68+
{
69+
$this->app->singleton(DatabaseProviderInterface::class, function ($app): DatabaseProviderInterface {
70+
return new DatabaseManager(
71+
$app[DatabaseConfig::class]
4272
);
4373
});
74+
75+
$this->app->alias(DatabaseProviderInterface::class, DatabaseManager::class);
76+
}
77+
78+
private function registerEntityManager(): void
79+
{
80+
$this->app->singleton(EntityManager::class, function ($app): EntityManager {
81+
return $app[Manager::class];
82+
});
4483
}
4584
}

src/Config.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final class Config implements Repository
1616
{
1717
private const REQUIRED_FIELDS = [
1818
'directories',
19-
'databases',
19+
'database',
2020
'schema',
2121
'migrations',
2222
'relations',
@@ -34,7 +34,7 @@ public static function fromArray(array $config): self
3434

3535
return new self(
3636
$config['directories'],
37-
$config['databases'],
37+
$config['database'],
3838
$config['schema'],
3939
$config['migrations'],
4040
$config['relations']

src/Contracts/EntityManager.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WayOfDev\Cycle\Contracts;
6+
7+
interface EntityManager
8+
{
9+
public function persist(object $entity, bool $cascade = true): self;
10+
11+
public function delete(object $entity, bool $cascade = true): self;
12+
}

src/Entity/Manager.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WayOfDev\Cycle\Entity;
6+
7+
use Cycle\ORM\ORMInterface;
8+
use Cycle\ORM\RepositoryInterface;
9+
use WayOfDev\Cycle\Contracts\EntityManager;
10+
11+
final class Manager implements EntityManager
12+
{
13+
private ORMInterface $orm;
14+
15+
public function __construct(ORMInterface $orm)
16+
{
17+
$this->orm = $orm;
18+
}
19+
20+
public function persist(object $entity, bool $cascade = true): EntityManager
21+
{
22+
// TODO: Implement persist() method.
23+
}
24+
25+
public function delete(object $entity, bool $cascade = true): EntityManager
26+
{
27+
// TODO: Implement delete() method.
28+
}
29+
30+
private function getRepository(object $entity): RepositoryInterface
31+
{
32+
return $this->orm->getRepository($entity);
33+
}
34+
}

src/Repository.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WayOfDev\Cycle;
6+
7+
use Cycle\ORM\Select;
8+
use Cycle\ORM\Select\Repository as CycleRepository;
9+
10+
/**
11+
* Repository provides ability to load entities and construct queries.
12+
*
13+
* @template TEntity of object
14+
* @extends CycleRepository<TEntity>
15+
*/
16+
class Repository extends CycleRepository
17+
{
18+
public function __construct(Select $select)
19+
{
20+
$this->select = $select;
21+
22+
parent::__construct($select);
23+
}
24+
}

tests/ConfigTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function dataProviderForConfig(): array
1919
'/foo/bar',
2020
'/boo/bar/baz',
2121
],
22-
'databases' => [],
22+
'database' => [],
2323
'schema' => [
2424
'sync' => false,
2525
'cache' => [

0 commit comments

Comments
 (0)