From 1d9200a79921c01da8403230d9bf938fefd98fa9 Mon Sep 17 00:00:00 2001 From: Ricardo Paes Date: Fri, 25 Mar 2022 16:42:18 -0300 Subject: [PATCH 1/2] :sparkles: Adding container resolution using `illuminate\container` --- README.md | 37 +++++++++++++++++++++++++++++++++ tests/EloquentContainerTest.php | 8 ++++++- 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..6c62345 --- /dev/null +++ b/README.md @@ -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(); + +``` \ No newline at end of file diff --git a/tests/EloquentContainerTest.php b/tests/EloquentContainerTest.php index 01029fb..71b39a4 100644 --- a/tests/EloquentContainerTest.php +++ b/tests/EloquentContainerTest.php @@ -2,6 +2,8 @@ namespace Like\Database\Tests; +use Illuminate\Container\Container; +use Like\Database\Config; use Like\Database\Eloquent; class EloquentContainerTest extends EloquentTest { @@ -9,6 +11,10 @@ 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()->has(Config::class)); + + Eloquent::init(); } } From e4bc4df0fd9f951d67c88b6115e59f575e1a0243 Mon Sep 17 00:00:00 2001 From: Ricardo Paes Date: Fri, 25 Mar 2022 16:58:09 -0300 Subject: [PATCH 2/2] :sparkles: Adding container resolution using `illuminate\container` Fix using PHP 5.6 --- README.md | 4 ++-- src/Eloquent.php | 4 ++-- tests/EloquentContainerTest.php | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 6c62345..41965f9 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,9 @@ composer require likesistemas/eloquent-external --dev ``` -### How to use +## How to use -#### Using class `ConfigBean` +### Using class `ConfigBean` ```php diff --git a/src/Eloquent.php b/src/Eloquent.php index 9e48acb..aa1933c 100644 --- a/src/Eloquent.php +++ b/src/Eloquent.php @@ -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); diff --git a/tests/EloquentContainerTest.php b/tests/EloquentContainerTest.php index 71b39a4..9d9d719 100644 --- a/tests/EloquentContainerTest.php +++ b/tests/EloquentContainerTest.php @@ -13,7 +13,7 @@ public static function set_up_before_class() { $config = new ConfigBean(); Container::getInstance()->instance(Config::class, $config); - self::assertTrue(Container::getInstance()->has(Config::class)); + self::assertTrue(Container::getInstance()->bound(Config::class)); Eloquent::init(); }