<commit_before># Silex Capsule Service Provider

This is a service provider for the [Silex Micro Framework](http://silex.sensiolabs.org/) that integrates [Laravel's Eloquent ORM](http://laravel.com/docs/5.0/eloquent) via [Capsule](https://github.com/illuminate/database), its standalone wrapper implementation.

## Requirements

In order to use the service provider you'll need to be running **PHP 5.4+**

## Installation

The best way to install the service provider is using [Composer](https://getcomposer.org):

````shell
composer require ziadoz/silex-capsule:1.0
````

Alternatively, you can add it directly to your `composer.json` file: 

````json
{
    "require": {
        "ziadoz/silex-capsule": "1.0"
    }
}
````

## Basic Usage

To use it in your application just register the service provider with Silex: 

````php
<?php        
$app = new Silex\Application;

$app->register(new Ziadoz\Silex\Provider\CapsuleServiceProvider, array(
    'capsule.connection' => [
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'database',
        'username'  => 'username',
        'password'  => 'password',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'logging'   => true,
    ],
));

    ````

For more information about the available options you should refer to the [Capsule documentation](https://github.com/illuminate/database).

Once you've registered the service provider a Capsule object will be created and Eloquent booted up before any of your route controllers are called. If you're interested in the technical aspects, Capsule is registered as `before` middleware with Silex using `Application::EARLY_EVENT`, so it'll only ever be made available once your application is run and before anything else happens.

If you need Capsule and Eloquent to be booted up before your application is run (`$app->run()`), for example in a Symfony Console command, you just need to access its array element within the dependency injection container: 

````php
$app['capsule']; 
````

Capsule will be made available globally by default, allowing you to write queries directly in your controllers should you wish:

````php
use Illuminate\Database\Capsule\Manager as Capsule;

$app->get('/book/{id}', function(Application $app, $id) {
    $book = Capsule::table('books')->where('id', $id)->get();
    return $app->json($book);
});
````

If you don't want Capsule to be booted then set the `capsule.global` setting to `false`. If you don't plan to use Eloquent to build models then you can also prevent it from being booted by setting `capsule.eloquent` to `false`.

Creating Eloquent models is identical to how you would create them in Laravel: 

````php
use Illuminate\Database\Eloquent\Model;

class Book extends Model
{
    protected $table = 'books';

    protected $fillable = [
        'title', 
        'author',
    ];

    protected $casts = [
        'title'  => 'string',
        'author' => 'string',
    ];

    // The rest of your model code...
}
````

You can then use it, and all of its features, in your controllers just like you would in Laravel: 

````php
$app->get('/books', function(Application $app) {
    $books = Book::with('tags')->all();
    return $app->json($books);
});

$app->post('/book', function(Application $app, Request $request) {
    $book         = new Book();
    $book->title  = $request->request->get('title');
    $book->author = $request->request->get('author');
    $book->save();
});
````

## Advanced Usage

You can setup multiple connections and even caching with the service provider; simply use the `capsule.connections` and `capsule.cache` options:

````php
<?php
$app = new Silex\Application;

$app->register(new Ziadoz\Silex\Provider\CapsuleServiceProvider, array(
    // Connections
    'capsule.connections' => [
        'default' => [
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'dname1',
            'username'  => 'root',
            'password'  => '',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'logging'   => false,
        ],

        'other' => [
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'dbname2',
            'username'  => 'root',
            'password'  => '',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'logging'   => true,
        ],
    ],

    // Cache
    'capsule.cache' => [
        'driver' => 'apc',
        'prefix' => 'laravel',
    ],
));
````

If you want to use caching you'll need to install the Laravel Events package, which you can do using Composer: 

````
composer require illuminte/events:5.*
````

Alternatively, you can add it directly to your `composer.json` file: <commit_msg>Use PHP 5.4 style arrays in readme.<commit_after># Silex Capsule Service Provider

This is a service provider for the [Silex Micro Framework](http://silex.sensiolabs.org/) that integrates [Laravel's Eloquent ORM](http://laravel.com/docs/5.0/eloquent) via [Capsule](https://github.com/illuminate/database), its standalone wrapper implementation.

## Requirements

In order to use the service provider you'll need to be running **PHP 5.4+**

## Installation

The best way to install the service provider is using [Composer](https://getcomposer.org):

````shell
composer require ziadoz/silex-capsule:1.0
````

Alternatively, you can add it directly to your `composer.json` file: 

````json
{
    "require": {
        "ziadoz/silex-capsule": "1.0"
    }
}
````

## Basic Usage

To use it in your application just register the service provider with Silex: 

````php
<?php        
$app = new Silex\Application;

$app->register(new Ziadoz\Silex\Provider\CapsuleServiceProvider, [
    'capsule.connection' => [
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'database',
        'username'  => 'username',
        'password'  => 'password',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'logging'   => true,
    ],
]);

    ````

For more information about the available options you should refer to the [Capsule documentation](https://github.com/illuminate/database).

Once you've registered the service provider a Capsule object will be created and Eloquent booted up before any of your route controllers are called. If you're interested in the technical aspects, Capsule is registered as `before` middleware with Silex using `Application::EARLY_EVENT`, so it'll only ever be made available once your application is run and before anything else happens.

If you need Capsule and Eloquent to be booted up before your application is run (`$app->run()`), for example in a Symfony Console command, you just need to access its array element within the dependency injection container: 

````php
$app['capsule']; 
````

Capsule will be made available globally by default, allowing you to write queries directly in your controllers should you wish:

````php
use Illuminate\Database\Capsule\Manager as Capsule;

$app->get('/book/{id}', function(Application $app, $id) {
    $book = Capsule::table('books')->where('id', $id)->get();
    return $app->json($book);
});
````

If you don't want Capsule to be booted then set the `capsule.global` setting to `false`. If you don't plan to use Eloquent to build models then you can also prevent it from being booted by setting `capsule.eloquent` to `false`.

Creating Eloquent models is identical to how you would create them in Laravel: 

````php
use Illuminate\Database\Eloquent\Model;

class Book extends Model
{
    protected $table = 'books';

    protected $fillable = [
        'title', 
        'author',
    ];

    protected $casts = [
        'title'  => 'string',
        'author' => 'string',
    ];

    // The rest of your model code...
}
````

You can then use it, and all of its features, in your controllers just like you would in Laravel: 

````php
$app->get('/books', function(Application $app) {
    $books = Book::with('tags')->all();
    return $app->json($books);
});

$app->post('/book', function(Application $app, Request $request) {
    $book         = new Book();
    $book->title  = $request->request->get('title');
    $book->author = $request->request->get('author');
    $book->save();
});
````

## Advanced Usage

You can setup multiple connections and even caching with the service provider; simply use the `capsule.connections` and `capsule.cache` options:

````php
<?php
$app = new Silex\Application;

$app->register(new Ziadoz\Silex\Provider\CapsuleServiceProvider, [
    // Connections
    'capsule.connections' => [
        'default' => [
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'dname1',
            'username'  => 'root',
            'password'  => '',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'logging'   => false,
        ],

        'other' => [
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'dbname2',
            'username'  => 'root',
            'password'  => '',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'logging'   => true,
        ],
    ],

    // Cache
    'capsule.cache' => [
        'driver' => 'apc',
        'prefix' => 'laravel',
    ],
]);
````

If you want to use caching you'll need to install the Laravel Events package, which you can do using Composer: 

````
composer require illuminte/events:5.*
````

Alternatively, you can add it directly to your `composer.json` file: 
{'fraction_non_alphanumeric': 0.14285714285714285, 'fraction_numerical': 0.003637098403717923, 'mean_word_length': 2.904930966469428, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 111, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2575334', 'n_tokens_mistral': 3032, 'n_tokens_neox': 2898, 'n_words': 960}