Skip to content

Commit 70b7438

Browse files
initial package
0 parents  commit 70b7438

10 files changed

+439
-0
lines changed

.gitignore

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/vendor
2+
composer.phar
3+
composer.lock
4+
/fixture/vendor
5+
build
6+
coverage
7+
.php_cs.cache
8+
.phpunit.result.cache
9+
.idea
10+
.php-cs-fixer.cache
11+
.DS_Store
12+
src/.DS_Store
13+
tests/.DS_Store

LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Dotswan <tech@dotswan.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Modules Auto-Discover
2+
3+
Automatically discover and register configurations, translations, and more from your [nWidart/laravel-modules](https://github.com/nWidart/laravel-modules) modules.
4+
5+
## Table of Contents
6+
7+
* [Introduction](#introduction)
8+
* [Installation](#installation)
9+
* [Usage](#usage)
10+
* [Auto-Discovery](#auto-discovery)
11+
* [Disabling Auto-Discovery per Module](#disabling-auto-discovery-per-module)
12+
* [Contributing](#contributing)
13+
* [License](#license)
14+
* [Support](#support)
15+
16+
## Introduction
17+
18+
When using the `nWidart/laravel-modules` package for modular Laravel applications, you might have noticed that configurations, translations, and other resources within your modules are not automatically discovered and registered by Laravel and you need to register/discover them in the `ModuleServiceProvider` for every module. This package bridges that gap by automatically discovering and registering these resources, simplifying your module development process.
19+
20+
With **Modules Auto-Discover**, you no longer need to manually call methods like `registerConfigs()` or `registerTranslationss()` in your module service providers. The package handles the discovery and registration automatically during the application's boot process.
21+
22+
## Installation
23+
24+
You can install the package via Composer:
25+
26+
```bash
27+
composer require dotswan/modules-auto-discover
28+
```
29+
30+
The package uses Laravel's auto-discovery feature, so no additional steps are required to register the service provider.
31+
32+
## Usage
33+
34+
Once installed, the package will automatically discover and register the following resources in your enabled modules:
35+
36+
* **Configurations** (`Config/` directory)
37+
* **Translations** (`Lang/` directory)
38+
* **Views** (`Resources/views/` directory)
39+
* **Routes** (`Routes/` directory)
40+
* **Migrations** (`Database/Migrations/` directory)
41+
* **Factories** (`Database/Factories/` directory)
42+
* **Seeds** (`Database/Seeders/` directory)
43+
44+
### Auto-Discovery
45+
46+
By default, auto-discovery is enabled for all your modules. This means that any resources placed in the standard directories will be automatically registered without any additional configuration.
47+
48+
For example, placing a configuration file at `Modules/YourModule/Config/permission.php` will make its contents available via Laravel's `config()` helper:
49+
50+
```php
51+
// Accessing a configuration value from your module
52+
$value = config('permission.key');`
53+
```
54+
**Note:** `nWidart Modules` will create a `config.php` file for every module by default (if you enabled to generate), we recommend you to create a separate file for each configuration to avoid conflicts (don't use `config.php` for all modules)
55+
56+
57+
### Disabling Auto-Discovery per Module
58+
59+
If you want to disable auto-discovery for a specific module, you can do so by adding an `"auto-discovery": false` entry to your module's `module.json` file:
60+
61+
```json
62+
{
63+
"name": "YourModule",
64+
"alias": "yourmodule",
65+
...
66+
"auto-discovery": false,
67+
...
68+
}
69+
```
70+
With `auto-discovery` set to `false`, the package will skip the automatic registration of resources for that module. You can then manually register resources in your module's service provider if needed.
71+
72+
## To-Do
73+
- [x] Discover `Config`
74+
- [x] Discover `Translations`
75+
- [ ] Discover `Views`
76+
- [ ] Discover `Routes`
77+
- [ ] Discover `Commands`
78+
79+
## Contributing
80+
81+
Contributions are welcome and encouraged! Please follow these steps to contribute:
82+
83+
1. Fork the repository on GitHub.
84+
2. Create a new branch for your feature or bug fix:
85+
86+
```bash
87+
git checkout -b feature/your-feature-name
88+
```
89+
90+
3. Make your changes.
91+
4. Run `composer lint` to format your code with Pint rules.
92+
5. Run `composer test` to run tests or `composer test-coverage` to generate a coverage report.
93+
6. Commit your changes with clear messages.
94+
7. Push your branch to your forked repository:
95+
96+
```bash
97+
git push origin feature/your-feature-name
98+
```
99+
100+
8. Open a pull request on the main repository.
101+
102+
Please make sure to write tests for your changes and ensure all existing tests pass.
103+
104+
## License
105+
106+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
107+
108+
## Support
109+
110+
If you encounter any issues or have questions, please contact us via [tech@dotswan.com](mailto:tech@dotswan.com) or open an issue on the [GitHub repository](https://github.com/dotswan/modules-auto-discover/issues).

composer.json

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
"name": "dotswan/modules-auto-discover",
3+
"description": "Discover configs, translations, views, and .. automatically",
4+
"keywords": [
5+
"dotswan",
6+
"laravel",
7+
"nwidart-modules",
8+
"modules"
9+
],
10+
"homepage": "https://github.com/dotswan/modules-auto-discover",
11+
"support": {
12+
"issues": "https://github.com/dotswan/modules-auto-discover/issues",
13+
"source": "https://github.com/dotswan/modules-auto-discover"
14+
},
15+
"authors": [
16+
{
17+
"name": "Majid Alavizadeh",
18+
"email": "tech@dotswan.com"
19+
}
20+
],
21+
"license": "MIT",
22+
"prefer-stable": true,
23+
"require": {
24+
"php": "^8.1",
25+
"nwidart/laravel-modules": "^11.0"
26+
},
27+
"require-dev": {
28+
"laravel/pint": "^1.0",
29+
"orchestra/testbench": "^8.0",
30+
"pestphp/pest": "^2.0",
31+
"pestphp/pest-plugin-arch": "^2.0",
32+
"pestphp/pest-plugin-laravel": "^2.0",
33+
"mockery/mockery": "^1.4"
34+
},
35+
"autoload": {
36+
"psr-4": {
37+
"Dotswan\\ModulesAutoDiscover\\": "src/"
38+
}
39+
},
40+
"autoload-dev": {
41+
"psr-4": {
42+
"Dotswan\\ModulesAutoDiscover\\Tests\\": "tests/"
43+
}
44+
},
45+
"scripts": {
46+
"test": "vendor/bin/pest",
47+
"test-coverage": "vendor/bin/pest --coverage",
48+
"lint": "vendor/bin/pint"
49+
},
50+
"config": {
51+
"allow-plugins": {
52+
"pestphp/pest-plugin": true,
53+
"phpstan/extension-installer": true,
54+
"wikimedia/composer-merge-plugin": false
55+
},
56+
"sort-packages": true
57+
},
58+
"extra": {
59+
"laravel": {
60+
"providers": [
61+
"Dotswan\\ModulesAutoDiscover\\ModulesAutoDiscoverServiceProvider"
62+
]
63+
}
64+
}
65+
}

phpunit.xml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
bootstrap="vendor/autoload.php"
4+
colors="true"
5+
>
6+
<testsuites>
7+
<testsuite name="Default">
8+
<directory suffix=".php">./tests</directory>
9+
</testsuite>
10+
</testsuites>
11+
</phpunit>

pint.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"preset": "laravel"
3+
}
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Dotswan\ModulesAutoDiscover;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use Nwidart\Modules\Laravel\Module;
7+
8+
class ModulesAutoDiscoverServiceProvider extends ServiceProvider
9+
{
10+
public function boot(): void
11+
{
12+
$modules = app()->make('modules')->allEnabled();
13+
$this->autoDiscovery($modules);
14+
}
15+
16+
protected function autoDiscovery(array $modules): void
17+
{
18+
foreach ($modules as $module) {
19+
20+
if ($module->get('auto-discovery') === false) {
21+
continue;
22+
}
23+
24+
$this->registerConfigs($module);
25+
$this->registerTranslations($module);
26+
}
27+
28+
}
29+
30+
protected function registerConfigs(Module $module): void
31+
{
32+
if (file_exists(base_path('bootstrap/cache/config.php'))) {
33+
return;
34+
}
35+
36+
$configPath = "{$module->getPath()}/".config('modules.paths.generator.config.path', 'Config');
37+
38+
if (! is_dir($configPath)) {
39+
return;
40+
}
41+
42+
foreach (glob($configPath.'/*.php') as $configFile) {
43+
$filename = pathinfo($configFile, PATHINFO_FILENAME);
44+
config()->set($filename, array_merge(config()->get($filename, []), require $configFile));
45+
}
46+
}
47+
48+
public function registerTranslations(Module $module): void
49+
{
50+
$translationPath = "{$module->getPath()}/".config('modules.paths.generator.lang.path', 'Lang');
51+
52+
if (! is_dir($translationPath)) {
53+
return;
54+
}
55+
56+
$this->loadJsonTranslationsFrom($translationPath);
57+
$this->loadTranslationsFrom($translationPath, $module->getLowerName());
58+
59+
}
60+
}

0 commit comments

Comments
 (0)