From 6fd1cef9904e1924e80d8321e2f99b87fb8dc76d Mon Sep 17 00:00:00 2001 From: Gerald Lam Date: Sat, 27 Jan 2018 00:52:51 +0800 Subject: [PATCH 1/3] Added ability to disable auto update for a currency. --- ...alter_currency_table_add_update_column.php | 45 +++++++++++++++++++ src/Contracts/DriverInterface.php | 23 +++++----- src/Drivers/Database.php | 17 +++++-- src/Drivers/Filesystem.php | 12 ++++- 4 files changed, 80 insertions(+), 17 deletions(-) create mode 100644 database/migrations/2013_11_26_161502_alter_currency_table_add_update_column.php diff --git a/database/migrations/2013_11_26_161502_alter_currency_table_add_update_column.php b/database/migrations/2013_11_26_161502_alter_currency_table_add_update_column.php new file mode 100644 index 0000000..07f6e03 --- /dev/null +++ b/database/migrations/2013_11_26_161502_alter_currency_table_add_update_column.php @@ -0,0 +1,45 @@ +table_name = config('currency.drivers.database.table'); + } + + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::table($this->table_name, function ($table) { + $table->boolean('auto_update')->default(true)->after('active'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table($this->table_name, function ($table) { + $table->dropColumn('auto_update'); + }); + } +} diff --git a/src/Contracts/DriverInterface.php b/src/Contracts/DriverInterface.php index 8097e4a..1317e0d 100644 --- a/src/Contracts/DriverInterface.php +++ b/src/Contracts/DriverInterface.php @@ -31,17 +31,18 @@ public function all(); * @return mixed */ public function find($code, $active = 1); - - /** - * Update given currency. - * - * @param string $code - * @param array $attributes - * @param DateTime $timestamp - * - * @return int - */ - public function update($code, array $attributes, DateTime $timestamp = null); + + /** + * Update given currency. + * + * @param string $code + * @param array $attributes + * @param DateTime $timestamp + * @param bool $auto Whether or not it's performed automatically + * + * @return int + */ + public function update($code, array $attributes, DateTime $timestamp = null, $auto = false); /** * Remove given currency from storage. diff --git a/src/Drivers/Database.php b/src/Drivers/Database.php index e326e60..f060ee8 100644 --- a/src/Drivers/Database.php +++ b/src/Drivers/Database.php @@ -47,6 +47,7 @@ public function create(array $params) 'format' => '', 'exchange_rate' => 1, 'active' => 0, + 'auto_update' => 1, 'created_at' => $created, 'updated_at' => $created, ], $params); @@ -97,7 +98,7 @@ public function find($code, $active = 1) /** * {@inheritdoc} */ - public function update($code, array $attributes, DateTime $timestamp = null) + public function update($code, array $attributes, DateTime $timestamp = null, $auto = false) { $table = $this->config('table'); @@ -106,9 +107,17 @@ public function update($code, array $attributes, DateTime $timestamp = null) $attributes['updated_at'] = new DateTime('now'); } - return $this->database->table($table) - ->where('code', strtoupper($code)) - ->update($attributes); + //Only apply to those with auto update if it's automatically updating + if ($auto) { + return $this->database->table($table) + ->where('code', strtoupper($code)) + ->where('auto_update', 1) + ->update($attributes); + } else { + return $this->database->table($table) + ->where('code', strtoupper($code)) + ->update($attributes); + } } /** diff --git a/src/Drivers/Filesystem.php b/src/Drivers/Filesystem.php index 16b7c68..1c9ac48 100644 --- a/src/Drivers/Filesystem.php +++ b/src/Drivers/Filesystem.php @@ -54,6 +54,7 @@ public function create(array $params) 'format' => '', 'exchange_rate' => 1, 'active' => 0, + 'auto_update' => 1, 'created_at' => $created, 'updated_at' => $created, ], $params); @@ -95,7 +96,7 @@ public function find($code, $active = 1) /** * {@inheritdoc} */ - public function update($code, array $attributes, DateTime $timestamp = null) + public function update($code, array $attributes, DateTime $timestamp = null, $auto = false) { // Get blacklist path $path = $this->config('path'); @@ -114,7 +115,14 @@ public function update($code, array $attributes, DateTime $timestamp = null) } // Merge values - $currencies[$code] = array_merge($currencies[$code], $attributes); + if ($auto) { + //If auto update disabled + if ($currencies[$code]['auto_update']) { + $currencies[$code] = array_merge($currencies[$code], $attributes); + } + } else { + $currencies[ $code ] = array_merge($currencies[ $code ], $attributes); + } return $this->filesystem->put($path, json_encode($currencies, JSON_PRETTY_PRINT)); } From 9989ed87e586912e93be9d1b7572e5d1cd13ab14 Mon Sep 17 00:00:00 2001 From: Gerald Lam Date: Sat, 27 Jan 2018 01:06:28 +0800 Subject: [PATCH 2/3] Support for Laravel 5.5 Auto Discovery --- composer.json | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/composer.json b/composer.json index 8c0537b..41149e9 100644 --- a/composer.json +++ b/composer.json @@ -24,5 +24,15 @@ "psr-4": { "Torann\\Currency\\": "src/" } + }, + "extra": { + "laravel": { + "providers": [ + "Torann\\Currency\\CurrencyServiceProvider" + ], + "aliases": { + "Currency": "Torann\\Currency\\Facades\\Currency" + } + } } } From bef6b0e6510b6f71eaaf06d068563cc3ab2e3391 Mon Sep 17 00:00:00 2001 From: Gerald Lam Date: Sat, 27 Jan 2018 01:37:40 +0800 Subject: [PATCH 3/3] Class name for migration fix --- ...2013_11_26_161502_alter_currency_table_add_update_column.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/migrations/2013_11_26_161502_alter_currency_table_add_update_column.php b/database/migrations/2013_11_26_161502_alter_currency_table_add_update_column.php index 07f6e03..b93e4b6 100644 --- a/database/migrations/2013_11_26_161502_alter_currency_table_add_update_column.php +++ b/database/migrations/2013_11_26_161502_alter_currency_table_add_update_column.php @@ -2,7 +2,7 @@ use Illuminate\Database\Migrations\Migration; -class CreateCurrencyTable extends Migration +class AlterCurrencyTableAddUpdateColumn extends Migration { /** * Currencies table name