Skip to content

Commit 5c16d0f

Browse files
committed
init
1 parent 265bdc9 commit 5c16d0f

25 files changed

+1971
-0
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false
13+
14+
[*.yml]
15+
indent_size = 2

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea/
2+
vendor/
3+
composer.lock
4+
.phpunit.result.cache

README.md

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
# Cycle ORM integration for the Laravel Framework
2+
3+
**!!! Important note!!!**
4+
At this moment this package work only with postgres database driver.
5+
6+
### Requirements
7+
- Laravel 7.x
8+
- PHP 7.4 and above
9+
10+
## Installation and Configuration
11+
12+
From the command line run
13+
```shell script
14+
composer require butschster/cycle-orm
15+
```
16+
17+
### Env variables
18+
19+
```
20+
DB_CONNECTION=postgres
21+
DB_HOST=127.0.0.1
22+
DB_PORT= # Default port 5432
23+
DB_DATABASE=homestead
24+
DB_USERNAME=root
25+
DB_PASSWORD=
26+
27+
DB_MIGRATIONS_TABLE=migrations # Migrations table name
28+
29+
DB_SCHEMA_SYNC=false # Sync DB schema without migrations
30+
DB_SCHEMA_CACHE=true # Cache DB schema
31+
DB_SCHEMA_CACHE_DRIVER=file # DB schema cache driver
32+
```
33+
34+
### Configuration
35+
36+
Publish the config file.
37+
```shell script
38+
php artisan vendor:publish --provider="Butschster\Cycle\Providers\LaravelServiceProvider" --tag=config
39+
```
40+
41+
**That's it!**
42+
43+
### Console commands
44+
45+
#### php artisan cycle:migrate
46+
Run cycle orm migrations from directory.
47+
48+
#### php artisan cycle:refresh
49+
Refresh database schema.
50+
51+
## Usage
52+
By default, class locator looks for entities in app folder. You can specify locations in `config/cycle.php` config file.
53+
54+
```php
55+
...
56+
'directories' => [
57+
app_path(),
58+
],
59+
...
60+
```
61+
62+
### Example
63+
64+
#### User
65+
```php
66+
<?php
67+
namespace App;
68+
69+
use Illuminate\Contracts\Auth\Authenticatable;
70+
71+
/**
72+
* @ORM\Entity(
73+
* table="users",
74+
* repository="App\UserRepository",
75+
* )
76+
*/
77+
class User implements Authenticatable
78+
{
79+
/** @ORM\Column(type="uuid", primary=true) */
80+
protected string $id;
81+
82+
/** @ORM\Column(type="string") */
83+
protected string $password;
84+
85+
/** @ORM\Column(type="string") */
86+
protected string $rememberToken = '';
87+
88+
public function getId(): string
89+
{
90+
return $this->id;
91+
}
92+
93+
/**
94+
* @return string
95+
*/
96+
public function getAuthIdentifierName(): string
97+
{
98+
return 'id';
99+
}
100+
101+
/**
102+
* @return string
103+
*/
104+
public function getAuthIdentifier(): string
105+
{
106+
return $this->getId();
107+
}
108+
109+
/**
110+
* @return string
111+
*/
112+
public function getAuthPassword(): string
113+
{
114+
return $this->password;
115+
}
116+
117+
/**
118+
* @param string $password
119+
*/
120+
public function setAuthPassword(string $password): void
121+
{
122+
$this->password = $password;
123+
}
124+
125+
/**
126+
* @return string
127+
*/
128+
public function getRememberToken(): string
129+
{
130+
return $this->rememberToken;
131+
}
132+
133+
/**
134+
* @param string $value
135+
*/
136+
public function setRememberToken($value): void
137+
{
138+
$this->rememberToken = $value;
139+
}
140+
141+
/**
142+
* @return string
143+
*/
144+
public function getRememberTokenName(): string
145+
{
146+
return 'rememberToken';
147+
}
148+
}
149+
```
150+
151+
#### Repository
152+
```php
153+
namespace App;
154+
155+
use Butschster\Cycle\Repository;
156+
157+
class UserRepository extends Repository
158+
{
159+
/** @inheritDoc */
160+
public function findByUsername(string $username): ?User
161+
{
162+
return $this->findOne(['username' => $username]);
163+
}
164+
}
165+
```
166+
167+
#### Create user
168+
```php
169+
use Cycle\ORM\ORMInterface;
170+
use Butschster\Cycle\Facades\ORM;
171+
172+
$user = new User();
173+
$repository = app(ORMInterface::class)->getRepository($user);
174+
// or
175+
$repository = ORM::getRepository($user);
176+
// or
177+
$repository = ORM::getRepository(User::class);
178+
179+
$repository->persist($user);
180+
```
181+
182+
#### Update user
183+
```php
184+
use Butschster\Cycle\Facades\ORM;
185+
186+
$repository = ORM::getRepository(User::class);
187+
$user = $repository->findByPK('5c9e177b0a975a6eeccf5960');
188+
$user->setAuthPassword('secret');
189+
190+
$repository->persist($user);
191+
```
192+
193+
#### Delete user
194+
```php
195+
use Butschster\Cycle\Facades\ORM;
196+
197+
$repository = ORM::getRepository(User::class);
198+
$user = $repository->findByPK('5c9e177b0a975a6eeccf5960');
199+
200+
$repository->delete($user);
201+
```

composer.json

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"name": "butschster/cycle-orm",
3+
"description": "Cycle ORM integration for the Laravel Framework",
4+
"keywords": [
5+
"laravel",
6+
"lumen",
7+
"orm",
8+
"database",
9+
"cycle"
10+
],
11+
"license": "MIT",
12+
"authors": [
13+
{
14+
"name": "Pavel Buchnev",
15+
"email": "[email protected]"
16+
}
17+
],
18+
"require": {
19+
"php": "^7.4",
20+
"illuminate/config": "^7.0",
21+
"illuminate/database": "^7.0",
22+
"illuminate/support": "^7.0",
23+
"illuminate/contracts": "^7.0",
24+
"illuminate/console": "^7.0",
25+
"illuminate/pagination": "^7.0",
26+
"cycle/annotated": "^2.0",
27+
"cycle/migrations": "^1.0",
28+
"cycle/orm": "^1.2",
29+
"cycle/proxy-factory": "^1.2.1",
30+
"cycle/schema-builder": "^1.1"
31+
},
32+
"require-dev": {
33+
"fzaninotto/faker": "^1.9.1",
34+
"mockery/mockery": "^1.0",
35+
"phpunit/phpunit": "^7.0"
36+
},
37+
"autoload": {
38+
"psr-4": {
39+
"Butschster\\Cycle\\": "src/"
40+
}
41+
},
42+
"autoload-dev": {
43+
"psr-4": {
44+
"Butschster\\Tests\\": "tests/"
45+
}
46+
},
47+
"extra": {
48+
"laravel": {
49+
"providers": [
50+
"Butschster\\Cycle\\Providers\\LaravelServiceProvider"
51+
]
52+
}
53+
},
54+
"minimum-stability": "dev",
55+
"prefer-stable": true
56+
}

config/cycle.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
use Cycle\ORM\Schema;
4+
5+
return [
6+
'directories' => [
7+
app_path(),
8+
],
9+
10+
'database' => [
11+
'default' => 'default',
12+
13+
'databases' => [
14+
'default' => [
15+
'connection' => env('DB_CONNECTION', 'postgres'),
16+
],
17+
],
18+
19+
'connections' => [
20+
'postgres' => [
21+
'driver' => Spiral\Database\Driver\Postgres\PostgresDriver::class,
22+
'options' => [
23+
'connection' => sprintf(
24+
'pgsql:host=%s;port=%d;dbname=%s;',
25+
env('DB_HOST', '127.0.0.1'),
26+
env('DB_PORT', '5432'),
27+
env('DB_DATABASE', 'homestead')
28+
),
29+
'username' => env('DB_USERNAME', 'root'),
30+
'password' => env('DB_PASSWORD'),
31+
],
32+
],
33+
],
34+
],
35+
36+
'schema' => [
37+
// Sync db schema with database without migrations
38+
'sync' => env('DB_SCHEMA_SYNC', false),
39+
40+
// Cache schema
41+
// Кеширование схемы. После изменение сущности необходимо будет сбрасывать схему
42+
'cache' => [
43+
'storage' => env('DB_SCHEMA_CACHE_DRIVER', 'file'),
44+
'enabled' => (bool)env('DB_SCHEMA_CACHE', true),
45+
],
46+
47+
'defaults' => [
48+
Schema::MAPPER => Butschster\Cycle\Mapper::class,
49+
Schema::REPOSITORY => Butschster\Cycle\Repository::class,
50+
Schema::SOURCE => Cycle\ORM\Select\Source::class,
51+
],
52+
],
53+
54+
'migrations' => [
55+
'directory' => database_path('migrations/cycle/'),
56+
'table' => env('DB_MIGRATIONS_TABLE', 'migrations'),
57+
],
58+
59+
// https://cycle-orm.dev/docs/advanced-promise#proxies-and-promises
60+
'relations' => [
61+
'materializer' => [
62+
'driver' => env('DB_MATERIALIZER_DRIVER', 'eval'),
63+
'drivers' => [
64+
'file' => [
65+
'path' => storage_path('framework/cache/entities'),
66+
]
67+
]
68+
],
69+
],
70+
];

phpunit.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false">
11+
<testsuites>
12+
<testsuite name="Unit">
13+
<directory suffix="Test.php">./tests</directory>
14+
</testsuite>
15+
</testsuites>
16+
<filter>
17+
<whitelist>
18+
<directory suffix=".php">./src</directory>
19+
</whitelist>
20+
</filter>
21+
</phpunit>

0 commit comments

Comments
 (0)