Skip to content

Commit

Permalink
Merge pull request #3 from likesistemas/hotfix/fix-use-container
Browse files Browse the repository at this point in the history
✨ Adding container resolution using `illuminate\container`
  • Loading branch information
ricardoapaes authored Mar 25, 2022
2 parents fc75fe6 + e4bc4df commit b9082da
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# [Eloquent] Using Eloquent without any dependency on Laravel Framework [![CI](https://github.com/likesistemas/eloquent-external/actions/workflows/ci.yml/badge.svg)](https://github.com/likesistemas/eloquent-external/actions/workflows/ci.yml)

## Installation

```
composer require likesistemas/eloquent-external --dev
```

## How to use

### Using class `ConfigBean`

```php

use Illuminate\Container\Container;
use Like\Database\Config;
use Like\Database\ConfigBean;
use Like\Database\Eloquent;

$config = new ConfigBean(
'host',
'user',
'password',
'db_name'
);
$config->setFactoryFolder(__DIR__ . "/./factories/"); # Folder where all the Eloquent factories are.
$config->addFakerProvider(ProdutoProvider::class); # Optional. Use to add new providers to Faker. Note: you can add as many as you like.

# If you are configuring the settings in the same file that will start, you can pass the config by parameter.
Eloquent::init($config);

# Or set using `illuminate\container` and run init without parameter.
Container::getInstance()->instance(Config::class, $config);
# Then call `init` wherever you think is best, without having to pass parameters.
Eloquent::init();

```
4 changes: 2 additions & 2 deletions src/Eloquent.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public static function init(Config $config=null) {
return;
}

if ($config === null && Container::getInstance()->has(Config::class)) {
if ($config === null && Container::getInstance()->bound(Config::class)) {
$config = Container::getInstance()
->get(Config::class);
->make(Config::class);
}

self::initEloquent($config);
Expand Down
8 changes: 7 additions & 1 deletion tests/EloquentContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

namespace Like\Database\Tests;

use Illuminate\Container\Container;
use Like\Database\Config;
use Like\Database\Eloquent;

class EloquentContainerTest extends EloquentTest {
public static function set_up_before_class() {
Eloquent::destroy();
self::assertFalse(Eloquent::isLoaded());

Eloquent::init(new ConfigBean());
$config = new ConfigBean();
Container::getInstance()->instance(Config::class, $config);
self::assertTrue(Container::getInstance()->bound(Config::class));

Eloquent::init();
}
}

0 comments on commit b9082da

Please sign in to comment.