Skip to content

Commit 01bf9d0

Browse files
author
Anton Komarev
authored
Release 4.0 (#14)
* Add isOwnedByDefaultOwner method to Ownable models * Upgrade package architecture
1 parent f4c2487 commit 01bf9d0

38 files changed

+463
-227
lines changed

.styleci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ disabled:
55
- phpdoc_no_package
66
- logical_not_operators_with_successor_space
77
- length_ordered_imports
8+
- simplified_null_return

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ language: php
33
sudo: false
44

55
php:
6-
- 5.6
76
- 7.0
87
- 7.1
8+
- 7.2
99

1010
env:
1111
global:

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22

33
All notable changes to `laravel-ownership` will be documented in this file.
44

5+
## [4.0.0] - 2017-09-09
6+
7+
### Added
8+
9+
- Ownable models got new `isOwnedByDefaultOwner` method which automatically try to resolve current user.
10+
11+
### Changed
12+
13+
- Contracts namespace changed from `Cog\Ownership\Contracts` to `Cog\Contracts\Laravel\Ownership`
14+
- Classes namespace changed from `Cog\Ownership` to `Cog\Laravel\Ownership`
15+
- `ModelObserver` renamed to `OwnableObserver`
16+
- `HasOwner` contract renamed to `Ownable`
17+
518
## [3.1.0] - 2017-08-30
619

720
### Added

README.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ And then include the service provider within `app/config/app.php`.
6060

6161
```php
6262
'providers' => [
63-
Cog\Ownership\Providers\OwnershipServiceProvider::class,
63+
Cog\Laravel\Ownership\Providers\OwnershipServiceProvider::class,
6464
];
6565
```
6666

@@ -76,14 +76,14 @@ Polymorphic ownership is useful when model can belongs to owners of different ty
7676

7777
### Prepare ownable model with strict ownership
7878

79-
Use `HasOwner` contract in model which will get ownership behavior and implement it or just use `HasOwner` trait.
79+
Use `Ownable` contract in model which will get ownership behavior and implement it or just use `HasOwner` trait.
8080

8181
```php
82-
use Cog\Ownership\Contracts\HasOwner as HasOwnerContract;
83-
use Cog\Ownership\Traits\HasOwner;
82+
use Cog\Contracts\Laravel\Ownership\Ownable as OwnableContract;
83+
use Cog\Laravel\Ownership\Traits\HasOwner;
8484
use Illuminate\Database\Eloquent\Model;
8585

86-
class Article extends Model implements HasOwnerContract
86+
class Article extends Model implements OwnableContract
8787
{
8888
use HasOwner;
8989
}
@@ -106,11 +106,11 @@ By default owner model will be the same as `config('auth.providers.users.model')
106106
To override default owner model in strict ownership, it's primary key or foreign key extend your ownable model with additional attributes:
107107

108108
```php
109-
use Cog\Ownership\Contracts\HasOwner as HasOwnerContract;
110-
use Cog\Ownership\Traits\HasOwner;
109+
use Cog\Contracts\Laravel\Ownership\Ownable as OwnableContract;
110+
use Cog\Laravel\Ownership\Traits\HasOwner;
111111
use Illuminate\Database\Eloquent\Model;
112112

113-
class Article extends Model implements HasOwnerContract
113+
class Article extends Model implements OwnableContract
114114
{
115115
use HasOwner;
116116

@@ -122,14 +122,14 @@ class Article extends Model implements HasOwnerContract
122122

123123
### Prepare ownable model with polymorphic ownership
124124

125-
Use `HasOwner` contract in model which will get polymorphic ownership behavior and implement it or just use `HasMorphOwner` trait.
125+
Use `Ownable` contract in model which will get polymorphic ownership behavior and implement it or just use `HasMorphOwner` trait.
126126

127127
```php
128-
use Cog\Ownership\Contracts\HasOwner as HasOwnerContract;
129-
use Cog\Ownership\Traits\HasMorphOwner;
128+
use Cog\Contracts\Laravel\Ownership\Ownable as OwnableContract;
129+
use Cog\Laravel\Ownership\Traits\HasMorphOwner;
130130
use Illuminate\Database\Eloquent\Model;
131131

132-
class Article extends Model implements HasOwnerContract
132+
class Article extends Model implements OwnableContract
133133
{
134134
use HasMorphOwner;
135135
}
@@ -209,7 +209,7 @@ $article->isNotOwnedBy($owner);
209209
#### Manually define default owner on model creation
210210

211211
```php
212-
$article = new Article();
212+
$article = new Article;
213213
$article->withDefaultOwner()->save();
214214
```
215215

@@ -219,14 +219,14 @@ Or provide concrete owner:
219219

220220
```php
221221
$user = User::where('name', 'admin')->first();
222-
$article = new Article();
222+
$article = new Article;
223223
$article->withDefaultOwner($user)->save();
224224
```
225225

226226
#### Skip defining default owner on model creation
227227

228228
```php
229-
$article = new Article();
229+
$article = new Article;
230230
$article->withoutDefaultOwner()->save();
231231
```
232232

@@ -249,11 +249,11 @@ Article::whereNotOwnedBy($owner)->get();
249249
To set currently authenticated user as owner for ownable model create - extend it with attribute `withDefaultOwnerOnCreate`. It works for both strict and polymorphic ownership behavior.
250250

251251
```php
252-
use Cog\Ownership\Contracts\HasOwner as HasOwnerContract;
253-
use Cog\Ownership\Traits\HasOwner;
252+
use Cog\Contracts\Laravel\Ownership\Ownable as OwnableContract;
253+
use Cog\Laravel\Ownership\Traits\HasOwner;
254254
use Illuminate\Database\Eloquent\Model;
255255

256-
class Article extends Model implements HasOwnerContract
256+
class Article extends Model implements OwnableContract
257257
{
258258
use HasOwner;
259259

@@ -264,11 +264,11 @@ class Article extends Model implements HasOwnerContract
264264
To override strategy of getting default owner extend ownable model with `resolveDefaultOwner` method:
265265

266266
```php
267-
use Cog\Ownership\Contracts\HasOwner as HasOwnerContract;
268-
use Cog\Ownership\Traits\HasOwner;
267+
use Cog\Contracts\Laravel\Ownership\Ownable as OwnableContract;
268+
use Cog\Laravel\Ownership\Traits\HasOwner;
269269
use Illuminate\Database\Eloquent\Model;
270270

271-
class Article extends Model implements HasOwnerContract
271+
class Article extends Model implements OwnableContract
272272
{
273273
use HasOwner;
274274

@@ -277,7 +277,7 @@ class Article extends Model implements HasOwnerContract
277277
/**
278278
* Resolve entity default owner.
279279
*
280-
* @return \Cog\Ownership\Contracts\CanBeOwner|null
280+
* @return \Cog\Contracts\Laravel\Ownership\CanBeOwner|null
281281
*/
282282
public function resolveDefaultOwner()
283283
{

UPGRADING.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
# Upgrade Guide
22

3-
- [Upgrading To 3.0 From 2.0](#upgrade-3.0)
3+
- [Upgrading From 3.0 To 4.0](#upgrade-4.0)
4+
- [Upgrading From 2.0 To 3.0](#upgrade-3.0)
45

56
<a name="upgrade-3.0"></a>
6-
## Upgrading To 3.0 From 2.0
7+
## Upgrading From 3.0 To 4.0
8+
9+
- Find all `Cog\Ownership\Contracts\HasOwner` and replace with `Cog\Contracts\Laravel\Ownership\Ownable`
10+
- Find all `Cog\Ownership\Observers\ModelObserver` and replace with `Cog\Laravel\Ownership\Observers\OwnableObserver`
11+
- Find all `Cog\Ownership\Contracts` and replace with `Cog\Contracts\Laravel\Ownership`
12+
- Find all `Cog\Ownership` and replace with `Cog\Laravel\Ownership`
13+
14+
<a name="upgrade-3.0"></a>
15+
## Upgrading From 2.0 To 3.0
716

817
You need to upgrade only if you have models with Strict Ownership and you are using default `owned_by` column names.
918

composer.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,19 @@
4747
"require-dev": {
4848
"friendsofphp/php-cs-fixer": "^1.11",
4949
"mockery/mockery": "^0.9.8",
50-
"orchestra/database": "~3.4.0",
51-
"orchestra/testbench": "~3.4.0",
52-
"phpunit/phpunit": "^5.7"
50+
"orchestra/database": "~3.5.0",
51+
"orchestra/testbench": "~3.5.0",
52+
"phpunit/phpunit": "^6.0"
5353
},
5454
"autoload": {
5555
"psr-4": {
56-
"Cog\\Ownership\\": "src/"
56+
"Cog\\Contracts\\Laravel\\Ownership\\": "contracts/",
57+
"Cog\\Laravel\\Ownership\\": "src/"
5758
}
5859
},
5960
"autoload-dev": {
6061
"psr-4": {
61-
"Cog\\Ownership\\Tests\\": "tests/"
62+
"Cog\\Tests\\Laravel\\Ownership\\": "tests/"
6263
}
6364
},
6465
"scripts": {

src/Contracts/CanBeOwner.php renamed to contracts/CanBeOwner.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Cog\Ownership\Contracts;
12+
namespace Cog\Contracts\Laravel\Ownership;
1313

1414
/**
1515
* Interface CanBeOwner.
1616
*
17-
* @package Cog\Ownership\Contracts
17+
* @package Cog\Contracts\Laravel\Ownership
1818
*/
1919
interface CanBeOwner
2020
{
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Laravel Ownership.
5+
*
6+
* (c) Anton Komarev <a.komarev@cybercog.su>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Cog\Contracts\Laravel\Ownership\Exceptions;
13+
14+
use Cog\Contracts\Laravel\Ownership\Ownable as OwnableContract;
15+
use Exception;
16+
17+
/**
18+
* Class InvalidDefaultOwner.
19+
*
20+
* @package Cog\Contracts\Laravel\Ownership\Exceptions
21+
*/
22+
class InvalidDefaultOwner extends Exception
23+
{
24+
/**
25+
* Default owner for ownable model is null.
26+
*
27+
* @param \Cog\Contracts\Laravel\Ownership\Ownable $ownable
28+
* @return static
29+
*/
30+
public static function isNull(OwnableContract $ownable)
31+
{
32+
return new static(sprintf('Model `%s` default owner is null.', get_class($ownable)));
33+
}
34+
}

src/Exceptions/InvalidOwnerType.php renamed to contracts/Exceptions/InvalidOwnerType.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,28 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Cog\Ownership\Exceptions;
12+
namespace Cog\Contracts\Laravel\Ownership\Exceptions;
1313

14+
use Cog\Contracts\Laravel\Ownership\Ownable as OwnableContract;
1415
use Exception;
15-
use Cog\Ownership\Contracts\HasOwner;
16-
use Cog\Ownership\Contracts\CanBeOwner as CanBeOwnerContract;
16+
use Cog\Contracts\Laravel\Ownership\CanBeOwner as CanBeOwnerContract;
1717

1818
/**
1919
* Class InvalidOwnerType.
2020
*
21-
* @package Cog\Ownership\Exceptions
21+
* @package Cog\Contracts\Laravel\Ownership\Exceptions
2222
*/
2323
class InvalidOwnerType extends Exception
2424
{
2525
/**
2626
* Owner of the provided type is not allowed to own this model.
2727
*
28-
* @param \Cog\Ownership\Contracts\HasOwner $model
29-
* @param \Cog\Ownership\Contracts\CanBeOwner $owner
28+
* @param \Cog\Contracts\Laravel\Ownership\Ownable $ownable
29+
* @param \Cog\Contracts\Laravel\Ownership\CanBeOwner $owner
3030
* @return static
3131
*/
32-
public static function notAllowed(HasOwner $model, CanBeOwnerContract $owner)
32+
public static function notAllowed(OwnableContract $ownable, CanBeOwnerContract $owner)
3333
{
34-
return new static(sprintf('Model `%s` not allows owner of type `%s`.', get_class($model), get_class($owner)));
34+
return new static(sprintf('Model `%s` not allows owner of type `%s`.', get_class($ownable), get_class($owner)));
3535
}
3636
}

0 commit comments

Comments
 (0)