Skip to content

Commit 1738aea

Browse files
committed
feat: add integration with cache, sessions, queue
1 parent b349e16 commit 1738aea

25 files changed

+653
-63
lines changed

config/cycle.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,4 +324,25 @@
324324
'entityBehavior' => [
325325
'register' => env('CYCLE_REGISTER_ENTITY_BEHAVIOUR', true),
326326
],
327+
328+
'integrations' => [
329+
/*
330+
* Enables migration generation for Laravel Queues
331+
*/
332+
'queue' => [
333+
'enabled' => env('CYCLE_ADAPTER_QUEUE_INTEGRATION', true),
334+
],
335+
/*
336+
* Enables migration generation for Laravel Sessions
337+
*/
338+
'session' => [
339+
'enabled' => env('CYCLE_ADAPTER_SESSION_INTEGRATION', true),
340+
],
341+
/*
342+
* Enables migration generation for Laravel Cache
343+
*/
344+
'cache' => [
345+
'enabled' => env('CYCLE_ADAPTER_CACHE_INTEGRATION', true),
346+
],
347+
],
327348
];

docs/pages/services/_meta.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
"testing": "Testing",
55
"validation": "Validation",
66
"pagination": "Pagination",
7+
"sessions": "Sessions",
8+
"queue": "Queue",
9+
"cache": "Cache",
710
"laravel-telescope": "Laravel Telescope"
811
}

docs/pages/services/cache.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Cache

docs/pages/services/laravel-telescope.mdx

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,30 @@ Install, telescope as usual, via Composer package manager:
2121
composer require laravel/telescope
2222
```
2323

24-
### Step 2: Publish Telescope Assets
24+
### Step 2: Add .env Configuration
25+
26+
Add the following configuration to your `.env` file:
27+
28+
```dotenv filename=".env"
29+
...
30+
31+
DB_USE_TELESCOPE_LOGGER=true
32+
33+
...
34+
```
35+
36+
### Step 3: Publish Telescope Assets
2537

2638
After installing Telescope, publish its assets and migrations using the `telescope:install` Artisan command.
2739

2840
```bash
2941
php artisan telescope:install
3042
```
3143

32-
### Step 3: Run Telescope Migrations trough Cycle-ORM-Adapter
44+
### Step 4: Run Telescope Migrations trough Cycle-ORM-Adapter
3345

3446
After installing Telescope, you should also run the migrate command in order to create the tables needed to store Telescope's data, but as you are using Cycle ORM, you should avoid using default `php artisan migrate` command, and instead, do the following steps:
3547

36-
Edit `config/cycle.php` file and add the following code to the `tokenizer.directories` array:
37-
38-
```php {7} filename="config/cycle.php"
39-
return [
40-
// ...
41-
'tokenizer' => [
42-
// ...
43-
'directories' => [
44-
app_path(),
45-
__DIR__ . '/../vendor/wayofdev/laravel-cycle-orm-adapter/src/Bridge/Telescope/Entities',
46-
],
47-
],
48-
],
49-
```
50-
5148
Run the following command to create the Telescope tables:
5249

5350
```bash
@@ -56,8 +53,7 @@ php artisan cycle:migrate:init
5653
php artisan cycle:orm:migrate --split --run
5754
```
5855

59-
60-
### Step 4: Add the CycleORM Query Watcher
56+
### Step 5: Add the CycleORM Query Watcher
6157

6258
Next, edit your `config/telescope.php` configuration file and add the following lines to the `watchers` array, right after the default`Watchers\QueryWatcher::class` line:
6359

@@ -85,18 +81,6 @@ return [
8581
];
8682
```
8783

88-
### Step 5: Add .env Configuration
89-
90-
Add the following configuration to your `.env` file:
91-
92-
```dotenv filename=".env"
93-
...
94-
95-
DB_USE_TELESCOPE_LOGGER=true
96-
97-
...
98-
```
99-
10084
### Step 6: Access Laravel Telescope
10185

10286
Finally, you may access the Telescope dashboard via the `/telescope` route. Of course, don't forget to start your Laravel application:

docs/pages/services/pagination.mdx

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,68 @@ Pagination is provided by external package [wayofdev/laravel-paginator](https://
88
composer require wayofdev/laravel-paginator
99
```
1010

11-
## Usage
11+
## Usage with Repositories
1212

1313
<div className="steps-container">
1414

1515
### Step 1: Define `paginate()` method in your Repository
1616

17-
@todo
17+
Create a `paginate()` method in your abstract repository class that will return a `CyclePaginator` instance.
18+
19+
```php filename="app/Infrastructure/Persistence/Cycle/Repository.php"
20+
<?php
21+
22+
declare(strict_types=1);
23+
24+
namespace Infrastructure\Persistence\Cycle;
25+
26+
use Cycle\ORM\EntityManagerInterface;
27+
use Cycle\ORM\Select;
28+
use Cycle\ORM\Select\Repository as CycleRepository;
29+
use Illuminate\Support\Collection;
30+
use Spiral\Pagination\Paginator as SpiralPaginator;
31+
use WayOfDev\Paginator\CyclePaginator;
32+
33+
class Repository extends CycleRepository
34+
{
35+
/**
36+
* Create repository linked to one specific selector.
37+
*
38+
* @param Select<TEntity> $select
39+
*/
40+
public function __construct(
41+
protected Select $select,
42+
protected EntityManagerInterface $entityManager
43+
) {
44+
parent::__construct($select);
45+
}
46+
47+
// ...
48+
49+
public function paginate(int $perPage = 20, int $page = 1, string $pageName = 'page'): CyclePaginator
50+
{
51+
return $this->paginateQuery(
52+
$this->select(),
53+
$perPage,
54+
$page,
55+
$pageName,
56+
);
57+
}
58+
59+
protected function paginateQuery(Select $query, int $perPage = 20, int $page = 1, string $pageName = 'page'): CyclePaginator
60+
{
61+
return new CyclePaginator(
62+
(new SpiralPaginator($perPage))->withPage($page)->paginate($query),
63+
$this->createCollection($query->fetchAll()),
64+
$pageName,
65+
);
66+
}
67+
68+
protected function createCollection(iterable $items): Collection
69+
{
70+
return new Collection($items);
71+
}
72+
}
73+
```
1874

1975
</div>

docs/pages/services/queue.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Queue

docs/pages/services/sessions.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Sessions

phpunit.xml.dist

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313
<server name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/>
1414
<server name="APP_ENV" value="testing"/>
1515
<server name="CACHE_DRIVER" value="array"/>
16+
<server name="CYCLE_ADAPTER_QUEUE_INTEGRATION" value="false"/>
17+
<server name="CYCLE_ADAPTER_SESSION_INTEGRATION" value="false"/>
18+
<server name="CYCLE_ADAPTER_CACHE_INTEGRATION" value="false"/>
19+
20+
<server name="CYCLE_ATTRIBUTES_CACHE" value="true"/>
21+
<server name="CYCLE_ATTRIBUTES_CACHE_DRIVER" value="array"/>
22+
23+
<server name="CYCLE_SCHEMA_CACHE" value="true"/>
24+
<server name="CYCLE_SCHEMA_CACHE_DRIVER" value="array"/>
1625
</php>
1726
<testsuites>
1827
<testsuite name="Tests">

src/Bridge/Cache/Entities/Cache.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WayOfDev\Cycle\Bridge\Cache\Entities;
6+
7+
use Cycle\Annotated\Annotation\Column;
8+
use Cycle\Annotated\Annotation\Entity;
9+
10+
#[Entity(table: 'cache')]
11+
class Cache
12+
{
13+
#[Column(type: 'string', primary: true)]
14+
public string $key;
15+
16+
#[Column(type: 'longText')]
17+
public string $text;
18+
19+
#[Column(type: 'integer')]
20+
public int $expiration;
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WayOfDev\Cycle\Bridge\Cache\Entities;
6+
7+
use Cycle\Annotated\Annotation\Column;
8+
use Cycle\Annotated\Annotation\Entity;
9+
10+
#[Entity(table: 'cache_locks')]
11+
class CacheLock
12+
{
13+
#[Column(type: 'string', primary: true)]
14+
public string $key;
15+
16+
#[Column(type: 'string')]
17+
public string $owner;
18+
19+
#[Column(type: 'integer')]
20+
public int $expiration;
21+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WayOfDev\Cycle\Bridge\Cache\Providers;
6+
7+
use Illuminate\Config\Repository;
8+
use Illuminate\Support\ServiceProvider;
9+
use Psr\Container\ContainerExceptionInterface;
10+
use Psr\Container\NotFoundExceptionInterface;
11+
12+
use function array_merge;
13+
14+
class CacheServiceProvider extends ServiceProvider
15+
{
16+
/**
17+
* @throws ContainerExceptionInterface
18+
* @throws NotFoundExceptionInterface
19+
*/
20+
public function register(): void
21+
{
22+
/** @var Repository $config */
23+
$config = $this->app->get(Repository::class);
24+
25+
$config->set('cycle.tokenizer.directories', array_merge(
26+
$config->get('cycle.tokenizer.directories', []),
27+
[__DIR__ . '/../Entities']
28+
));
29+
}
30+
}

src/Bridge/Laravel/Providers/CycleServiceProvider.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
use Illuminate\Support\ServiceProvider;
1111
use Psr\Container\ContainerExceptionInterface;
1212
use Psr\Container\NotFoundExceptionInterface;
13+
use WayOfDev\Cycle\Bridge\Cache\Providers\CacheServiceProvider;
1314
use WayOfDev\Cycle\Bridge\Laravel\Console\Commands\Database;
1415
use WayOfDev\Cycle\Bridge\Laravel\Console\Commands\Migrations;
1516
use WayOfDev\Cycle\Bridge\Laravel\Console\Commands\ORM;
17+
use WayOfDev\Cycle\Bridge\Queue\Providers\QueueServiceProvider;
18+
use WayOfDev\Cycle\Bridge\Session\Providers\SessionServiceProvider;
19+
use WayOfDev\Cycle\Bridge\Telescope\Providers\TelescopeServiceProvider;
1620

1721
final class CycleServiceProvider extends ServiceProvider
1822
{
@@ -65,6 +69,24 @@ public function register(): void
6569
foreach ($registrators as $registrator) {
6670
(new $registrator())($this->app);
6771
}
72+
73+
$this->registerIntegrations();
74+
}
75+
76+
private function registerIntegrations(): void
77+
{
78+
$services = [
79+
'cycle.integrations.session.enabled' => SessionServiceProvider::class,
80+
'cycle.integrations.cache.enabled' => CacheServiceProvider::class,
81+
'cycle.integrations.queue.enabled' => QueueServiceProvider::class,
82+
'cycle.database.logger.use_telescope' => TelescopeServiceProvider::class,
83+
];
84+
85+
foreach ($services as $configKey => $providerClass) {
86+
if (config($configKey) === true) {
87+
$this->app->register($providerClass);
88+
}
89+
}
6890
}
6991

7092
private function registerConsoleCommands(): void
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WayOfDev\Cycle\Bridge\Queue\Entities;
6+
7+
use Cycle\Annotated\Annotation\Column;
8+
use Cycle\Annotated\Annotation\Entity;
9+
use Cycle\Annotated\Annotation\Table\Index;
10+
use DateTimeImmutable;
11+
12+
#[Index(columns: ['uuid'], unique: true)]
13+
#[Entity(table: 'failed_jobs')]
14+
class FailedJob
15+
{
16+
#[Column(type: 'primary')]
17+
public int $id;
18+
19+
#[Column(type: 'string')]
20+
public string $uuid;
21+
22+
#[Column(type: 'text')]
23+
public string $connection;
24+
25+
#[Column(type: 'text')]
26+
public string $queue;
27+
28+
#[Column(type: 'longText')]
29+
public string $payload;
30+
31+
#[Column(type: 'longText')]
32+
public string $exception;
33+
34+
#[Column(type: 'datetime', default: 'CURRENT_TIMESTAMP')]
35+
public DateTimeImmutable $failedAt;
36+
}

src/Bridge/Queue/Entities/Job.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WayOfDev\Cycle\Bridge\Queue\Entities;
6+
7+
use Cycle\Annotated\Annotation\Column;
8+
use Cycle\Annotated\Annotation\Entity;
9+
use Cycle\Annotated\Annotation\Table\Index;
10+
11+
#[Index(columns: ['queue'])]
12+
#[Entity(table: 'jobs')]
13+
class Job
14+
{
15+
#[Column(type: 'primary')]
16+
public int $id;
17+
18+
#[Column(type: 'string')]
19+
public string $queue;
20+
21+
#[Column(type: 'longText')]
22+
public string $payload;
23+
24+
#[Column(type: 'tinyInteger', unsigned: true)]
25+
public int $attempts;
26+
27+
#[Column(type: 'timestamp', nullable: true)]
28+
public ?int $reservedAt;
29+
30+
#[Column(type: 'timestamp')]
31+
public int $availableAt;
32+
33+
#[Column(type: 'timestamp')]
34+
public int $createdAt;
35+
}

0 commit comments

Comments
 (0)