Skip to content

Commit

Permalink
Add support for Laravel 5
Browse files Browse the repository at this point in the history
  • Loading branch information
etrepat committed Mar 4, 2015
1 parent b868b70 commit 54e9abe
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 104 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.1.0
_Mar 3 2015_
* enhancements
- 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.

## 1.0.13
_Sep 30 2014_
* bug fixes
Expand Down
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

[![Build Status](https://travis-ci.org/etrepat/baum.png?branch=master)](https://travis-ci.org/etrepat/baum)

Baum is an implementation of the [Nested Set](http://en.wikipedia.org/wiki/Nested_set_model)
pattern for [Laravel 4's](http://laravel.com/) Eloquent ORM.
Baum is an implementation of the [Nested Set](http://en.wikipedia.org/wiki/Nested_set_model) pattern for [Laravel 5's](http://laravel.com/) Eloquent ORM.

> 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).
## Documentation

Expand Down Expand Up @@ -93,18 +94,18 @@ ordinary trees are suddenly quite fast. Nifty, isn't it?
<a name="installation"></a>
## Installation

Baum works with Laravel 4 onwards. You can add it to your `composer.json` file
Baum works with Laravel 5 onwards. You can add it to your `composer.json` file
with:

"baum/baum": "~1.0"
"baum/baum": "~1.1"

Run `composer install` to install it.

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

'Baum\BaumServiceProvider',
'Baum\Providers\BaumServiceProvider',

<a name="getting-started"></a>
## Getting started
Expand Down
2 changes: 1 addition & 1 deletion bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

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

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

$capsule->bootEloquent();

Expand Down
15 changes: 8 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "baum/baum",
"type": "library",
"description": "Baum is an implementation of the Nested Set pattern for Eloquent models.",
"keywords": ["nested set", "laravel", "laravel 4", "eloquent", "database"],
"keywords": ["nested set", "laravel", "laravel 4", "laravel 5", "eloquent", "database"],
"license": "MIT",
"authors": [
{
Expand All @@ -12,16 +12,17 @@
}
],
"require": {
"php": ">=5.3.0",
"illuminate/support": "4.*|5.*",
"illuminate/console": "4.*|5.*",
"illuminate/filesystem": "4.*|5.*",
"illuminate/database": "4.*|5.*"
"php": ">=5.4.0",
"illuminate/console": "5.*",
"illuminate/database": "5.*",
"illuminate/events": "5.*",
"illuminate/filesystem": "5.*",
"illuminate/support": "5.*"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
"mockery/mockery": "~0.9",
"d11wtq/boris": "~1.0"
"d11wtq/boris": "~1.0.10"
},
"autoload": {
"psr-0": {
Expand Down
5 changes: 2 additions & 3 deletions src/Baum/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public function fire() {

$this->writeModel($name);

$this->call('dump-autoload');
}

/**
Expand Down Expand Up @@ -109,7 +108,7 @@ protected function writeModel($name) {
* @return string
*/
protected function getMigrationsPath() {
return $this->laravel['path'].'/database/migrations';
return $this->laravel['path.database'].'/migrations';
}

/**
Expand All @@ -118,7 +117,7 @@ protected function getMigrationsPath() {
* @return string
*/
protected function getModelsPath() {
return $this->laravel['path'].'/models';
return $this->laravel['path.base'];
}

}
31 changes: 14 additions & 17 deletions src/Baum/Extensions/Eloquent/Model.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Baum\Extensions\Eloquent;

use Illuminate\Database\Eloquent\Model as BaseModel;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Baum\Extensions\Query\Builder as QueryBuilder;

abstract class Model extends BaseModel {
Expand Down Expand Up @@ -50,8 +50,8 @@ public function getObservableEvents() {
* @param Closure|string $callback
* @return void
*/
public static function moving($callback) {
static::registerModelEvent('moving', $callback);
public static function moving($callback, $priority = 0) {
static::registerModelEvent('moving', $callback, $priority);
}

/**
Expand All @@ -60,8 +60,8 @@ public static function moving($callback) {
* @param Closure|string $callback
* @return void
*/
public static function moved($callback) {
static::registerModelEvent('moved', $callback);
public static function moved($callback, $priority = 0) {
static::registerModelEvent('moved', $callback, $priority);
}

/**
Expand Down Expand Up @@ -95,18 +95,15 @@ protected function getFreshInstance() {
* @return boolean
*/
public function areSoftDeletesEnabled() {
// Soft-delete functionality in 4.2 has been moved into a trait.
// The proper way to check if a model includes a global scope in >= 4.2
// should look similar to the following:
//
// static::hasGlobalScope(new SoftDeletingScope);
//
// We are doing it this way (not the best probably) to keep backwards
// compatibility...
return (
(property_exists($this, 'softDelete') && $this->softDelete == true) ||
(!property_exists($this, 'softDelete') && method_exists($this, 'getDeletedAtColumn'))
);
// To determine if there's a global soft delete scope defined we must
// first determine if there are any, to workaround a non-existent key error.
$globalScopes = $this->getGlobalScopes();

if ( count($globalScopes) === 0 ) return false;

// Now that we're sure that the calling class has some kind of global scope
// we check for the SoftDeletingScope existance
return static::hasGlobalScope(new SoftDeletingScope);
}

/**
Expand Down
29 changes: 3 additions & 26 deletions src/Baum/Generators/stubs/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,31 +95,8 @@ class {{class}} extends Node {
// In the same way as Eloquent's model events, returning false from the
// `moving` event handler will halt the operation.
//
// Below is a sample `boot` method just for convenience, as an example of how
// one should hook into those events. This is the *recommended* way to hook
// into model events, as stated in the documentation. Please refer to the
// Laravel documentation for details.
//
// If you don't plan on using model events in your program you can safely
// remove all the commented code below.
//

// /**
// * The "booting" method of the model.
// *
// * @return void
// */
// protected static function boot() {
// // Do not forget this!
// parent::boot();

// static::moving(function($node) {
// // YOUR CODE HERE
// });

// static::moved(function($node) {
// // YOUR CODE HERE
// });
// }
// Please refer the Laravel documentation for further instructions on how
// to hook your own callbacks/observers into this events:
// http://laravel.com/docs/5.0/eloquent#model-events

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Baum;
namespace Baum\Providers;

use Baum\Generators\MigrationGenerator;
use Baum\Generators\ModelGenerator;
Expand All @@ -14,23 +14,7 @@ class BaumServiceProvider extends ServiceProvider {
*
* @var string
*/
const VERSION = '1.0.13';

/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;

/**
* Bootstrap the application events.
*
* @return void
*/
public function boot() {
$this->package('baum/baum');
}
const VERSION = '1.1.0';

/**
* Register the service provider.
Expand All @@ -41,15 +25,6 @@ public function register() {
$this->registerCommands();
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides() {
return array('node');
}

/**
* Register the commands.
*
Expand All @@ -70,8 +45,8 @@ public function registerCommands() {
* @return void
*/
protected function registerBaumCommand() {
$this->app['command.baum'] = $this->app->share(function($app) {
return new BaumCommand();
$this->app->singleton('command.baum', function($app) {
return new BaumCommand;
});
}

Expand All @@ -81,12 +56,21 @@ protected function registerBaumCommand() {
* @return void
*/
protected function registerInstallCommand() {
$this->app['command.baum.install'] = $this->app->share(function($app) {
$this->app->singleton('command.baum.install', function($app) {
$migrator = new MigrationGenerator($app['files']);
$modeler = new ModelGenerator($app['files']);

return new InstallCommand($migrator, $modeler);
});
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides() {
return array('command.baum', 'command.baum.install');
}

}
4 changes: 2 additions & 2 deletions tests/models/Category.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use Illuminate\Database\Eloquent\SoftDeletingTrait;
use Illuminate\Database\Eloquent\SoftDeletes;
use Baum\Node;

class Category extends Node {
Expand Down Expand Up @@ -39,7 +39,7 @@ class OrderedScopedCategory extends Category {

class SoftCategory extends Category {

use SoftDeletingTrait;
use SoftDeletes;

public $timestamps = true;

Expand Down
4 changes: 2 additions & 2 deletions tests/models/Cluster.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use Illuminate\Database\Eloquent\SoftDeletingTrait;
use Illuminate\Database\Eloquent\SoftDeletes;
use Baum\Node;

class Cluster extends Node {
Expand Down Expand Up @@ -58,7 +58,7 @@ class OrderedCluster extends Cluster {

class SoftCluster extends Cluster {

use SoftDeletingTrait;
use SoftDeletes;

public $timestamps = true;

Expand Down
14 changes: 8 additions & 6 deletions tests/suite/NodeModelExtensionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,29 @@ public function testSoftDeletesEnabledStatic() {

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

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

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

Category::unsetEventDispatcher();

Category::setEventDispatcher($dispatcher);
}

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

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

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

Category::unsetEventDispatcher();

Category::setEventDispatcher($dispatcher);
}

Expand Down
5 changes: 2 additions & 3 deletions tests/suite/QueryBuilderExtensionTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

use Mockery as m;
use Illuminate\Database\Query\Grammars\Grammar;
use Baum\Extensions\Query\Builder as QueryBuilder;

class QueryBuilderExtensionTest extends PHPUnit_Framework_TestCase {
Expand All @@ -11,7 +10,7 @@ public function tearDown() {
}

protected function getBuilder() {
$grammar = new Grammar;
$grammar = new Illuminate\Database\Query\Grammars\Grammar;
$processor = m::mock('Illuminate\Database\Query\Processors\Processor');

return new QueryBuilder(m::mock('Illuminate\Database\ConnectionInterface'), $grammar, $processor);
Expand All @@ -32,7 +31,7 @@ public function testAggregatesRemoveOrderBy() {
$this->assertEquals(1, $results);

$builder = $this->getBuilder();
$builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from "users"', array())->andReturn(array(array('aggregate' => 1)));
$builder->getConnection()->shouldReceive('select')->once()->with('select count(*) as aggregate from "users" limit 1', array())->andReturn(array(array('aggregate' => 1)));
$builder->getProcessor()->shouldReceive('processSelect')->once()->andReturnUsing(function($builder, $results) { return $results; });
$results = $builder->from('users')->orderBy('age', 'desc')->exists();
$this->assertTrue($results);
Expand Down

0 comments on commit 54e9abe

Please sign in to comment.