diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dee93ee --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/vendor/* +/.idea/* \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..565291a --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2019 Mpande Andrew + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..3c9e601 --- /dev/null +++ b/composer.json @@ -0,0 +1,30 @@ +{ + "name": "fannypack/laracombee-multi-db", + "description": "A Recombee with multiple databases integration for Laravel", + "type": "library", + "require": { + "amranidev/laracombee": "^0.1.37" + }, + "license": "MIT", + "authors": [ + { + "name": "Mpande Andrew", + "email": "andrewmvp007@gmail.com" + } + ], + "autoload": { + "psr-4": { + "FannyPack\\LaracombeeMultiDB\\": "src/" + } + }, + "extra": { + "laravel": { + "providers": [ + "FannyPack\\LaracombeeMultiDB\\LaracombeeMultiDBServiceProvider" + ], + "aliases": { + "MultiDB": "FannyPack\\LaracombeeMultiDB\\MultiDBFacade" + } + } + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..d01efcb --- /dev/null +++ b/composer.lock @@ -0,0 +1,743 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "e54bec83489c0a11dffc0894fdfb66c8", + "packages": [ + { + "name": "amranidev/laracombee", + "version": "v0.1.37", + "source": { + "type": "git", + "url": "https://github.com/amranidev/laracombee.git", + "reference": "da75b622eeba6274cd8735276be1ae95b0e269a7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/amranidev/laracombee/zipball/da75b622eeba6274cd8735276be1ae95b0e269a7", + "reference": "da75b622eeba6274cd8735276be1ae95b0e269a7", + "shasum": "" + }, + "require": { + "guzzlehttp/promises": "^1.3", + "recombee/php-api-client": "^2.2" + }, + "require-dev": { + "orchestra/testbench": "3.7.0", + "phpunit/phpunit": "7.0" + }, + "type": "laravel-package", + "extra": { + "laravel": { + "providers": [ + "Amranidev\\Laracombee\\Providers\\LaracombeeServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Amranidev\\Laracombee\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Houssain Amrani", + "email": "amranidev@gmail.com" + } + ], + "description": "Recommendation system for laravel", + "keywords": [ + "laravel", + "laravel and recombee", + "laravel with recombee", + "php", + "recombee", + "recommendation system" + ], + "time": "2019-04-21T22:39:22+00:00" + }, + { + "name": "doctrine/inflector", + "version": "v1.3.0", + "source": { + "type": "git", + "url": "https://github.com/doctrine/inflector.git", + "reference": "5527a48b7313d15261292c149e55e26eae771b0a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/inflector/zipball/5527a48b7313d15261292c149e55e26eae771b0a", + "reference": "5527a48b7313d15261292c149e55e26eae771b0a", + "shasum": "" + }, + "require": { + "php": "^7.1" + }, + "require-dev": { + "phpunit/phpunit": "^6.2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.3.x-dev" + } + }, + "autoload": { + "psr-4": { + "Doctrine\\Common\\Inflector\\": "lib/Doctrine/Common/Inflector" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com" + }, + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + }, + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com" + } + ], + "description": "Common String Manipulations with regard to casing and singular/plural rules.", + "homepage": "http://www.doctrine-project.org", + "keywords": [ + "inflection", + "pluralize", + "singularize", + "string" + ], + "time": "2018-01-09T20:05:19+00:00" + }, + { + "name": "guzzlehttp/promises", + "version": "v1.3.1", + "source": { + "type": "git", + "url": "https://github.com/guzzle/promises.git", + "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/promises/zipball/a59da6cf61d80060647ff4d3eb2c03a2bc694646", + "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646", + "shasum": "" + }, + "require": { + "php": ">=5.5.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Promise\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Guzzle promises library", + "keywords": [ + "promise" + ], + "time": "2016-12-20T10:07:11+00:00" + }, + { + "name": "illuminate/contracts", + "version": "v5.7.28", + "source": { + "type": "git", + "url": "https://github.com/illuminate/contracts.git", + "reference": "b63324d349a8ae2156fbc2697c1ccc85879b3803" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/illuminate/contracts/zipball/b63324d349a8ae2156fbc2697c1ccc85879b3803", + "reference": "b63324d349a8ae2156fbc2697c1ccc85879b3803", + "shasum": "" + }, + "require": { + "php": "^7.1.3", + "psr/container": "^1.0", + "psr/simple-cache": "^1.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.7-dev" + } + }, + "autoload": { + "psr-4": { + "Illuminate\\Contracts\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + } + ], + "description": "The Illuminate Contracts package.", + "homepage": "https://laravel.com", + "time": "2019-02-12T07:46:48+00:00" + }, + { + "name": "illuminate/support", + "version": "v5.7.28", + "source": { + "type": "git", + "url": "https://github.com/illuminate/support.git", + "reference": "3e2810145f37eb89fa11759781ee88ee1c1a5262" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/illuminate/support/zipball/3e2810145f37eb89fa11759781ee88ee1c1a5262", + "reference": "3e2810145f37eb89fa11759781ee88ee1c1a5262", + "shasum": "" + }, + "require": { + "doctrine/inflector": "^1.1", + "ext-mbstring": "*", + "illuminate/contracts": "5.7.*", + "nesbot/carbon": "^1.26.3", + "php": "^7.1.3" + }, + "conflict": { + "tightenco/collect": "<5.5.33" + }, + "suggest": { + "illuminate/filesystem": "Required to use the composer class (5.7.*).", + "moontoast/math": "Required to use ordered UUIDs (^1.1).", + "ramsey/uuid": "Required to use Str::uuid() (^3.7).", + "symfony/process": "Required to use the composer class (^4.1).", + "symfony/var-dumper": "Required to use the dd function (^4.1)." + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.7-dev" + } + }, + "autoload": { + "psr-4": { + "Illuminate\\Support\\": "" + }, + "files": [ + "helpers.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + } + ], + "description": "The Illuminate Support package.", + "homepage": "https://laravel.com", + "time": "2019-02-12T07:57:07+00:00" + }, + { + "name": "nesbot/carbon", + "version": "1.37.1", + "source": { + "type": "git", + "url": "https://github.com/briannesbitt/Carbon.git", + "reference": "5be4fdf97076a685b23efdedfc2b73ad0c5eab70" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/5be4fdf97076a685b23efdedfc2b73ad0c5eab70", + "reference": "5be4fdf97076a685b23efdedfc2b73ad0c5eab70", + "shasum": "" + }, + "require": { + "php": ">=5.3.9", + "symfony/translation": "~2.6 || ~3.0 || ~4.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.8.35 || ^5.7" + }, + "suggest": { + "friendsofphp/php-cs-fixer": "Needed for the `composer phpcs` command. Allow to automatically fix code style.", + "phpstan/phpstan": "Needed for the `composer phpstan` command. Allow to detect potential errors." + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Carbon\\Laravel\\ServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Brian Nesbitt", + "email": "brian@nesbot.com", + "homepage": "http://nesbot.com" + } + ], + "description": "A simple API extension for DateTime.", + "homepage": "http://carbon.nesbot.com", + "keywords": [ + "date", + "datetime", + "time" + ], + "time": "2019-04-19T10:27:42+00:00" + }, + { + "name": "psr/container", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/container.git", + "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/container/zipball/b7ce3b176482dbbc1245ebf52b181af44c2cf55f", + "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Container\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common Container Interface (PHP FIG PSR-11)", + "homepage": "https://github.com/php-fig/container", + "keywords": [ + "PSR-11", + "container", + "container-interface", + "container-interop", + "psr" + ], + "time": "2017-02-14T16:28:37+00:00" + }, + { + "name": "psr/simple-cache", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/php-fig/simple-cache.git", + "reference": "408d5eafb83c57f6365a3ca330ff23aa4a5fa39b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/simple-cache/zipball/408d5eafb83c57f6365a3ca330ff23aa4a5fa39b", + "reference": "408d5eafb83c57f6365a3ca330ff23aa4a5fa39b", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\SimpleCache\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interfaces for simple caching", + "keywords": [ + "cache", + "caching", + "psr", + "psr-16", + "simple-cache" + ], + "time": "2017-10-23T01:57:42+00:00" + }, + { + "name": "recombee/php-api-client", + "version": "v2.4.0", + "source": { + "type": "git", + "url": "https://github.com/recombee/php-api-client.git", + "reference": "6dfb0b6a03acde7688bae878a621e9a0b35a18ed" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/recombee/php-api-client/zipball/6dfb0b6a03acde7688bae878a621e9a0b35a18ed", + "reference": "6dfb0b6a03acde7688bae878a621e9a0b35a18ed", + "shasum": "" + }, + "require": { + "php": ">=5.3.0", + "rmccue/requests": "^1.7.0" + }, + "require-dev": { + "phpdocumentor/phpdocumentor": "2.*", + "phpunit/phpunit": "^6.1.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Recombee\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ondřej Fiedler", + "email": "ondrej.fiedler@recombee.com" + } + ], + "description": "PHP client for easy use of the Recombee recommendation API.", + "homepage": "https://recombee.com", + "time": "2019-03-11T13:18:27+00:00" + }, + { + "name": "rmccue/requests", + "version": "v1.7.0", + "source": { + "type": "git", + "url": "https://github.com/rmccue/Requests.git", + "reference": "87932f52ffad70504d93f04f15690cf16a089546" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/rmccue/Requests/zipball/87932f52ffad70504d93f04f15690cf16a089546", + "reference": "87932f52ffad70504d93f04f15690cf16a089546", + "shasum": "" + }, + "require": { + "php": ">=5.2" + }, + "require-dev": { + "requests/test-server": "dev-master" + }, + "type": "library", + "autoload": { + "psr-0": { + "Requests": "library/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "ISC" + ], + "authors": [ + { + "name": "Ryan McCue", + "homepage": "http://ryanmccue.info" + } + ], + "description": "A HTTP library written in PHP, for human beings.", + "homepage": "http://github.com/rmccue/Requests", + "keywords": [ + "curl", + "fsockopen", + "http", + "idna", + "ipv6", + "iri", + "sockets" + ], + "time": "2016-10-13T00:11:37+00:00" + }, + { + "name": "symfony/contracts", + "version": "v1.0.2", + "source": { + "type": "git", + "url": "https://github.com/symfony/contracts.git", + "reference": "1aa7ab2429c3d594dd70689604b5cf7421254cdf" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/contracts/zipball/1aa7ab2429c3d594dd70689604b5cf7421254cdf", + "reference": "1aa7ab2429c3d594dd70689604b5cf7421254cdf", + "shasum": "" + }, + "require": { + "php": "^7.1.3" + }, + "require-dev": { + "psr/cache": "^1.0", + "psr/container": "^1.0" + }, + "suggest": { + "psr/cache": "When using the Cache contracts", + "psr/container": "When using the Service contracts", + "symfony/cache-contracts-implementation": "", + "symfony/service-contracts-implementation": "", + "symfony/translation-contracts-implementation": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Contracts\\": "" + }, + "exclude-from-classmap": [ + "**/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "A set of abstractions extracted out of the Symfony components", + "homepage": "https://symfony.com", + "keywords": [ + "abstractions", + "contracts", + "decoupling", + "interfaces", + "interoperability", + "standards" + ], + "time": "2018-12-05T08:06:11+00:00" + }, + { + "name": "symfony/polyfill-mbstring", + "version": "v1.11.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-mbstring.git", + "reference": "fe5e94c604826c35a32fa832f35bd036b6799609" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/fe5e94c604826c35a32fa832f35bd036b6799609", + "reference": "fe5e94c604826c35a32fa832f35bd036b6799609", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "suggest": { + "ext-mbstring": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.11-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Mbstring\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for the Mbstring extension", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "mbstring", + "polyfill", + "portable", + "shim" + ], + "time": "2019-02-06T07:57:58+00:00" + }, + { + "name": "symfony/translation", + "version": "v4.2.8", + "source": { + "type": "git", + "url": "https://github.com/symfony/translation.git", + "reference": "181a426dd129cb496f12d7e7555f6d0b37a7615b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/translation/zipball/181a426dd129cb496f12d7e7555f6d0b37a7615b", + "reference": "181a426dd129cb496f12d7e7555f6d0b37a7615b", + "shasum": "" + }, + "require": { + "php": "^7.1.3", + "symfony/contracts": "^1.0.2", + "symfony/polyfill-mbstring": "~1.0" + }, + "conflict": { + "symfony/config": "<3.4", + "symfony/dependency-injection": "<3.4", + "symfony/yaml": "<3.4" + }, + "provide": { + "symfony/translation-contracts-implementation": "1.0" + }, + "require-dev": { + "psr/log": "~1.0", + "symfony/config": "~3.4|~4.0", + "symfony/console": "~3.4|~4.0", + "symfony/dependency-injection": "~3.4|~4.0", + "symfony/finder": "~2.8|~3.0|~4.0", + "symfony/http-kernel": "~3.4|~4.0", + "symfony/intl": "~3.4|~4.0", + "symfony/var-dumper": "~3.4|~4.0", + "symfony/yaml": "~3.4|~4.0" + }, + "suggest": { + "psr/log-implementation": "To use logging capability in translator", + "symfony/config": "", + "symfony/yaml": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.2-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Translation\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Translation Component", + "homepage": "https://symfony.com", + "time": "2019-05-01T12:55:36+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [] +} diff --git a/config/laracombee-multi-db.php b/config/laracombee-multi-db.php new file mode 100644 index 0000000..57c1f4a --- /dev/null +++ b/config/laracombee-multi-db.php @@ -0,0 +1,23 @@ + 'default', + 'databases' => [ + 'default' => [ + 'database' => '', + 'token' => '', + 'timeout' => 2000, + 'protocol' => 'http', + 'parties' => [ + 'user' => '', + 'item' => '', + ], + ] + ] +]; \ No newline at end of file diff --git a/src/Console/Commands/AddColumnsCommand.php b/src/Console/Commands/AddColumnsCommand.php new file mode 100644 index 0000000..2f67fad --- /dev/null +++ b/src/Console/Commands/AddColumnsCommand.php @@ -0,0 +1,74 @@ +option('to')) { + $this->error('--to option is required!'); + die(); + } + + MultiDBFacade::db($this->db())->batch($this->loadColumns($this->argument('columns'))->all()) + ->then(function ($response) { + $this->info('Done!'); + }) + ->otherwise(function ($error) { + $this->error($error); + }) + ->wait(); + } + + /** + * Load columns. + * + * @param array $columns + * + * @return \Illuminate\Support\Collection + */ + public function loadColumns(array $columns) + { + return collect($columns)->map(function (string $column) { + list($property, $type) = explode(':', $column); + + return $this->{'add'.ucfirst($this->option('to')).'Property'}($property, $type); + }); + } +} diff --git a/src/Console/Commands/DropColumnsCommand.php b/src/Console/Commands/DropColumnsCommand.php new file mode 100644 index 0000000..e32a036 --- /dev/null +++ b/src/Console/Commands/DropColumnsCommand.php @@ -0,0 +1,73 @@ +option('from')) { + $this->error('--from option is required!'); + die(); + } + + MultiDBFacade::db($this->db())->batch($this->loadColumns($this->argument('columns'))->all()) + ->then(function ($response) { + $this->info('Done!'); + }) + ->otherwise(function ($error) { + $this->error($error); + }) + ->wait(); + } + + /** + * Load columns. + * + * @param array $columns + * + * @return \Illuminate\Support\Collection + */ + public function loadColumns(array $columns) + { + return collect($columns)->map(function (string $column) { + return $this->{'delete'.ucfirst($this->option('from')).'Property'}($column); + }); + } +} diff --git a/src/Console/Commands/MigrateCommand.php b/src/Console/Commands/MigrateCommand.php new file mode 100644 index 0000000..54279af --- /dev/null +++ b/src/Console/Commands/MigrateCommand.php @@ -0,0 +1,71 @@ +prepareScope()->all(); + + MultiDBFacade::db($this->db())->batch($scope) + ->then(function ($response) { + $this->info('Done!'); + }) + ->otherwise(function ($error) { + $this->error($error); + }) + ->wait(); + } + + /** + * Prepare scope. + * + * @return mixed + */ + public function prepareScope() + { + $class = config("laracombee-multi-db.databases.{$this->db()}.parties.{$this->argument('type')}"); + + $properties = $class::$laracombee; + + return collect($properties)->map(function (string $type, string $property) { + return $this->{'add'.ucfirst($this->argument('type')).'Property'}($property, $type); + }); + } +} diff --git a/src/Console/Commands/ResetDatabaseCommand.php b/src/Console/Commands/ResetDatabaseCommand.php new file mode 100644 index 0000000..d8d3f73 --- /dev/null +++ b/src/Console/Commands/ResetDatabaseCommand.php @@ -0,0 +1,52 @@ +confirm('All your recombee data will be erased, including items, item properties, series, user database, purchases, ratings, detail views, and bookmarks. Make sure the request to be never executed in production environment! Resetting your database is irreversible. Are you sure?')) { + $request = MultiDBFacade::db($this->db())->resetDatabase(); + MultiDBFacade::db($this->db())->send($request)->then(function ($response) { + $this->info('Recombee data has been erased!'); + })->otherwise(function ($error) { + $this->error($error); + })->wait(); + } + } +} diff --git a/src/Console/Commands/RollbackCommand.php b/src/Console/Commands/RollbackCommand.php new file mode 100644 index 0000000..23633cd --- /dev/null +++ b/src/Console/Commands/RollbackCommand.php @@ -0,0 +1,71 @@ +prepareScope()->all(); + + MultiDBFacade::db($this->db())->batch($scope) + ->then(function ($response) { + $this->info('Done!'); + }) + ->otherwise(function ($error) { + $this->error($error); + }) + ->wait(); + } + + /** + * Prepare scope. + * + * @return mixed + */ + public function prepareScope() + { + $class = config("laracombee-multi-db.databases.{$this->db()}.parties.{$this->argument('type')}"); + + $properties = $class::$laracombee; + + return collect($properties)->map(function (string $type, string $property) { + return $this->{'delete'.ucfirst($this->argument('type')).'Property'}($property, $type); + }); + } +} diff --git a/src/Console/Commands/SeedCommand.php b/src/Console/Commands/SeedCommand.php new file mode 100644 index 0000000..6d8f2bc --- /dev/null +++ b/src/Console/Commands/SeedCommand.php @@ -0,0 +1,78 @@ +option('chunk') ?: self::$chunk; + + $class = config("laracombee-multi-db.databases.{$this->db()}.parties.{$this->argument('type')}"); + + $records = $class::all(); + + $total = $records->count(); + + $bar = $this->output->createProgressBar($total / $chunk); + + $records->chunk($chunk)->each(function ($users) use ($bar) { + $batch = $this->{'add'.ucfirst($this->argument('type')).'s'}($users->all()); + MultiDBFacade::db($this->db())->batch($batch)->then(function ($response) use ($bar) { + })->otherwise(function ($error) { + $this->info(''); + $this->error($error); + die(); + })->wait(); + + $bar->advance(); + }); + + $bar->finish(); + + $this->info(''); + $this->info('Done!'); + } +} diff --git a/src/Console/LaracombeeCommand.php b/src/Console/LaracombeeCommand.php new file mode 100644 index 0000000..8ca1bdf --- /dev/null +++ b/src/Console/LaracombeeCommand.php @@ -0,0 +1,123 @@ +option('db'); + } + + /** + * Add User property. + * + * @param string $property. + * @param string $type. + * + * @return \Recombee\RecommApi\Requests\AddUserProperty + */ + public function addUserProperty(string $property, string $type) + { + return MultiDBFacade::db($this->db())->addUserProperty($property, $type); + } + + /** + * Add Item property. + * + * @param string $property. + * @param string $type. + * + * @return \Recombee\RecommApi\Requests\AddItemProperty + */ + public function addItemProperty(string $property, string $type) + { + return MultiDBFacade::db($this->db())->addItemProperty($property, $type); + } + + /** + * Delete User property. + * + * @param string $property. + * + * @return \Recombee\RecommApi\Requests\DeleteUserProperty + */ + public function deleteUserProperty(string $property) + { + return MultiDBFacade::db($this->db())->deleteUserProperty($property); + } + + /** + * Delete Item property. + * + * @param string $property. + * + * @return \Recombee\RecommApi\Requests\DeleteItemProperty + */ + public function deleteItemProperty(string $property) + { + return MultiDBFacade::db($this->db())->deleteItemProperty($property); + } + + /** + * Add user to recombee. + * + * @param \Illuminate\Foundation\Auth\User $user. + * + * @return \Recombee\RecommApi\Requests\Request + */ + public function addUser(User $user) + { + return MultiDBFacade::db($this->db())->addUser($user); + } + + /** + * Add item to recombee. + * + * @param \Illuminate\Database\Eloquent\Model $item. + * + * @return \Recombee\RecommApi\Requests\Request + */ + public function addItem(Model $item) + { + return MultiDBFacade::db($this->db())->addItem($item); + } + + /** + * Add users as bulk. + * + * @param array $batch. + * + * @return \Recombee\RecommApi\Requests\Request + */ + public function addUsers(array $batch) + { + return MultiDBFacade::db($this->db())->addUsers($batch); + } + + /** + * Add items as bulk. + * + * @param array $batch. + * + * @return \Recombee\RecommApi\Requests\Request + */ + public function addItems(array $batch) + { + return MultiDBFacade::db($this->db())->addItems($batch); + } +} diff --git a/src/DatabaseManager.php b/src/DatabaseManager.php new file mode 100644 index 0000000..dff4534 --- /dev/null +++ b/src/DatabaseManager.php @@ -0,0 +1,90 @@ +app = $app; + } + + /** + * Get the default driver name. + * + * @return string + */ + public function getDefaultDriver() + { + return $this->app['config']['laracombee-multi-db.default']; + } + + /** + * Create new db access + * + * @param null $db + * @return Laracombee + */ + public function db($db = null) { + return $this->driver($db); + } + + /** + * Create access to default db + * + * @return Laracombee + */ + public function createDefaultDriver() { + return $this->createInstance($this->getDefaultDriver()); + } + + /** + * Call a custom driver creator. + * + * @param string $driver + * @return mixed + */ + protected function callCustomCreator($driver) + { + $config = $this->getConfig($driver); + + return $this->customCreators[$driver]($config); + } + + /** + * Get the payment method configuration. + * + * @param string $name + * @return array + */ + protected function getConfig($name) + { + return $this->app['config']["laracombee-multi-db.databases.{$name}"]; + } + + protected function createInstance($name) { + $config = $this->getConfig($name); + + return new Laracombee( + $config["database"], $config["token"], $config["protocol"], $config["timeout"] + ); + } +} \ No newline at end of file diff --git a/src/Laracombee.php b/src/Laracombee.php new file mode 100644 index 0000000..8ca4c26 --- /dev/null +++ b/src/Laracombee.php @@ -0,0 +1,165 @@ +toArray())->filter(function ($value, $key) use ($laracombeeProperties) { + return isset($laracombeeProperties[$key]); + })->all(); + + return $this->setUserValues($user->id, $values); + } + + /** + * Add multiple users to recombee db. + * + * @param array $users + * + * @return array + */ + public function addUsers(array $users) + { + return array_map(function ($user) { + return $this->addUser($user); + }, $users); + } + + /** + * Update a user in recombee. + * + * @param \Illuminate\Foundation\Auth\User $user + * + * @return \Recombee\RecommApi\Requests\SetUserValues + */ + public function updateUser(User $user) + { + return $this->addUser($user); + } + + /** + * Merge users. + * + * @param \Illuminate\Foundation\Auth\User $target_user + * @param \Illuminate\Foundation\Auth\User $source_user + * + * @return \Recombee\RecommApi\Requests\MergeUsers + */ + public function mergeUsers(User $target_user, User $source_user) + { + return $this->mergeUsersWithId($target_user->id, $source_user->id, ['cascade_create' => true]); + } + + /** + * Add an item to recombee db. + * + * @param \Illuminate\Database\Eloquent\Model $item + * + * @return \Recombee\RecommApi\Requests\SetItemValues + */ + public function addItem(Model $item) + { + $laracombeeProperties = $item::$laracombee; + + $values = collect($item->toArray())->filter(function ($value, $key) use ($laracombeeProperties) { + return isset($laracombeeProperties[$key]); + })->all(); + + return $this->setItemValues($item->id, $values); + } + + /** + * Update an item in recombee db. + * + * @param \Illuminate\Database\Eloquent\Model $item + * + * @return \Recombee\RecommApi\Requests\SetItemValues + */ + public function updateItem(Model $item) + { + return $this->addItem($item); + } + + /** + * Add multiple items to recombee db. + * + * @param array $items + * + * @return array + */ + public function addItems(array $items) + { + return array_map(function ($item) { + return $this->addItem($item); + }, $items); + } + + /** + * Recommend items to user. + * + * @param \Illuminate\Foundation\Auth\User $user + * @param int $limit + * @param array $options + * + * @return mixed + */ + public function recommendTo(User $user, $limit = 10, $options = []) + { + return $this->recommendItemsToUser($user->id, $limit, $options); + } + + /** + * Send request. + * + * @param \Recombee\RecommApi\Requests\Request $request + * + * @return \GuzzleHttp\Promise\Promise|mixed + */ + public function send(Request $request) + { + return $promise = new Promise(function () use (&$promise, $request) { + try { + $request->setTimeout($this->timeout); + $response = $this->client->send($request); + $promise->resolve($response); + } catch (ApiTimeoutException $e) { + $promise->reject($e->getMessage()); + } catch (ResponseException $e) { + $promise->reject($e->getMessage()); + } catch (ApiException $e) { + $promise->reject($e->getMessage()); + } + }); + } +} \ No newline at end of file diff --git a/src/LaracombeeMultiDBServiceProvider.php b/src/LaracombeeMultiDBServiceProvider.php new file mode 100644 index 0000000..77ad66e --- /dev/null +++ b/src/LaracombeeMultiDBServiceProvider.php @@ -0,0 +1,56 @@ +app->singleton('laracombee-multi-db', function () { + return new DatabaseManager($this->app); + }); + } + + /** + * Bootstrap services. + * + * @return void + */ + public function boot() + { + $this->publishes([ + __DIR__.'/../config/laracombee-multi-db.php' => config_path('laracombee-multi-db.php'), + ]); + + $this->commands( + [ + SeedCommand::class, + MigrateCommand::class, + RollbackCommand::class, + AddColumnsCommand::class, + DropColumnsCommand::class, + ResetDatabaseCommand::class, + ] + ); + } +} \ No newline at end of file diff --git a/src/MultiDBFacade.php b/src/MultiDBFacade.php new file mode 100644 index 0000000..781b0ac --- /dev/null +++ b/src/MultiDBFacade.php @@ -0,0 +1,19 @@ +