Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mpociot committed Oct 29, 2015
0 parents commit 4b105e8
Show file tree
Hide file tree
Showing 13 changed files with 960 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
composer.lock
/vendor/
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
language: php

php:
- 5.5
- 5.6
- 7.0

before_script:
- travis_retry composer self-update
- travis_retry composer install --prefer-source --no-interaction

script:
- vendor/bin/phpunit --coverage-clover=coverage.xml

before_install:
- pip install --user codecov
after_success:
- codecov
152 changes: 152 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# Versionable
## Easy to use Model versioning for Laravel 4 and Laravel 5

![image](http://img.shields.io/packagist/v/mpociot/versionable.svg?style=flat)
![image](http://img.shields.io/packagist/l/mpociot/versionable.svg?style=flat)
![image](http://img.shields.io/packagist/dt/mpociot/versionable.svg?style=flat)
[![codecov.io](https://codecov.io/github/mpociot/versionable/coverage.svg?branch=master)](https://codecov.io/github/mpociot/versionable?branch=master)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/mpociot/versionable/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/mpociot/versionable/?branch=master)
[![Build Status](https://travis-ci.org/mpociot/versionable.svg?branch=master)](https://travis-ci.org/mpociot/versionable)

Keep track of all your model changes and revert to previous versions of it.


```php
// Restore to the previous change
$content->previousVersion()->revert();

// Get model from a version
$oldModel = Version::find(100)->getModel();
```


## Contents

- [Installation](#installation)
- [Implementation](#implementation)
- [Usage](#usage)
- [Exclude attributes from versioning](#exclude)
- [Retrieving all versions associated to a model](#retrieve)
- [Getting a diff of two versions](#diff)
- [Revert to a previous version](#revert)
- [FAQ](#faq)
- [License](#license)

<a name="installation" />
## Installation

In order to add Versionable to your project, just add

"mpociot/versionable": "~2.0"

to your composer.json. Then run `composer install` or `composer update`.

Or run `composer require mpociot/versionable ` if you prefere that.

Run the migrations to create the "versions" table that will hold all version information.

```bash
php artisan migrate --path=vendor/mpociot/versionable/src/migrations
```

<a name="usage" />
## Usage

Let the Models you want to set under version control use the `VersionableTrait`.

```php
class Content extends Model {

use Mpociot\Versionable\VersionableTrait;

}
```
That's it!

Every time you update your model, a new version containing the previous attributes will be stores in your database.

All timestamps and the possible soft-delete timestamp will be ignored.

<a name="exclude" />
### Exclude attributes from versioning

Sometimes you don't want to create a version *every* time an attribute on your model changes. For example your User model might have a `last_login_at` attribute.
I'm pretty sure you don't want to create a new version of your User model every time that user logs in.

To exclude specific attributes from versioning, add a new array property to your model named `dontVersionFields`.

```php
class User extends Model {

use Mpociot\Versionable\VersionableTrait;

/**
* @var array
*/
protected $dontVersionFields = [ 'last_login_at' ];

}
```

<a name="retrieve" />
### Retrieving all versions associated to a model

To retrieve all stored versions use the `versions` attribute on your model.

This attribute can also be accessed like any other Laravel relation, since it is a `MorphMany` relation.

```php
$model->versions;
```

<a name="diff" />
### Getting a diff of two versions

If you want to know, what exactly has changed between two versions, use the version model's `diff` method.

The diff method takes a version model as an argument. This defines the version to diff against. If no version is provided, it will use the current version.

```php
/**
* Create a diff against the current version
*/
$diff = $page->previousVersion()->diff();


/**
* Create a diff against a specific version
*/
$diff = $page->currentVersion()->diff( $version );
```

The result will be an associative array containing the attribute name as the key, and the different attribute value.

<a name="revert" />
### Revert to a previous version

Saving versions is pretty cool, but the real benefit will be the ability to revert to a specific version.

There are multiple ways to do this.

**Revert to the previous version**

You can easiliy revert to the version prior to the currently active version using:

```php
$content->previousVersion()->revert();
```

**Revert to a specific version ID**

You can also revert to a specific version ID of a model using:

```php
$revertedModel = Version::find( $version_id )->revert();
```



<a name="license" />
## License

Versionable is free software distributed under the terms of the MIT license.
37 changes: 37 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "mpociot/captainhook",
"license": "MIT",
"description": "Add webhooks to your Laravel app.",
"keywords": ["webhooks", "laravel", "hook", "events"],
"homepage": "http://github.com/mpociot/captainhook",
"authors": [
{
"name": "Marcel Pociot",
"email": "[email protected]"
}
],
"support": {
"issues": "https://github.com/mpociot/captainhook/issues",
"source": "https://github.com/mpociot/captainhook"
},
"require": {
"php": ">=5.3.0",
"illuminate/support": "~5.0"
},
"require-dev": {
"phpunit/phpunit": "4.7.*",
"mockery/mockery": "dev-master",
"illuminate/database": "~5.0",
"illuminate/events": "~5.0",
"orchestra/testbench": "~3.0",
"guzzlehttp/guzzle": ">=4.0"
},
"autoload": {
"psr-0": {
"Mpociot\\CaptainHook": "src/"
}
},
"scripts": {
"test": "phpunit"
}
}
22 changes: 22 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="true"
syntaxCheck="false">
<testsuites>
<testsuite name="Versionable Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/Mpociot/</directory>
</whitelist>
</filter>
</phpunit>
Loading

0 comments on commit 4b105e8

Please sign in to comment.