<commit_before># Hawkbit Persistence

[![Latest Version on Packagist][ico-version]][link-packagist]
[![Software License][ico-license]](LICENSE.md)
[![Build Status][ico-travis]][link-travis]
[![Total Downloads][ico-downloads]][link-downloads]
[![Coverage Status][ico-coveralls]][link-coveralls]

Persistence layer for Hawkbit PSR-7 Micro PHP framework.
Hawkbit Persitence uses factories of `dasprid/container-interop-doctrine` and wraps them with in a PersistenceService

## Install

### Using Composer

Hawkbit Persistence is available on [Packagist][link-packagist] and can be installed using [Composer](https://getcomposer.org/). This can be done by running the following command or by updating your `composer.json` file.

```bash
composer require hawkbit/persistence
```

composer.json

```javascript
{
    "require": {
        "hawkbit/persistence": "~1.0"
    }
}
```

Be sure to also include your Composer autoload file in your project:

```php
<?php

require __DIR__ . '/vendor/autoload.php';
```

### Downloading .zip file

This project is also available for download as a `.zip` file on GitHub. Visit the [releases page](https://github.com/hawkbit/persistence/releases), select the version you want, and click the "Source code (zip)" download button.

### Requirements

The following versions of PHP are supported by this version.

* PHP 5.5
* PHP 5.6
* PHP 7.0
* HHVM

## Setup

Setup with an existing application configuration

```php
<?php

use \Hawkbit\Application;
use \Hawkbit\Persistence\PersistenceService;
use \Hawkbit\Persistence\PersistenceServiceProvider;

$app = new Application(require_once __DIR__ . '/config.php');

$entityFactoryClass = \ContainerInteropDoctrine\EntityManagerFactory::class;

$persistenceService = new PersistenceService([
   PersistenceService::resolveFactoryAlias($entityFactoryClass) => [$entityFactoryClass]
], $app);

$app->register(new PersistenceServiceProvider($persistenceService));
```

Access persistence service in controller

```php
<?php

use \Hawkbit\Persistence\PersistenceServiceInterface;

class MyController{
    
    /**
     * @var \Hawkbit\Persistence\PersistenceServiceInterface 
     */
    private $persistence = null;
    
    public function __construct(PersistenceServiceInterface $persistence){
        $this->persistence = $persistence;
    }
    
    public function index(){
        $em = $this->persistence->getEntityManager();
        
        // or with from specific connection
        $em = $this->persistence->getEntityManager('connectionname');
    }
}
```

Or from application

```php
<?php

/** @var \Hawkbit\Persistence\PersistenceServiceInterface $persistence */
$persistence = $app[\Hawkbit\Persistence\PersistenceServiceInterface::class];

$em = $persistence->getEntityManager();

// or with from specific connection
$em = $persistence->getEntityManager('connectionname');

```<commit_msg>fix typo<commit_after># Hawkbit Persistence

[![Latest Version on Packagist][ico-version]][link-packagist]
[![Software License][ico-license]](LICENSE.md)
[![Build Status][ico-travis]][link-travis]
[![Total Downloads][ico-downloads]][link-downloads]
[![Coverage Status][ico-coveralls]][link-coveralls]

Persistence layer for Hawkbit PSR-7 Micro PHP framework.
Hawkbit Persitence uses factories of `dasprid/container-interop-doctrine` and wraps them with in a PersistenceService

## Install

### Using Composer

Hawkbit Persistence is available on [Packagist][link-packagist] and can be installed using [Composer](https://getcomposer.org/). This can be done by running the following command or by updating your `composer.json` file.

```bash
composer require hawkbit/persistence
```

composer.json

```javascript
{
    "require": {
        "hawkbit/persistence": "~1.0"
    }
}
```

Be sure to also include your Composer autoload file in your project:

```php
<?php

require __DIR__ . '/vendor/autoload.php';
```

### Downloading .zip file

This project is also available for download as a `.zip` file on GitHub. Visit the [releases page](https://github.com/hawkbit/persistence/releases), select the version you want, and click the "Source code (zip)" download button.

### Requirements

The following versions of PHP are supported by this version.

* PHP 5.5
* PHP 5.6
* PHP 7.0
* HHVM

## Setup

Setup with an existing application configuration (we refer to [tests/assets/config.php](tests/assets/config.php))

```php
<?php

use \Hawkbit\Application;
use \Hawkbit\Persistence\PersistenceService;
use \Hawkbit\Persistence\PersistenceServiceProvider;

$app = new Application(require_once __DIR__ . '/config.php');

$entityFactoryClass = \ContainerInteropDoctrine\EntityManagerFactory::class;

$persistenceService = new PersistenceService([
   PersistenceService::resolveFactoryAlias($entityFactoryClass) => [$entityFactoryClass]
], $app);

$app->register(new PersistenceServiceProvider($persistenceService));
```

## Examples

### Full configuration

A full configuration is available on [DASPRiD/container-interop-doctrine/example/full-config.php](https://github.com/DASPRiD/container-interop-doctrine/blob/master/example/full-config.php). 
Refer to [container-interop-doctrine Documentation](https://github.com/DASPRiD/container-interop-doctrine) for further instructions on factories.

### Persistence from Hawbit Application

```php
<?php

/** @var \Hawkbit\Persistence\PersistenceServiceInterface $persistence */
$persistence = $app[\Hawkbit\Persistence\PersistenceServiceInterface::class];

$em = $persistence->getEntityManager();

// or with from specific connection
$em = $persistence->getEntityManager('connectionname');

```

### Persistence in a Hawkbit controller

Access persistence service in controller. Hawbit is inject classes to controllers by default.

```php
<?php

use \Hawkbit\Persistence\PersistenceServiceInterface;

class MyController{
    
    /**
     * @var \Hawkbit\Persistence\PersistenceServiceInterface 
     */
    private $persistence = null;
    
    public function __construct(PersistenceServiceInterface $persistence){
        $this->persistence = $persistence;
    }
    
    public function index(){
        $em = $this->persistence->getEntityManager();
        
        // or with from specific connection
        $em = $this->persistence->getEntityManager('connectionname');
    }
}
```
{'fraction_non_alphanumeric': 0.12248135808345233, 'fraction_numerical': 0.0028557829604950024, 'mean_word_length': 4.820867959372115, 'pattern_counts': {'":': 4, '<': 11, '<?xml version=': 0, '>': 21, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4112917', 'n_tokens_mistral': 1874, 'n_tokens_neox': 1838, 'n_words': 490}