Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature : add API enable/disable configuration for Algerian Cities package #21

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ It provides functionality to load Wilayas (provinces) and Communes (municipaliti
- Wilaya and Commune Eloquent models with relationships.
- Supports Arabic and French languages.
- Includes postal codes and latitude/longitude for each commune.
- Available as API endpoints.
- Helper functions for easy integration in Blade views.
- [Helper functions for easy integration in Blade views](#using-helper-functions).
- [Available as API endpoints](#using-the-package-as-an-api).

## Requirements

Expand Down Expand Up @@ -51,23 +51,14 @@ The package provides two models: `Wilaya` and `Commune`.
A `Wilaya` has many `Commune`, and you can interact with them just like any other Eloquent models.

```php
namespace App;

use Illuminate\Database\Eloquent\Model;
use Kossa\AlgerianCities\Commune;
use Kossa\AlgerianCities\Wilaya;

class AnyClass extends Model
{
// Retrieve all Wilayas
$wilayas = Wilaya::all();

// Retrieve all Communes
$communes = Commune::all();

// Get all Communes belonging to Algiers (Wilaya ID: 16)
$algiers_communes = Commune::where('wilaya_id', 16)->get();
}
// Retrieve all Wilayas
$wilayas = Wilaya::all();

// Retrieve all Communes
$communes = Commune::all();

// Get all Communes belonging to Algiers (Wilaya ID: 16)
$algiers_communes = Commune::where('wilaya_id', 16)->get();
```

### Using Helper Functions
Expand Down Expand Up @@ -143,6 +134,16 @@ This package includes `api.php` routes, allowing you to interact with the data t
| GET | `/api/search/wilaya/{q}` | Search Wilayas by name or Arabic name |
| GET | `/api/search/commune/{q}` | Search Communes by name or Arabic name |

### API Availability Toggle

You can enable or disable the Algerian Cities API endpoints by setting the following option in your `.env` file:

```dotenv
ALGERIAN_CITIES_API_ENABLED=false # Default: true
```

----

## Future Planned Features

- [ ] Add support for Dairas (districts), including relationships with Wilayas and Communes
Expand Down
9 changes: 9 additions & 0 deletions config/algerian-cities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

return [

/**
* Enable or disable the Algerian Cities API
*/
'api_enabled' => env('ALGERIAN_CITIES_API_ENABLED', true),
];
16 changes: 11 additions & 5 deletions src/Providers/AlgerianCitiesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ public function boot()
]);
}

// API
$this->loadRoutesFrom(__DIR__.'/../../routes/api.php');
// Config file
$this->publishes([
__DIR__.'/../../config/algerian-cities.php' => config_path('algerian-cities.php'),
], 'config');

if (config('algerian-cities.api_enabled')) {
$this->loadRoutesFrom(__DIR__.'/../../routes/api.php');
}

require __DIR__.'/../helpers.php';
}
Expand All @@ -50,8 +56,8 @@ public function register()
* Uncomment this function call to load the config file.
* If the config file is also publishable, it will merge with that file
*/
// $this->mergeConfigFrom(
// __DIR__.'/../../config/algerian-cities.php', 'algerian-cities'
// );
$this->mergeConfigFrom(
__DIR__.'/../../config/algerian-cities.php', 'algerian-cities'
);
}
}
12 changes: 5 additions & 7 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kossa\AlgerianCities\Tests;

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Artisan;

Expand All @@ -14,7 +15,7 @@ class TestCase extends \Orchestra\Testbench\TestCase
{
use RefreshDatabase;

public function getEnvironmentSetUp($app)
public function getEnvironmentSetUp($app): void
{
$CreateCitiesTable = include __DIR__.'/../database/migrations/2024_10_26_000000_create_cities_table.php.stub';

Expand All @@ -26,13 +27,10 @@ public function getEnvironmentSetUp($app)

/**
* Include the package's service provider(s)
*
* @see https://packages.tools/testbench/basic/testcase.html#package-service-providers
*
* @param \Illuminate\Foundation\Application $app
* @return array
**
* @param Application $app
*/
protected function getPackageProviders($app)
protected function getPackageProviders($app): array
{
return [
\Kossa\AlgerianCities\Providers\AlgerianCitiesServiceProvider::class,
Expand Down