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" + } + } } } 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..b93e4b6 --- /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)); }