Skip to content

Commit 4be5ace

Browse files
committed
feat: support for cycle/annotated v4.x
1 parent 5e47aef commit 4be5ace

File tree

10 files changed

+233
-144
lines changed

10 files changed

+233
-144
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"require": {
1818
"php": "^8.2",
1919
"ext-pdo": "*",
20-
"cycle/annotated": "^3.5",
20+
"cycle/annotated": "^4.1",
2121
"cycle/database": "^2.8",
2222
"cycle/entity-behavior": "^1.3",
2323
"cycle/migrations": "^4.2",

composer.lock

Lines changed: 83 additions & 131 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/cycle.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,24 @@
5050
* Should class locator cache the results?
5151
*/
5252
'cache' => [
53-
'directory' => null,
54-
'enabled' => false,
53+
'enabled' => env('CYCLE_TOKENIZER_CACHE_TARGETS', true),
54+
'directory' => storage_path('framework/cache/cycle/tokenizer'),
55+
],
56+
57+
/*
58+
* What kind of classes should be loaded?
59+
*/
60+
'load' => [
61+
'classes' => env('CYCLE_TOKENIZER_LOAD_CLASSES', true),
62+
'enums' => env('CYCLE_TOKENIZER_LOAD_ENUMS', false),
63+
'interfaces' => env('CYCLE_TOKENIZER_LOAD_INTERFACES', false),
64+
],
65+
],
66+
67+
'attributes' => [
68+
'cache' => [
69+
'enabled' => env('CYCLE_ATTRIBUTES_CACHE', true),
70+
'store' => env('CYCLE_ATTRIBUTES_CACHE_DRIVER', 'file'),
5571
],
5672
],
5773

@@ -163,7 +179,7 @@
163179
*/
164180
'cache' => [
165181
'enabled' => env('CYCLE_SCHEMA_CACHE', true),
166-
'store' => env('CACHE_DRIVER', 'file'),
182+
'store' => env('CYCLE_SCHEMA_CACHE_DRIVER', 'file'),
167183
],
168184

169185
/*

src/Bridge/Laravel/Providers/CycleServiceProvider.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace WayOfDev\Cycle\Bridge\Laravel\Providers;
66

7+
use Cycle\ORM\ORM as CycleORM;
78
use Cycle\ORM\ORMInterface;
89
use Illuminate\Contracts\Config\Repository as IlluminateConfig;
910
use Illuminate\Support\ServiceProvider;
@@ -34,6 +35,7 @@ public function boot(): void
3435
$warmup = $config->get('cycle.warmup');
3536

3637
if (true === $warmup) {
38+
/** @var CycleORM $orm */
3739
$orm = $this->app->get(ORMInterface::class);
3840
$orm->prepareServices();
3941
}
@@ -51,7 +53,8 @@ public function register(): void
5153

5254
$registrators = [
5355
Registrators\RegisterConfigs::class,
54-
Registrators\RegisterClassesInterface::class,
56+
Registrators\RegisterTokenizer::class,
57+
Registrators\RegisterAttributes::class,
5558
Registrators\RegisterAnnotated::class,
5659
Registrators\RegisterDatabase::class,
5760
Registrators\RegisterSchema::class,

src/Bridge/Laravel/Providers/Registrator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ interface Registrator
99
public const CFG_KEY = 'cycle';
1010
public const CFG_KEY_DATABASE = 'cycle.database';
1111
public const CFG_KEY_TOKENIZER = 'cycle.tokenizer';
12+
public const CFG_KEY_ATTRIBUTES = 'cycle.attributes';
1213
public const CFG_KEY_MIGRATIONS = 'cycle.migrations';
1314
public const CFG_KEY_SCHEMA = 'cycle.schema';
1415
public const CFG_KEY_WARMUP = 'cycle.warmup';

src/Bridge/Laravel/Providers/Registrators/RegisterAnnotated.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,40 @@
66

77
use Cycle\Annotated;
88
use Illuminate\Contracts\Foundation\Application;
9-
use Spiral\Attributes\Factory;
109
use Spiral\Attributes\ReaderInterface;
1110
use Spiral\Tokenizer\ClassesInterface;
1211

1312
/**
14-
* @see https://github.com/spiral/cycle-bridge/blob/2.0/src/Bootloader/AnnotatedBootloader.php
13+
* @see https://github.com/spiral/cycle-bridge/blob/2.x/src/Bootloader/AnnotatedBootloader.php
1514
*/
1615
final class RegisterAnnotated
1716
{
1817
public function __invoke(Application $app): void
1918
{
20-
$app->bind(ReaderInterface::class, function () {
21-
return (new Factory())->create();
19+
$app->singleton(Annotated\Locator\EmbeddingLocatorInterface::class, function ($app) {
20+
return new Annotated\Locator\TokenizerEmbeddingLocator(
21+
$app->get(ClassesInterface::class),
22+
$app->get(ReaderInterface::class)
23+
);
24+
});
25+
26+
$app->singleton(Annotated\Locator\EntityLocatorInterface::class, function ($app) {
27+
return new Annotated\Locator\TokenizerEntityLocator(
28+
$app->get(ClassesInterface::class),
29+
$app->get(ReaderInterface::class)
30+
);
2231
});
2332

2433
$app->bind(Annotated\Embeddings::class, function ($app) {
2534
return new Annotated\Embeddings(
26-
$app->get(ClassesInterface::class),
35+
$app->get(Annotated\Locator\EmbeddingLocatorInterface::class),
2736
$app->get(ReaderInterface::class)
2837
);
2938
});
3039

3140
$app->bind(Annotated\Entities::class, function ($app) {
3241
return new Annotated\Entities(
33-
$app->get(ClassesInterface::class),
42+
$app->get(Annotated\Locator\EntityLocatorInterface::class),
3443
$app->get(ReaderInterface::class)
3544
);
3645
});
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WayOfDev\Cycle\Bridge\Laravel\Providers\Registrators;
6+
7+
use Illuminate\Contracts\Cache\Factory as CacheFactory;
8+
use Illuminate\Contracts\Foundation\Application;
9+
use Spiral\Attributes\AttributeReader;
10+
use Spiral\Attributes\Internal\Instantiator\InstantiatorInterface;
11+
use Spiral\Attributes\Internal\Instantiator\NamedArgumentsInstantiator;
12+
use Spiral\Attributes\Psr16CachedReader;
13+
use Spiral\Attributes\ReaderInterface;
14+
15+
/**
16+
* @see https://github.com/spiral/framework/blob/master/src/Framework/Bootloader/Attributes/AttributesBootloader.php
17+
*/
18+
final class RegisterAttributes
19+
{
20+
public function __invoke(Application $app): void
21+
{
22+
$app->singleton(InstantiatorInterface::class, function () {
23+
return new NamedArgumentsInstantiator();
24+
});
25+
26+
$app->singleton(ReaderInterface::class, function (Application $app): ReaderInterface {
27+
$reader = new AttributeReader($app->get(InstantiatorInterface::class));
28+
29+
/** @var bool $cacheEnabled */
30+
$cacheEnabled = config('cycle.attributes.cache.enabled', false);
31+
32+
if ($cacheEnabled) {
33+
/** @var CacheFactory $cacheFactory */
34+
$cacheFactory = $app->get(CacheFactory::class);
35+
$cacheStore = config('cycle.attributes.cache.store', 'file');
36+
$reader = new Psr16CachedReader($reader, $cacheFactory->store($cacheStore));
37+
}
38+
39+
return $reader;
40+
});
41+
}
42+
}

src/Bridge/Laravel/Providers/Registrators/RegisterClassesInterface.php renamed to src/Bridge/Laravel/Providers/Registrators/RegisterTokenizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/**
1818
* @see https://github.com/spiral/tokenizer/blob/master/src/Bootloader/TokenizerBootloader.php
1919
*/
20-
final class RegisterClassesInterface
20+
final class RegisterTokenizer
2121
{
2222
public function __invoke(Application $app): void
2323
{
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WayOfDev\Tests\Bridge\Laravel\Providers\Registrators;
6+
7+
use Psr\Container\ContainerExceptionInterface;
8+
use Psr\Container\NotFoundExceptionInterface;
9+
use Spiral\Attributes\AttributeReader;
10+
use Spiral\Attributes\Internal\Instantiator\InstantiatorInterface;
11+
use Spiral\Attributes\Psr16CachedReader;
12+
use Spiral\Attributes\ReaderInterface;
13+
use WayOfDev\Tests\TestCase;
14+
15+
class RegisterAttributesTest extends TestCase
16+
{
17+
/**
18+
* @test
19+
*/
20+
public function it_registers_instantiator_interface_as_singleton(): void
21+
{
22+
try {
23+
$instantiator1 = $this->app->get(InstantiatorInterface::class);
24+
$instantiator2 = $this->app->get(InstantiatorInterface::class);
25+
} catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
26+
$this::fail($e->getMessage());
27+
}
28+
29+
$this::assertInstanceOf(
30+
InstantiatorInterface::class,
31+
$instantiator1
32+
);
33+
34+
$this::assertSame($instantiator1, $instantiator2);
35+
}
36+
37+
/**
38+
* @test
39+
*/
40+
public function it_uses_cached_reader(): void
41+
{
42+
try {
43+
$reader = $this->app->get(ReaderInterface::class);
44+
} catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
45+
$this::fail($e->getMessage());
46+
}
47+
48+
$this::assertInstanceOf(Psr16CachedReader::class, $reader);
49+
}
50+
51+
/**
52+
* @test
53+
*/
54+
public function it_uses_attribute_reader_if_cache_disabled(): void
55+
{
56+
config()->set('cycle.attributes.cache.enabled', false);
57+
58+
try {
59+
$reader = $this->app->get(ReaderInterface::class);
60+
} catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
61+
$this::fail($e->getMessage());
62+
}
63+
64+
$this::assertInstanceOf(AttributeReader::class, $reader);
65+
}
66+
}

tests/src/Bridge/Laravel/Providers/Registrators/RegisterClassesInterfaceTest.php renamed to tests/src/Bridge/Laravel/Providers/Registrators/RegisterTokenizerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Spiral\Tokenizer\Tokenizer;
1414
use WayOfDev\Tests\TestCase;
1515

16-
class RegisterClassesInterfaceTest extends TestCase
16+
class RegisterTokenizerTest extends TestCase
1717
{
1818
/**
1919
* @test

0 commit comments

Comments
 (0)