Skip to content

Commit 717b68b

Browse files
committed
Merge branch 'master' of github.com:etrepat/baum
Conflicts: README.md src/Baum/Console/InstallCommand.php src/Baum/Providers/BaumServiceProvider.php
2 parents ec13e35 + 9290bed commit 717b68b

14 files changed

+81
-96
lines changed

CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
## 1.1.1
2+
_Mar 4 2015_
3+
* bug fixes
4+
- Fix README.md typos [PR #134 and #131]
5+
- Use table prefixes in `rebuild` command [PR #133]
6+
- Fix `limitDepth` query scope [PR #125]
7+
8+
## 1.1.0
9+
_Mar 3 2015_
10+
* enhancements
11+
- Add support for Laravel 5.x, leave a [1.0.x](https://github.com/etrepat/baum/tree/1.0.x-stable) branch for backwards compatibility.
12+
113
## 1.0.13
214
_Sep 30 2014_
315
* bug fixes

README.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
Baum is an implementation of the [Nested Set](http://en.wikipedia.org/wiki/Nested_set_model)
44
pattern for [Laravel 5's](http://laravel.com/) Eloquent ORM.
55

6+
For **Laravel 4.2.x compatibility**, check the [1.0.x branch](https://github.com/etrepat/baum/tree/1.0.x-stable) branch or use the latest [1.0.x tagged release](https://github.com/etrepat/baum/releases).
7+
68
## Documentation
79

810
* [About Nested Sets](#about)
@@ -91,18 +93,18 @@ ordinary trees are suddenly quite fast. Nifty, isn't it?
9193
<a name="installation"></a>
9294
## Installation
9395

94-
Baum works with Laravel 4 onwards. You can add it to your `composer.json` file
96+
Baum works with Laravel 5 onwards. You can add it to your `composer.json` file
9597
with:
9698

97-
"baum/baum": "~1.0"
99+
"baum/baum": "~1.1"
98100

99101
Run `composer install` to install it.
100102

101-
As with most Laravel 4 packages you'll then need to register the Baum
102-
*service provider*. To do that, head over your `app/config/app.php` file and add
103+
As with most Laravel 5 packages you'll then need to register the Baum
104+
*service provider*. To do that, head over your `config/app.php` file and add
103105
the following line into the `providers` array:
104106

105-
'Baum\BaumServiceProvider',
107+
'Baum\Providers\BaumServiceProvider',
106108

107109
<a name="getting-started"></a>
108110
## Getting started
@@ -329,7 +331,7 @@ You can ask some questions to your Baum nodes:
329331
* `isAncestorOf($other)`: Returns true if node is an ancestor of the other.
330332
* `isSelfOrAncestorOf($other)`: Returns true if node is self or an ancestor.
331333
* `equals($node)`: current node instance equals the other.
332-
* `insideSubtree($node)`: Checks wether the given node is inside the subtree
334+
* `insideSubtree($node)`: Checks whether the given node is inside the subtree
333335
defined by the left and right indices.
334336
* `inSameScope($node)`: Returns true if the given node is in the same scope
335337
as the current one. That is, if *every* column in the `scoped` property has
@@ -472,7 +474,7 @@ Retrieving a complete tree hierarchy into a regular `Collection` object with
472474
its children *properly nested* is as simple as:
473475

474476
```php
475-
$tree = Category::where('name', '=', Books)->first()->getDescendantsAndSelf()->toHierarchy();
477+
$tree = Category::where('name', '=', 'Books')->first()->getDescendantsAndSelf()->toHierarchy();
476478
```
477479

478480
<a name="node-model-events"></a>

bootstrap.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
$capsule->addConnection(require(__DIR__.'/tests/config/database.php'));
1414

15-
$capsule->setEventDispatcher(new Illuminate\Events\Dispatcher);
15+
$capsule->setEventDispatcher(new Illuminate\Events\Dispatcher(new Illuminate\Container\Container));
1616

1717
$capsule->bootEloquent();
1818

composer.json

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "unikent/baum",
33
"type": "library",
44
"description": "Baum is an implementation of the Nested Set pattern for Eloquent models.",
5-
"keywords": ["nested set", "laravel", "laravel 4", "eloquent", "database"],
5+
"keywords": ["nested set", "laravel", "laravel 4", "laravel 5", "eloquent", "database"],
66
"license": "MIT",
77
"authors": [
88
{
@@ -12,16 +12,17 @@
1212
}
1313
],
1414
"require": {
15-
"php": ">=5.3.0",
16-
"illuminate/support": "4.*|5.*",
17-
"illuminate/console": "4.*|5.*",
18-
"illuminate/filesystem": "4.*|5.*",
19-
"illuminate/database": "4.*|5.*"
15+
"php": ">=5.4.0",
16+
"illuminate/console": "5.*",
17+
"illuminate/database": "5.*",
18+
"illuminate/events": "5.*",
19+
"illuminate/filesystem": "5.*",
20+
"illuminate/support": "5.*"
2021
},
2122
"require-dev": {
2223
"phpunit/phpunit": "~4.0",
2324
"mockery/mockery": "~0.9",
24-
"d11wtq/boris": "~1.0"
25+
"d11wtq/boris": "~1.0.10"
2526
},
2627
"autoload": {
2728
"psr-0": {

src/Baum/Console/InstallCommand.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ protected function writeModel($name) {
108108
* @return string
109109
*/
110110
protected function getMigrationsPath() {
111-
return $this->laravel['path.base'].'/database/migrations';
111+
return $this->laravel['path.database'].'/migrations';
112112
}
113113

114114
/**
@@ -117,7 +117,8 @@ protected function getMigrationsPath() {
117117
* @return string
118118
*/
119119
protected function getModelsPath() {
120-
return $this->laravel['path'];
120+
121+
return $this->laravel['path.base'];
121122
}
122123

123124
}

src/Baum/Extensions/Eloquent/Model.php

+14-17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
2-
32
namespace Baum\Extensions\Eloquent;
43

54
use Illuminate\Database\Eloquent\Model as BaseModel;
65
use Illuminate\Database\Eloquent\ModelNotFoundException;
6+
use Illuminate\Database\Eloquent\SoftDeletingScope;
77
use Baum\Extensions\Query\Builder as QueryBuilder;
88

99
abstract class Model extends BaseModel {
@@ -50,8 +50,8 @@ public function getObservableEvents() {
5050
* @param Closure|string $callback
5151
* @return void
5252
*/
53-
public static function moving($callback) {
54-
static::registerModelEvent('moving', $callback);
53+
public static function moving($callback, $priority = 0) {
54+
static::registerModelEvent('moving', $callback, $priority);
5555
}
5656

5757
/**
@@ -60,8 +60,8 @@ public static function moving($callback) {
6060
* @param Closure|string $callback
6161
* @return void
6262
*/
63-
public static function moved($callback) {
64-
static::registerModelEvent('moved', $callback);
63+
public static function moved($callback, $priority = 0) {
64+
static::registerModelEvent('moved', $callback, $priority);
6565
}
6666

6767
/**
@@ -95,18 +95,15 @@ protected function getFreshInstance() {
9595
* @return boolean
9696
*/
9797
public function areSoftDeletesEnabled() {
98-
// Soft-delete functionality in 4.2 has been moved into a trait.
99-
// The proper way to check if a model includes a global scope in >= 4.2
100-
// should look similar to the following:
101-
//
102-
// static::hasGlobalScope(new SoftDeletingScope);
103-
//
104-
// We are doing it this way (not the best probably) to keep backwards
105-
// compatibility...
106-
return (
107-
(property_exists($this, 'softDelete') && $this->softDelete == true) ||
108-
(!property_exists($this, 'softDelete') && method_exists($this, 'getDeletedAtColumn'))
109-
);
98+
// To determine if there's a global soft delete scope defined we must
99+
// first determine if there are any, to workaround a non-existent key error.
100+
$globalScopes = $this->getGlobalScopes();
101+
102+
if ( count($globalScopes) === 0 ) return false;
103+
104+
// Now that we're sure that the calling class has some kind of global scope
105+
// we check for the SoftDeletingScope existance
106+
return static::hasGlobalScope(new SoftDeletingScope);
110107
}
111108

112109
/**

src/Baum/Generators/stubs/model.php

+3-26
Original file line numberDiff line numberDiff line change
@@ -95,31 +95,8 @@ class {{class}} extends Node {
9595
// In the same way as Eloquent's model events, returning false from the
9696
// `moving` event handler will halt the operation.
9797
//
98-
// Below is a sample `boot` method just for convenience, as an example of how
99-
// one should hook into those events. This is the *recommended* way to hook
100-
// into model events, as stated in the documentation. Please refer to the
101-
// Laravel documentation for details.
102-
//
103-
// If you don't plan on using model events in your program you can safely
104-
// remove all the commented code below.
105-
//
106-
107-
// /**
108-
// * The "booting" method of the model.
109-
// *
110-
// * @return void
111-
// */
112-
// protected static function boot() {
113-
// // Do not forget this!
114-
// parent::boot();
115-
116-
// static::moving(function($node) {
117-
// // YOUR CODE HERE
118-
// });
119-
120-
// static::moved(function($node) {
121-
// // YOUR CODE HERE
122-
// });
123-
// }
98+
// Please refer the Laravel documentation for further instructions on how
99+
// to hook your own callbacks/observers into this events:
100+
// http://laravel.com/docs/5.0/eloquent#model-events
124101

125102
}

src/Baum/Node.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,9 @@ public function scopeWithoutRoot($query) {
500500
public function scopeLimitDepth($query, $limit) {
501501
$depth = $this->exists ? $this->getDepth() : $this->getLevel();
502502
$max = $depth + $limit;
503+
$scopes = array($depth, $max);
503504

504-
return $query->whereBetween($this->getDepthColumnName(), array($depth, $max));
505+
return $query->whereBetween($this->getDepthColumnName(), array(min($scopes), max($scopes)));
505506
}
506507

507508
/**

src/Baum/BaumServiceProvider.php src/Baum/Providers/BaumServiceProvider.php

+14-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
namespace Baum;
2+
namespace Baum\Providers;
33

44
use Baum\Generators\MigrationGenerator;
55
use Baum\Generators\ModelGenerator;
@@ -14,14 +14,7 @@ class BaumServiceProvider extends ServiceProvider {
1414
*
1515
* @var string
1616
*/
17-
const VERSION = '1.0.13';
18-
19-
/**
20-
* Indicates if loading of the provider is deferred.
21-
*
22-
* @var bool
23-
*/
24-
protected $defer = false;
17+
const VERSION = '1.1.1';
2518

2619
/**
2720
* Register the service provider.
@@ -32,15 +25,6 @@ public function register() {
3225
$this->registerCommands();
3326
}
3427

35-
/**
36-
* Get the services provided by the provider.
37-
*
38-
* @return array
39-
*/
40-
public function provides() {
41-
return array('node');
42-
}
43-
4428
/**
4529
* Register the commands.
4630
*
@@ -61,8 +45,8 @@ public function registerCommands() {
6145
* @return void
6246
*/
6347
protected function registerBaumCommand() {
64-
$this->app['command.baum'] = $this->app->share(function($app) {
65-
return new BaumCommand();
48+
$this->app->singleton('command.baum', function($app) {
49+
return new BaumCommand;
6650
});
6751
}
6852

@@ -72,12 +56,21 @@ protected function registerBaumCommand() {
7256
* @return void
7357
*/
7458
protected function registerInstallCommand() {
75-
$this->app['command.baum.install'] = $this->app->share(function($app) {
59+
$this->app->singleton('command.baum.install', function($app) {
7660
$migrator = new MigrationGenerator($app['files']);
7761
$modeler = new ModelGenerator($app['files']);
7862

7963
return new InstallCommand($migrator, $modeler);
8064
});
8165
}
8266

67+
/**
68+
* Get the services provided by the provider.
69+
*
70+
* @return array
71+
*/
72+
public function provides() {
73+
return array('command.baum', 'command.baum.install');
74+
}
75+
8376
}

src/Baum/SetValidator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected function validateBounds() {
7171
$qualifiedRgtCol >= parent.$rgtCol)))";
7272

7373
$query = $this->node->newQuery()
74-
->join($connection->raw($grammar->wrap($tableName).' AS parent'),
74+
->join($connection->raw($grammar->wrapTable($tableName).' AS parent'),
7575
$parentColumn, '=', $connection->raw('parent.'.$grammar->wrap($primaryKeyName)),
7676
'left outer')
7777
->whereRaw($whereStm);

tests/models/Category.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use Illuminate\Database\Eloquent\SoftDeletingTrait;
3+
use Illuminate\Database\Eloquent\SoftDeletes;
44
use Baum\Node;
55

66
class Category extends Node {
@@ -39,7 +39,7 @@ class OrderedScopedCategory extends Category {
3939

4040
class SoftCategory extends Category {
4141

42-
use SoftDeletingTrait;
42+
use SoftDeletes;
4343

4444
public $timestamps = true;
4545

tests/models/Cluster.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use Illuminate\Database\Eloquent\SoftDeletingTrait;
3+
use Illuminate\Database\Eloquent\SoftDeletes;
44
use Baum\Node;
55

66
class Cluster extends Node {
@@ -58,7 +58,7 @@ class OrderedCluster extends Cluster {
5858

5959
class SoftCluster extends Cluster {
6060

61-
use SoftDeletingTrait;
61+
use SoftDeletes;
6262

6363
public $timestamps = true;
6464

tests/suite/NodeModelExtensionsTest.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,29 @@ public function testSoftDeletesEnabledStatic() {
5050

5151
public function testMoving() {
5252
$dispatcher = Category::getEventDispatcher();
53-
Category::setEventDispatcher($events = m::mock('Illuminate\Events\Dispatcher'));
5453

55-
$closure = function() {};
56-
$events->shouldReceive('listen')->once()->with('eloquent.moving: '.get_class(new Category), $closure);
54+
Category::setEventDispatcher($events = m::mock('Illuminate\Contracts\Events\Dispatcher'));
5755

56+
$closure = function() {};
57+
$events->shouldReceive('listen')->once()->with('eloquent.moving: '.get_class(new Category), $closure, 0);
5858
Category::moving($closure);
5959

6060
Category::unsetEventDispatcher();
61+
6162
Category::setEventDispatcher($dispatcher);
6263
}
6364

6465
public function testMoved() {
6566
$dispatcher = Category::getEventDispatcher();
66-
Category::setEventDispatcher($events = m::mock('Illuminate\Events\Dispatcher'));
6767

68-
$closure = function() {};
69-
$events->shouldReceive('listen')->once()->with('eloquent.moved: '.get_class(new Category), $closure);
68+
Category::setEventDispatcher($events = m::mock('Illuminate\Contracts\Events\Dispatcher'));
7069

70+
$closure = function() {};
71+
$events->shouldReceive('listen')->once()->with('eloquent.moved: '.get_class(new Category), $closure, 0);
7172
Category::moved($closure);
7273

7374
Category::unsetEventDispatcher();
75+
7476
Category::setEventDispatcher($dispatcher);
7577
}
7678

0 commit comments

Comments
 (0)