├── .gitignore ├── .php-cs-fixer.php ├── CODE_OF_CONDUCT.md ├── LICENCE ├── README.md ├── composer.json ├── composer.lock ├── config └── filament-modular.php ├── phpunit.xml ├── snap-transparent.png ├── snap.png ├── src ├── Commands │ ├── Aliases │ │ ├── ModuleMakeBelongsToManyCommand.php │ │ ├── ModuleMakeHasManyCommand.php │ │ ├── ModuleMakeHasManyThroughCommand.php │ │ ├── ModuleMakeMorphManyCommand.php │ │ ├── ModuleMakeMorphToManyCommand.php │ │ ├── ModuleMakePageCommand.php │ │ ├── ModuleMakeRelationManagerCommand.php │ │ ├── ModuleMakeResourceCommand.php │ │ └── ModuleMakeWidgetCommand.php │ ├── Concerns │ │ └── InteractsWithFileNames.php │ ├── ModuleMakeBelongsToManyCommand.php │ ├── ModuleMakeHasManyCommand.php │ ├── ModuleMakeHasManyThroughCommand.php │ ├── ModuleMakeMorphManyCommand.php │ ├── ModuleMakeMorphToManyCommand.php │ ├── ModuleMakePageCommand.php │ ├── ModuleMakeRelationManagerCommand.php │ ├── ModuleMakeResourceCommand.php │ └── ModuleMakeWidgetCommand.php └── FilamentModularServiceProvider.php ├── stubs ├── ChartWidget.stub ├── CustomResourcePage.stub ├── Page.stub ├── PageView.stub ├── RelationManager.stub ├── Resource.stub ├── ResourceEditPage.stub ├── ResourceListPage.stub ├── ResourceManagePage.stub ├── ResourcePage.stub ├── ResourceViewPage.stub ├── StatsOverviewWidget.stub ├── TableWidget.stub ├── Widget.stub └── WidgetView.stub └── tests └── TestCase.php /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | /coverage 3 | /vendor 4 | /.phpunit.cache 5 | /.php-cs-fixer.cache 6 | /.phpunit.result.cache 7 | /.phpunit.cache 8 | composer.lock 9 | -------------------------------------------------------------------------------- /.php-cs-fixer.php: -------------------------------------------------------------------------------- 1 | in(["config", "src", "tests"]); 5 | 6 | $config = new PhpCsFixer\Config(); 7 | return $config->setRules([ 8 | '@Symfony' => true, 9 | 'ordered_class_elements' => [ 10 | 'order' => [ 11 | 'case', 12 | 'use_trait', 13 | 'constant_public', 14 | 'constant_protected', 15 | 'constant_private', 16 | 'property_public_static', 17 | 'property_protected_static', 18 | 'property_private_static', 19 | 'method_public_static', 20 | 'method_protected_static', 21 | 'method_private_static', 22 | 'property_public', 23 | 'property_protected', 24 | 'property_private', 25 | 'construct', 26 | 'destruct', 27 | 'magic', 28 | 'method_public', 29 | 'method_protected', 30 | 'method_private' 31 | ] 32 | ] 33 | ]) 34 | ->setFinder($finder); -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | We as members, contributors, and leaders pledge to make participation in our 6 | community a harassment-free experience for everyone, regardless of age, body 7 | size, visible or invisible disability, ethnicity, sex characteristics, gender 8 | identity and expression, level of experience, education, socio-economic status, 9 | nationality, personal appearance, race, religion, or sexual identity 10 | and orientation. 11 | 12 | We pledge to act and interact in ways that contribute to an open, welcoming, 13 | diverse, inclusive, and healthy community. 14 | 15 | ## Our Standards 16 | 17 | Examples of behavior that contributes to a positive environment for our 18 | community include: 19 | 20 | * Demonstrating empathy and kindness toward other people 21 | * Being respectful of differing opinions, viewpoints, and experiences 22 | * Giving and gracefully accepting constructive feedback 23 | * Accepting responsibility and apologizing to those affected by our mistakes, 24 | and learning from the experience 25 | * Focusing on what is best not just for us as individuals, but for the 26 | overall community 27 | 28 | Examples of unacceptable behavior include: 29 | 30 | * The use of sexualized language or imagery, and sexual attention or 31 | advances of any kind 32 | * Trolling, insulting or derogatory comments, and personal or political attacks 33 | * Public or private harassment 34 | * Publishing others' private information, such as a physical or email 35 | address, without their explicit permission 36 | * Other conduct which could reasonably be considered inappropriate in a 37 | professional setting 38 | 39 | ## Enforcement Responsibilities 40 | 41 | Community leaders are responsible for clarifying and enforcing our standards of 42 | acceptable behavior and will take appropriate and fair corrective action in 43 | response to any behavior that they deem inappropriate, threatening, offensive, 44 | or harmful. 45 | 46 | Community leaders have the right and responsibility to remove, edit, or reject 47 | comments, commits, code, wiki edits, issues, and other contributions that are 48 | not aligned to this Code of Conduct, and will communicate reasons for moderation 49 | decisions when appropriate. 50 | 51 | ## Scope 52 | 53 | This Code of Conduct applies within all community spaces, and also applies when 54 | an individual is officially representing the community in public spaces. 55 | Examples of representing our community include using an official e-mail address, 56 | posting via an official social media account, or acting as an appointed 57 | representative at an online or offline event. 58 | 59 | ## Enforcement 60 | 61 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 62 | reported to the community leaders responsible for enforcement at 63 | . 64 | All complaints will be reviewed and investigated promptly and fairly. 65 | 66 | All community leaders are obligated to respect the privacy and security of the 67 | reporter of any incident. 68 | 69 | ## Enforcement Guidelines 70 | 71 | Community leaders will follow these Community Impact Guidelines in determining 72 | the consequences for any action they deem in violation of this Code of Conduct: 73 | 74 | ### 1. Correction 75 | 76 | **Community Impact**: Use of inappropriate language or other behavior deemed 77 | unprofessional or unwelcome in the community. 78 | 79 | **Consequence**: A private, written warning from community leaders, providing 80 | clarity around the nature of the violation and an explanation of why the 81 | behavior was inappropriate. A public apology may be requested. 82 | 83 | ### 2. Warning 84 | 85 | **Community Impact**: A violation through a single incident or series 86 | of actions. 87 | 88 | **Consequence**: A warning with consequences for continued behavior. No 89 | interaction with the people involved, including unsolicited interaction with 90 | those enforcing the Code of Conduct, for a specified period of time. This 91 | includes avoiding interactions in community spaces as well as external channels 92 | like social media. Violating these terms may lead to a temporary or 93 | permanent ban. 94 | 95 | ### 3. Temporary Ban 96 | 97 | **Community Impact**: A serious violation of community standards, including 98 | sustained inappropriate behavior. 99 | 100 | **Consequence**: A temporary ban from any sort of interaction or public 101 | communication with the community for a specified period of time. No public or 102 | private interaction with the people involved, including unsolicited interaction 103 | with those enforcing the Code of Conduct, is allowed during this period. 104 | Violating these terms may lead to a permanent ban. 105 | 106 | ### 4. Permanent Ban 107 | 108 | **Community Impact**: Demonstrating a pattern of violation of community 109 | standards, including sustained inappropriate behavior, harassment of an 110 | individual, or aggression toward or disparagement of classes of individuals. 111 | 112 | **Consequence**: A permanent ban from any sort of public interaction within 113 | the community. 114 | 115 | ## Attribution 116 | 117 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 118 | version 2.0, available at 119 | https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. 120 | 121 | Community Impact Guidelines were inspired by [Mozilla's code of conduct 122 | enforcement ladder](https://github.com/mozilla/diversity). 123 | 124 | [homepage]: https://www.contributor-covenant.org 125 | 126 | For answers to common questions about this code of conduct, see the FAQ at 127 | https://www.contributor-covenant.org/faq. Translations are available at 128 | https://www.contributor-covenant.org/translations. 129 | -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 RealMrHex Ltd 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Cover](https://filament.ams3.digitaloceanspaces.com/521/tFyehKIVV346TS3Emhqj5vhGYbFGbW-metaVW50aXRsZWQtMS0wMS5qcGc=-.jpg) 2 | 3 | ## Filament Modular 4 | 5 | ### Introduction 6 | 7 | Filament Modular is a Laravel package that enables the use of FilamentPHP in the nwidart/laravel-modules Modular structure. 8 | With this package, developers can leverage the power of Filament's admin panel and dashboard components while building modular Laravel applications. 9 | 10 | Filament Modular achieves this by providing Laravel Artisan commands that generate the necessary files and folder structure to integrate Filament into a Laravel module. 11 | By using these commands, developers can quickly and easily set up a modular Laravel application with a fully-functional Filament dashboard. 12 | 13 | ### Installation 14 | 15 | To install Filament Modular, simply add it to your Laravel application using Composer: 16 | 17 | ```shell 18 | composer require realmrhex/filament-modular 19 | ``` 20 | 21 | #### Laravel Auto-Discovery 22 | 23 | Filament Modular is fully compatible with Laravel's package auto-discovery feature, which was introduced in Laravel 5.5. This feature allows packages to be registered with Laravel automatically, without the need for manual registration in the config/app.php file. 24 | 25 | With package auto-discovery, Filament Modular can be installed and integrated with a Laravel application quickly and easily. When you install Filament Modular, Laravel will automatically detect the package and register its service provider. 26 | 27 | The service provider for Filament Modular is responsible for registering the package's commands, event listeners, and other components with Laravel. Once the service provider is registered, you can use the package's features and functionalities without any additional configuration. 28 | 29 | To take advantage of Laravel's package auto-discovery feature, you must be using Laravel 5.5 or higher. If you are using an older version of Laravel, you will need to register the Filament Modular service provider manually in your config/app.php file. 30 | 31 | In summary, Filament Modular's support for Laravel's package auto-discovery feature makes it easy for developers to install and integrate the package with a Laravel application. With no additional configuration required, developers can start using Filament Modular's features and functionalities immediately after installation. 32 | 33 | ### Configuration 34 | 35 | Filament Modular comes with a default configuration file that defines the default paths and settings for FilamentPHP integration with Laravel modules. 36 | If you need to customize these settings, you can publish the configuration file using the `vendor:publish` Artisan command: 37 | ```shell 38 | php artisan vendor:publish --tag=filament-modular-config 39 | ``` 40 | 41 | ### Usage 42 | 43 | You can use all available Filament `make-commands` with `module:` prefix as well with this format. 44 | 45 | ```shell 46 | php artisan module:make-filament-[command] [resource?] [module] 47 | ``` 48 | 49 | #### Commands 50 | 51 | Here is the list of the available commands 52 | 53 | ![snap](https://raw.githubusercontent.com/RealMrHex/filament-modular/master/snap.png) 54 | 55 | #### Options 56 | - -h, --help to Display this help message 57 | - -f, --force to Force overwrite of existing files 58 | 59 | #### Examples 60 | 61 | To create a `Activation` **page** in `User` Module 62 | 63 | ```shell 64 | php artisan module:make-filament-page Activation User 65 | ``` 66 | 67 | To create a `User` **resource** in `User` module 68 | ```shell 69 | php artisan module:make-filament-resource UserResource User 70 | ``` 71 | 72 | ### Conclusion 73 | 74 | With Filament Modular, developers can enjoy the benefits of both FilamentPHP and the nwidart/laravel-modules Modular structure in their Laravel applications. This package provides an easy-to-use solution for integrating Filament into a modular Laravel application, allowing developers to build powerful and flexible admin panels and dashboards with ease. 75 | 76 | ## Extra Details 77 | 78 | ### Contributing 79 | 80 | We welcome and appreciate contributions from the community! 81 | 82 | If you would like to contribute to Filament Modular, please follow these steps: 83 | 84 | 1. Fork the repository and create a new branch for your changes. 85 | 2. Make your changes and ensure that all tests pass. 86 | 3. Submit a pull request with a clear description of your changes and why they are necessary. 87 | 88 | Please note that by contributing to this project, you agree to abide by the [Code of Conduct](https://github.com/RealMrHex/filament-modular/blob/master/CODE_OF_CONDUCT.md). If you have any questions or concerns, please feel free to reach out to us or open an issue on our [GitHub repository](https://github.com/RealMrHex/filament-modular). 89 | 90 | We appreciate all contributions, big and small, and thank you for helping to make Filament Modular a better tool for everyone! 91 | 92 | 93 | ### Security Vulnerabilities 94 | 95 | If you discover a security vulnerability within our product, please send an email to our security team at [RealMrHex@gmail.com](mailto:RealMrHex@gmail.com). 96 | We take all security vulnerabilities seriously and will respond to reports as quickly as possible. We ask that you do not publicly disclose the issue until we have had a chance to address it. 97 | 98 | We appreciate your help in making our product more secure. 99 | 100 | ### Credits 101 | Filament Modular is developed and maintained by [Armin Hooshmand](https://github.com/RealMrHex). 102 | 103 | We would like to thank the following individuals for their contributions to this project: 104 | 105 | - [Mohaphez](https://github.com/mohaphez) for their help with testing and bug reports. 106 | 107 | We would also like to thank the following projects for their inspiration and guidance: 108 | 109 | - [FilamentPHP](https://filamentphp.com/) 110 | - [nwidart/laravel-modules](https://github.com/nWidart/laravel-modules) 111 | 112 | If you would like to contribute to Filament Modular or report any issues you encounter, please visit our [GitHub repository](https://github.com/RealMrHex/Filament-Modular). 113 | 114 | ### License 115 | 116 | Filament Modular is open-sourced software licensed under the [MIT license](https://github.com/RealMrHex/filament-modular/blob/master/LICENCE). This means that you are free to use, modify, and distribute the software as you see fit, as long as you include the original license file in your distribution and give appropriate attribution to the original authors. 117 | 118 | By using Filament Modular, you agree to abide by the terms and conditions of the MIT license. If you have any questions or concerns about the license, please refer to the [full text of the license](https://opensource.org/license/mit/) for more information. 119 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "realmrhex/filament-modular", 3 | "description": "Modular support for filament.", 4 | "type": "library", 5 | "license": "MIT", 6 | "autoload": { 7 | "psr-4": { 8 | "RealMrHex\\FilamentModular\\": "src/" 9 | } 10 | }, 11 | "authors": [ 12 | { 13 | "name": "Armin Hooshmand", 14 | "email": "RealMrHex@gmail.com" 15 | }, 16 | { 17 | "name": "Hessam Taghvaei", 18 | "email": "mohaphez@gmail.com" 19 | } 20 | ], 21 | "minimum-stability": "dev", 22 | "require": { 23 | "php": "^8.1", 24 | "filament/filament": "^2.0", 25 | "nwidart/laravel-modules": ">=9" 26 | }, 27 | "extra": { 28 | "laravel": { 29 | "providers": [ 30 | "RealMrHex\\FilamentModular\\FilamentModularServiceProvider" 31 | ] 32 | } 33 | }, 34 | "require-dev": { 35 | "phpunit/phpunit": "^10.1@dev", 36 | "friendsofphp/php-cs-fixer": "dev-master", 37 | "orchestra/testbench": "dev-develop" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /config/filament-modular.php: -------------------------------------------------------------------------------- 1 | [ 16 | 'namespace' => '\\Pages', 17 | 'path' => '/Pages', 18 | ], 19 | 20 | /* 21 | |-------------------------------------------------------------------------- 22 | | Resources 23 | |-------------------------------------------------------------------------- 24 | | 25 | | This is the namespace and directory that Filament will automatically 26 | | register resources from. You may also register resources here. 27 | | Just write the simple directory name that is exists in the module directory. 28 | | 29 | */ 30 | 31 | 'resources' => [ 32 | 'namespace' => '\\Resources', 33 | 'path' => '/Resources', 34 | ], 35 | 36 | /* 37 | |-------------------------------------------------------------------------- 38 | | Widgets 39 | |-------------------------------------------------------------------------- 40 | | 41 | | This is the namespace and directory that Filament will automatically 42 | | register dashboard widgets from. You may also register widgets here. 43 | | Just write the simple directory name that is exists in the module directory. 44 | | 45 | */ 46 | 47 | 'widgets' => [ 48 | 'namespace' => '\\Widgets', 49 | 'path' => '/Widgets', 50 | ], 51 | 52 | /* 53 | |-------------------------------------------------------------------------- 54 | | Livewire 55 | |-------------------------------------------------------------------------- 56 | | 57 | | This is the namespace and directory that Filament will automatically 58 | | register Livewire components inside. 59 | | Just write the simple directory name that is exists in the module directory. 60 | | Other directories like Resources, Pages and Widgets use this as the base 61 | | directory. 62 | | **** It will be the parent directory **** 63 | | for e.g.: imagine u have User module, so the paths will be as mentioned below: 64 | | Modules > User > Admin 65 | | Modules > User > Admin > Resources 66 | | Modules > User > Admin > Pages 67 | | Modules > User > Admin > Widget 68 | | 69 | */ 70 | 71 | 'livewire' => [ 72 | 'namespace' => '\\Admin', 73 | 'path' => '/Admin', 74 | ], 75 | 76 | /* 77 | |-------------------------------------------------------------------------- 78 | | Modules 79 | |-------------------------------------------------------------------------- 80 | | 81 | | This is the namespace of your modules directory that Filament use 82 | | to register your resources and other things inside it. 83 | | 84 | */ 85 | 86 | 'modules' => [ 87 | 'namespace' => config('modules.namespace', 'Modules'), 88 | ], 89 | 90 | /* 91 | |-------------------------------------------------------------------------- 92 | | Views 93 | |-------------------------------------------------------------------------- 94 | | 95 | | This is the views config for the modular support of the filament package. 96 | | Use it once you installed this package and never change it after creating 97 | | resources and other things. 98 | | Making changes in existing projects may lead to unexpected errors. 99 | | 100 | | Set 'in_module' to true if you want to get your views inside the module 101 | | directory, otherwise; set it 'false' and set the 'path' to where you want 102 | | to your views will be stored. 103 | | 104 | | In case of using custom paths, you MUST provide a namespace for views that 105 | | is used to find views in your specified path by laravel. 106 | | 107 | */ 108 | 109 | 'views' => [ 110 | 'in_module' => true, 111 | 'path' => null, 112 | 'namespace' => null, 113 | ], 114 | ]; 115 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | ./tests/Feature 10 | 11 | 12 | 13 | 14 | ./src 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /snap-transparent.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RealMrHex/filament-modular/439e13a522255f1e79902a003bc35bf6607b4078/snap-transparent.png -------------------------------------------------------------------------------- /snap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RealMrHex/filament-modular/439e13a522255f1e79902a003bc35bf6607b4078/snap.png -------------------------------------------------------------------------------- /src/Commands/Aliases/ModuleMakeBelongsToManyCommand.php: -------------------------------------------------------------------------------- 1 | argument($arg) ?? $this->askRequired($ask, $arg); 15 | } 16 | 17 | /** 18 | * Ensure option. 19 | */ 20 | private function ensureOption(string $option, string $ask): ?string 21 | { 22 | return $this->option($option) ?? $this->ask($ask); 23 | } 24 | 25 | /** 26 | * Simplify String. 27 | */ 28 | private function simplifyStr(string $string): string 29 | { 30 | return Str::of($string) 31 | ->trim('/') 32 | ->trim('\\') 33 | ->trim(' ') 34 | ->replace('/', '\\') 35 | ->toString(); 36 | } 37 | 38 | /** 39 | * Simplify resource name. 40 | */ 41 | private function simplifyResource(string $string): string 42 | { 43 | $_ = Str::of($string) 44 | ->studly() 45 | ->trim('/') 46 | ->trim('\\') 47 | ->trim(' ') 48 | ->replace('/', '\\') 49 | ->toString(); 50 | 51 | return Str::of($_)->endsWith('Resource') ? $_ : "{$_}Resource"; 52 | } 53 | 54 | /** 55 | * Get class name from string. 56 | */ 57 | private function classFromStr(string $string): string 58 | { 59 | return Str::of($string)->afterLast('\\')->toString(); 60 | } 61 | 62 | /** 63 | * Get namespace from string. 64 | */ 65 | private function namespaceFromStr(string $string): string 66 | { 67 | return Str::of($string)->contains('\\') 68 | ? Str::of($string)->beforeLast('\\')->toString() 69 | : ''; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/Commands/ModuleMakeBelongsToManyCommand.php: -------------------------------------------------------------------------------- 1 | argument('name') ?? $this->askRequired('Name (e.g. `Settings`)', 'name')) 27 | ->trim('/') 28 | ->trim('\\') 29 | ->trim(' ') 30 | ->replace('/', '\\'); 31 | 32 | $module_name = $this->ensureArg('module', 'Module Name (e.g. `User`)'); 33 | $module_name = Str::of($module_name)->studly()->toString(); 34 | $module = null; 35 | 36 | $base = config('filament-modular.livewire.path'); 37 | 38 | try { 39 | /** 40 | * @var Module $module 41 | */ 42 | $module = app('modules')->findOrFail($module_name); 43 | } catch (\Throwable $exception) { 44 | $this->error('module not found'); 45 | 46 | return static::INVALID; 47 | } 48 | 49 | $_directory_format = '%s/'.Str::replaceFirst('/', '', config('filament-modular.livewire.path')); 50 | $_namespace_format = '%s\\%s\\'.Str::replaceFirst('\\', '', config('filament-modular.livewire.namespace')); 51 | $_module_namespace = config('filament-modular.modules.namespace'); 52 | 53 | $module_path = $module->getPath(); 54 | $module_name = $module->getName(); 55 | 56 | $module_directory = sprintf($_directory_format, $module_path); 57 | $module_namespace = sprintf($_namespace_format, $_module_namespace, $module_name); 58 | 59 | $_widgets_format = '%s/'.Str::replaceFirst('/', '', config('filament-modular.widgets.path')); 60 | $_resources_format = '%s/'.Str::replaceFirst('/', '', config('filament-modular.resources.path')); 61 | $_pages_format = '%s/'.Str::replaceFirst('/', '', config('filament-modular.pages.path')); 62 | 63 | $widgets_path = sprintf($_widgets_format, $module_directory); 64 | $resources_path = sprintf($_resources_format, $module_directory); 65 | $pages_path = sprintf($_pages_format, $module_directory); 66 | 67 | $_widgets_namespace_format = '%s\\'.Str::replaceFirst('\\', '', config('filament-modular.widgets.namespace')); 68 | $_resources_namespace_format = '%s\\'.Str::replaceFirst('\\', '', config('filament-modular.resources.namespace')); 69 | $_pages_namespace_format = '%s\\'.Str::replaceFirst('\\', '', config('filament-modular.pages.namespace')); 70 | 71 | $widgets_namespace = sprintf($_widgets_namespace_format, $module_namespace); 72 | $resources_namespace = sprintf($_resources_namespace_format, $module_namespace); 73 | $pages_namespace = sprintf($_pages_namespace_format, $module_namespace); 74 | 75 | $path = $pages_path; 76 | $resourcePath = $resources_path; 77 | $namespace = $pages_namespace; 78 | $resourceNamespace = $resources_namespace; 79 | $views_namespace = config('filament-modular.views.namespace'); 80 | 81 | $pageClass = (string) Str::of($page)->afterLast('\\'); 82 | $pageNamespace = Str::of($page)->contains('\\') 83 | ? 84 | (string) Str::of($page)->beforeLast('\\') 85 | : 86 | ''; 87 | 88 | $resource = null; 89 | $resourceClass = null; 90 | $resourcePage = null; 91 | 92 | $resourceInput = $this->option('resource') ?? $this->ask('(Optional) Resource (e.g. `UserResource`)'); 93 | 94 | if (null !== $resourceInput) { 95 | $resource = (string) Str::of($resourceInput) 96 | ->studly() 97 | ->trim('/') 98 | ->trim('\\') 99 | ->trim(' ') 100 | ->replace('/', '\\'); 101 | 102 | if (!Str::of($resource)->endsWith('Resource')) { 103 | $resource .= 'Resource'; 104 | } 105 | 106 | $resourceClass = (string) Str::of($resource) 107 | ->afterLast('\\'); 108 | 109 | $resourcePage = $this->option('type') ?? $this->choice( 110 | 'Which type of page would you like to create?', 111 | [ 112 | 'custom' => 'Custom', 113 | 'ListRecords' => 'List', 114 | 'CreateRecord' => 'Create', 115 | 'EditRecord' => 'Edit', 116 | 'ViewRecord' => 'View', 117 | 'ManageRecords' => 'Manage', 118 | ], 119 | 'custom', 120 | ); 121 | } 122 | 123 | $view = Str::of($page) 124 | ->prepend( 125 | (string) Str::of(null === $resource ? "{$namespace}\\" : "{$resourceNamespace}\\{$resource}\\pages\\") 126 | ->replace('App\\', ''), 127 | ) 128 | ->replace('\\', '/') 129 | ->replace('Modules/', '') 130 | ->explode('/') 131 | ->map(fn ($segment) => Str::lower(Str::kebab($segment))) 132 | ->implode('.'); 133 | 134 | $path = (string) Str::of($page) 135 | ->prepend('/') 136 | ->prepend(null === $resource ? $path : "{$resourcePath}\\{$resource}\\Pages\\") 137 | ->replace('\\', '/') 138 | ->replace('//', '/') 139 | ->append('.php'); 140 | 141 | $theme_dir = config('filament-modular.views.path'); 142 | $in_module = config('filament-modular.views.in_module'); 143 | 144 | if ($in_module) { 145 | $kebab = explode('.', $view)[0]; 146 | 147 | $viewPath = Str::of($view) 148 | ->replace('.', '/') 149 | ->replace("$kebab/", '') 150 | ->prepend($module_path.'/Resources/views/') 151 | ->append('.blade.php') 152 | ->replace('modules/', '') 153 | ->toString(); 154 | 155 | $view = Str::of($view)->replace("$kebab.", '')->toString(); 156 | $namespaced_view = "$kebab::$view"; 157 | } else { 158 | $viewPath = Str::of($view) 159 | ->replace('.', '/') 160 | ->prepend($theme_dir) 161 | ->append('.blade.php') 162 | ->replace('modules/', '') 163 | ->toString(); 164 | 165 | $namespaced_view = "$views_namespace::$view"; 166 | } 167 | 168 | $files = array_merge( 169 | [$path], 170 | 'custom' === $resourcePage ? [$viewPath] : [], 171 | ); 172 | 173 | if (!$this->option('force') && $this->checkForCollision($files)) { 174 | return static::INVALID; 175 | } 176 | 177 | if (null === $resource) { 178 | $this->copyStubToApp('Page', $path, [ 179 | 'class' => $pageClass, 180 | 'namespace' => $namespace.('' !== $pageNamespace ? "\\{$pageNamespace}" : ''), 181 | 'view' => $namespaced_view, 182 | ]); 183 | } else { 184 | $this->copyStubToApp('custom' === $resourcePage ? 'CustomResourcePage' : 'ResourcePage', $path, [ 185 | 'baseResourcePage' => 'Filament\\Resources\\Pages\\'.('custom' === $resourcePage ? 'Page' : $resourcePage), 186 | 'baseResourcePageClass' => 'custom' === $resourcePage ? 'Page' : $resourcePage, 187 | 'namespace' => "{$resourceNamespace}\\{$resource}\\Pages".('' !== $pageNamespace ? "\\{$pageNamespace}" : ''), 188 | 'resource' => "{$resourceNamespace}\\{$resource}", 189 | 'resourceClass' => $resourceClass, 190 | 'resourcePageClass' => $pageClass, 191 | 'view' => $namespaced_view, 192 | ]); 193 | } 194 | 195 | if (null === $resource || 'custom' === $resourcePage) { 196 | $this->copyStubToApp('PageView', $viewPath); 197 | } 198 | 199 | $this->info("Successfully created {$page}!"); 200 | 201 | if (null !== $resource) { 202 | $this->info("Make sure to register the page in `{$resourceClass}::getPages()`."); 203 | } 204 | 205 | return static::SUCCESS; 206 | } 207 | } 208 | -------------------------------------------------------------------------------- /src/Commands/ModuleMakeRelationManagerCommand.php: -------------------------------------------------------------------------------- 1 | ensureArg('module', 'Module Name (e.g. `User`)'); 29 | $module_name = Str::of($module_name)->studly()->toString(); 30 | $module = null; 31 | 32 | $base = config('filament-modular.livewire.path'); 33 | 34 | try { 35 | /** 36 | * @var Module $module 37 | */ 38 | $module = app('modules')->findOrFail($module_name); 39 | } catch (Throwable $exception) { 40 | $this->error('module not found'); 41 | 42 | return static::INVALID; 43 | } 44 | 45 | $_directory_format = '%s/'.Str::replaceFirst('/', '', config('filament-modular.livewire.path')); 46 | $_namespace_format = '%s\\%s\\'.Str::replaceFirst('\\', '', config('filament-modular.livewire.namespace')); 47 | $_module_namespace = config('filament-modular.modules.namespace'); 48 | 49 | $module_path = $module->getPath(); 50 | $module_name = $module->getName(); 51 | 52 | $module_directory = sprintf($_directory_format, $module_path); 53 | $module_namespace = sprintf($_namespace_format, $_module_namespace, $module_name); 54 | 55 | $_widgets_format = '%s/'.Str::replaceFirst('/', '', config('filament-modular.widgets.path')); 56 | $_resources_format = '%s/'.Str::replaceFirst('/', '', config('filament-modular.resources.path')); 57 | $_pages_format = '%s/'.Str::replaceFirst('/', '', config('filament-modular.pages.path')); 58 | 59 | $widgets_path = sprintf($_widgets_format, $module_directory); 60 | $resources_path = sprintf($_resources_format, $module_directory); 61 | $pages_path = sprintf($_pages_format, $module_directory); 62 | 63 | $_widgets_namespace_format = '%s\\'.Str::replaceFirst('\\', '', config('filament-modular.widgets.namespace')); 64 | $_resources_namespace_format = '%s\\'.Str::replaceFirst('\\', '', config('filament-modular.resources.namespace')); 65 | $_pages_namespace_format = '%s\\'.Str::replaceFirst('\\', '', config('filament-modular.pages.namespace')); 66 | 67 | $widgets_namespace = sprintf($_widgets_namespace_format, $module_namespace); 68 | $resources_namespace = sprintf($_resources_namespace_format, $module_namespace); 69 | $pages_namespace = sprintf($_pages_namespace_format, $module_namespace); 70 | 71 | $path = $resources_path; 72 | $namespace = $resources_namespace; 73 | 74 | $resource = (string) Str::of($this->argument('resource') ?? $this->askRequired('Resource (e.g. `DepartmentResource`)', 'resource')) 75 | ->studly() 76 | ->trim('/') 77 | ->trim('\\') 78 | ->trim(' ') 79 | ->replace('/', '\\'); 80 | 81 | if (!Str::of($resource)->endsWith('Resource')) { 82 | $resource .= 'Resource'; 83 | } 84 | 85 | $relationship = (string) Str::of($this->argument('relationship') ?? $this->askRequired('Relationship (e.g. `members`)', 'relationship')) 86 | ->trim(' '); 87 | $managerClass = (string) Str::of($relationship) 88 | ->studly() 89 | ->append('RelationManager'); 90 | 91 | $recordTitleAttribute = (string) Str::of($this->argument('recordTitleAttribute') ?? $this->askRequired('Title attribute (e.g. `name`)', 'title attribute')) 92 | ->trim(' '); 93 | 94 | $resourcePath = $resources_path; 95 | 96 | $path = (string) Str::of($managerClass) 97 | ->prepend("{$resourcePath}/{$resource}/RelationManagers/") 98 | ->replace('\\', '/') 99 | ->append('.php'); 100 | 101 | if (!$this->option('force') && $this->checkForCollision([ 102 | $path, 103 | ])) { 104 | return static::INVALID; 105 | } 106 | 107 | $tableHeaderActions = []; 108 | 109 | $tableHeaderActions[] = 'Tables\Actions\CreateAction::make(),'; 110 | 111 | if ($this->option('associate')) { 112 | $tableHeaderActions[] = 'Tables\Actions\AssociateAction::make(),'; 113 | } 114 | 115 | if ($this->option('attach')) { 116 | $tableHeaderActions[] = 'Tables\Actions\AttachAction::make(),'; 117 | } 118 | 119 | $tableHeaderActions = implode(PHP_EOL, $tableHeaderActions); 120 | 121 | $tableActions = []; 122 | 123 | if ($this->option('view')) { 124 | $tableActions[] = 'Tables\Actions\ViewAction::make(),'; 125 | } 126 | 127 | $tableActions[] = 'Tables\Actions\EditAction::make(),'; 128 | 129 | if ($this->option('associate')) { 130 | $tableActions[] = 'Tables\Actions\DissociateAction::make(),'; 131 | } 132 | 133 | if ($this->option('attach')) { 134 | $tableActions[] = 'Tables\Actions\DetachAction::make(),'; 135 | } 136 | 137 | $tableActions[] = 'Tables\Actions\DeleteAction::make(),'; 138 | 139 | if ($this->option('soft-deletes')) { 140 | $tableActions[] = 'Tables\Actions\ForceDeleteAction::make(),'; 141 | $tableActions[] = 'Tables\Actions\RestoreAction::make(),'; 142 | } 143 | 144 | $tableActions = implode(PHP_EOL, $tableActions); 145 | 146 | $tableBulkActions = []; 147 | 148 | if ($this->option('associate')) { 149 | $tableBulkActions[] = 'Tables\Actions\DissociateBulkAction::make(),'; 150 | } 151 | 152 | if ($this->option('attach')) { 153 | $tableBulkActions[] = 'Tables\Actions\DetachBulkAction::make(),'; 154 | } 155 | 156 | $tableBulkActions[] = 'Tables\Actions\DeleteBulkAction::make(),'; 157 | 158 | $eloquentQuery = ''; 159 | 160 | if ($this->option('soft-deletes')) { 161 | $tableBulkActions[] = 'Tables\Actions\RestoreBulkAction::make(),'; 162 | $tableBulkActions[] = 'Tables\Actions\ForceDeleteBulkAction::make(),'; 163 | 164 | $eloquentQuery .= PHP_EOL.PHP_EOL.'protected function getTableQuery(): Builder'; 165 | $eloquentQuery .= PHP_EOL.'{'; 166 | $eloquentQuery .= PHP_EOL.' return parent::getTableQuery()'; 167 | $eloquentQuery .= PHP_EOL.' ->withoutGlobalScopes(['; 168 | $eloquentQuery .= PHP_EOL.' SoftDeletingScope::class,'; 169 | $eloquentQuery .= PHP_EOL.' ]);'; 170 | $eloquentQuery .= PHP_EOL.'}'; 171 | } 172 | 173 | $tableBulkActions = implode(PHP_EOL, $tableBulkActions); 174 | 175 | $this->copyStubToApp('RelationManager', $path, [ 176 | 'eloquentQuery' => $this->indentString($eloquentQuery, 1), 177 | 'namespace' => "{$resources_namespace}\\{$resource}\\RelationManagers", 178 | 'managerClass' => $managerClass, 179 | 'recordTitleAttribute' => $recordTitleAttribute, 180 | 'relationship' => $relationship, 181 | 'tableActions' => $this->indentString($tableActions, 4), 182 | 'tableBulkActions' => $this->indentString($tableBulkActions, 4), 183 | 'tableFilters' => $this->indentString( 184 | $this->option('soft-deletes') ? 'Tables\Filters\TrashedFilter::make()' : '//', 185 | 4, 186 | ), 187 | 'tableHeaderActions' => $this->indentString($tableHeaderActions, 4), 188 | ]); 189 | 190 | $this->info("Successfully created {$managerClass}!"); 191 | 192 | $this->info("Make sure to register the relation in `{$resource}::getRelations()`."); 193 | 194 | return static::SUCCESS; 195 | } 196 | } 197 | -------------------------------------------------------------------------------- /src/Commands/ModuleMakeResourceCommand.php: -------------------------------------------------------------------------------- 1 | ensureArg('module', 'Module Name (e.g. `User`)'); 35 | $module_name = Str::of($module_name)->studly()->toString(); 36 | $module = null; 37 | 38 | $base = config('filament-modular.livewire.path'); 39 | 40 | try { 41 | /** 42 | * @var Module $module 43 | */ 44 | $module = app('modules')->findOrFail($module_name); 45 | } catch (\Throwable $exception) { 46 | $this->error('module not found'); 47 | 48 | return static::INVALID; 49 | } 50 | 51 | $_directory_format = '%s/'.Str::replaceFirst('/', '', config('filament-modular.livewire.path')); 52 | $_namespace_format = '%s\\%s\\'.Str::replaceFirst('\\', '', config('filament-modular.livewire.namespace')); 53 | $_module_namespace = config('filament-modular.modules.namespace'); 54 | 55 | $module_path = $module->getPath(); 56 | $module_name = $module->getName(); 57 | 58 | $module_directory = sprintf($_directory_format, $module_path); 59 | $module_namespace = sprintf($_namespace_format, $_module_namespace, $module_name); 60 | 61 | $_widgets_format = '%s/'.Str::replaceFirst('/', '', config('filament-modular.widgets.path')); 62 | $_resources_format = '%s/'.Str::replaceFirst('/', '', config('filament-modular.resources.path')); 63 | $_pages_format = '%s/'.Str::replaceFirst('/', '', config('filament-modular.pages.path')); 64 | 65 | $widgets_path = sprintf($_widgets_format, $module_directory); 66 | $resources_path = sprintf($_resources_format, $module_directory); 67 | $pages_path = sprintf($_pages_format, $module_directory); 68 | 69 | $_widgets_namespace_format = '%s\\'.Str::replaceFirst('\\', '', config('filament-modular.widgets.namespace')); 70 | $_resources_namespace_format = '%s\\'.Str::replaceFirst('\\', '', config('filament-modular.resources.namespace')); 71 | $_pages_namespace_format = '%s\\'.Str::replaceFirst('\\', '', config('filament-modular.pages.namespace')); 72 | 73 | $widgets_namespace = sprintf($_widgets_namespace_format, $module_namespace); 74 | $resources_namespace = sprintf($_resources_namespace_format, $module_namespace); 75 | $pages_namespace = sprintf($_pages_namespace_format, $module_namespace); 76 | 77 | $path = $resources_path; 78 | $namespace = $resources_namespace; 79 | 80 | $model = (string) Str::of($this->argument('name') ?? $this->askRequired('Model (e.g. `BlogPost`)', 'name')) 81 | ->studly() 82 | ->beforeLast('Resource') 83 | ->trim('/') 84 | ->trim('\\') 85 | ->trim(' ') 86 | ->studly() 87 | ->replace('/', '\\'); 88 | 89 | if (blank($model)) { 90 | $model = 'Resource'; 91 | } 92 | 93 | $modelClass = (string) Str::of($model)->afterLast('\\'); 94 | $modelNamespace = Str::of($model)->contains('\\') ? 95 | (string) Str::of($model)->beforeLast('\\') : 96 | ''; 97 | $pluralModelClass = (string) Str::of($modelClass)->pluralStudly(); 98 | 99 | $resource = "{$model}Resource"; 100 | $resourceClass = "{$modelClass}Resource"; 101 | $resourceNamespace = $modelNamespace; 102 | $namespace .= '' !== $resourceNamespace ? "\\{$resourceNamespace}" : ''; 103 | $listResourcePageClass = "List{$pluralModelClass}"; 104 | $manageResourcePageClass = "Manage{$pluralModelClass}"; 105 | $createResourcePageClass = "Create{$modelClass}"; 106 | $editResourcePageClass = "Edit{$modelClass}"; 107 | $viewResourcePageClass = "View{$modelClass}"; 108 | 109 | $baseResourcePath = 110 | (string) Str::of($resource) 111 | ->prepend('/') 112 | ->prepend($path) 113 | ->replace('\\', '/') 114 | ->replace('//', '/'); 115 | 116 | $resourcePath = "{$baseResourcePath}.php"; 117 | $resourcePagesDirectory = "{$baseResourcePath}/Pages"; 118 | $listResourcePagePath = "{$resourcePagesDirectory}/{$listResourcePageClass}.php"; 119 | $manageResourcePagePath = "{$resourcePagesDirectory}/{$manageResourcePageClass}.php"; 120 | $createResourcePagePath = "{$resourcePagesDirectory}/{$createResourcePageClass}.php"; 121 | $editResourcePagePath = "{$resourcePagesDirectory}/{$editResourcePageClass}.php"; 122 | $viewResourcePagePath = "{$resourcePagesDirectory}/{$viewResourcePageClass}.php"; 123 | 124 | if (!$this->option('force') && $this->checkForCollision([ 125 | $resourcePath, 126 | $listResourcePagePath, 127 | $manageResourcePagePath, 128 | $createResourcePagePath, 129 | $editResourcePagePath, 130 | $viewResourcePagePath, 131 | ])) { 132 | return static::INVALID; 133 | } 134 | 135 | $pages = ''; 136 | $pages .= '\'index\' => Pages\\'.($this->option('simple') ? $manageResourcePageClass : $listResourcePageClass).'::route(\'/\'),'; 137 | 138 | if (!$this->option('simple')) { 139 | $pages .= PHP_EOL."'create' => Pages\\{$createResourcePageClass}::route('/create'),"; 140 | 141 | if ($this->option('view')) { 142 | $pages .= PHP_EOL."'view' => Pages\\{$viewResourcePageClass}::route('/{record}'),"; 143 | } 144 | 145 | $pages .= PHP_EOL."'edit' => Pages\\{$editResourcePageClass}::route('/{record}/edit'),"; 146 | } 147 | 148 | $tableActions = []; 149 | 150 | if ($this->option('view')) { 151 | $tableActions[] = 'Tables\Actions\ViewAction::make(),'; 152 | } 153 | 154 | $tableActions[] = 'Tables\Actions\EditAction::make(),'; 155 | 156 | $relations = ''; 157 | 158 | if ($this->option('simple')) { 159 | $tableActions[] = 'Tables\Actions\DeleteAction::make(),'; 160 | 161 | if ($this->option('soft-deletes')) { 162 | $tableActions[] = 'Tables\Actions\ForceDeleteAction::make(),'; 163 | $tableActions[] = 'Tables\Actions\RestoreAction::make(),'; 164 | } 165 | } else { 166 | $relations .= PHP_EOL.'public static function getRelations(): array'; 167 | $relations .= PHP_EOL.'{'; 168 | $relations .= PHP_EOL.' return ['; 169 | $relations .= PHP_EOL.' //'; 170 | $relations .= PHP_EOL.' ];'; 171 | $relations .= PHP_EOL.'}'.PHP_EOL; 172 | } 173 | 174 | $tableActions = implode(PHP_EOL, $tableActions); 175 | 176 | $tableBulkActions = []; 177 | 178 | $tableBulkActions[] = 'Tables\Actions\DeleteBulkAction::make(),'; 179 | 180 | $eloquentQuery = ''; 181 | 182 | if ($this->option('soft-deletes')) { 183 | $tableBulkActions[] = 'Tables\Actions\ForceDeleteBulkAction::make(),'; 184 | $tableBulkActions[] = 'Tables\Actions\RestoreBulkAction::make(),'; 185 | 186 | $eloquentQuery .= PHP_EOL.PHP_EOL.'public static function getEloquentQuery(): Builder'; 187 | $eloquentQuery .= PHP_EOL.'{'; 188 | $eloquentQuery .= PHP_EOL.' return parent::getEloquentQuery()'; 189 | $eloquentQuery .= PHP_EOL.' ->withoutGlobalScopes(['; 190 | $eloquentQuery .= PHP_EOL.' SoftDeletingScope::class,'; 191 | $eloquentQuery .= PHP_EOL.' ]);'; 192 | $eloquentQuery .= PHP_EOL.'}'; 193 | } 194 | 195 | $tableBulkActions = implode(PHP_EOL, $tableBulkActions); 196 | 197 | $this->copyStubToApp('Resource', $resourcePath, [ 198 | 'eloquentQuery' => $this->indentString($eloquentQuery, 1), 199 | 'formSchema' => $this->indentString($this->option('generate') ? $this->getResourceFormSchema( 200 | 'App\\Models'.('' !== $modelNamespace ? "\\{$modelNamespace}" : '').'\\'.$modelClass, 201 | ) : '//', 4), 202 | 'model' => 'Resource' === $model ? 'Resource as ResourceModel' : $model, 203 | 'modelClass' => 'Resource' === $model ? 'ResourceModel' : $modelClass, 204 | 'namespace' => $namespace, 205 | 'pages' => $this->indentString($pages, 3), 206 | 'relations' => $this->indentString($relations, 1), 207 | 'resource' => "{$namespace}\\{$resourceClass}", 208 | 'resourceClass' => $resourceClass, 209 | 'tableActions' => $this->indentString($tableActions, 4), 210 | 'tableBulkActions' => $this->indentString($tableBulkActions, 4), 211 | 'tableColumns' => $this->indentString($this->option('generate') ? $this->getResourceTableColumns( 212 | 'App\Models'.('' !== $modelNamespace ? "\\{$modelNamespace}" : '').'\\'.$modelClass, 213 | ) : '//', 4), 214 | 'tableFilters' => $this->indentString( 215 | $this->option('soft-deletes') ? 'Tables\Filters\TrashedFilter::make(),' : '//', 216 | 4, 217 | ), 218 | ]); 219 | 220 | if ($this->option('simple')) { 221 | $this->copyStubToApp('ResourceManagePage', $manageResourcePagePath, [ 222 | 'namespace' => "{$namespace}\\{$resourceClass}\\Pages", 223 | 'resource' => "{$namespace}\\{$resourceClass}", 224 | 'resourceClass' => $resourceClass, 225 | 'resourcePageClass' => $manageResourcePageClass, 226 | ]); 227 | } else { 228 | $this->copyStubToApp('ResourceListPage', $listResourcePagePath, [ 229 | 'namespace' => "{$namespace}\\{$resourceClass}\\Pages", 230 | 'resource' => "{$namespace}\\{$resourceClass}", 231 | 'resourceClass' => $resourceClass, 232 | 'resourcePageClass' => $listResourcePageClass, 233 | ]); 234 | 235 | $this->copyStubToApp('ResourcePage', $createResourcePagePath, [ 236 | 'baseResourcePage' => 'Filament\\Resources\\Pages\\CreateRecord', 237 | 'baseResourcePageClass' => 'CreateRecord', 238 | 'namespace' => "{$namespace}\\{$resourceClass}\\Pages", 239 | 'resource' => "{$namespace}\\{$resourceClass}", 240 | 'resourceClass' => $resourceClass, 241 | 'resourcePageClass' => $createResourcePageClass, 242 | ]); 243 | 244 | $editPageActions = []; 245 | 246 | if ($this->option('view')) { 247 | $this->copyStubToApp('ResourceViewPage', $viewResourcePagePath, [ 248 | 'namespace' => "{$namespace}\\{$resourceClass}\\Pages", 249 | 'resource' => "{$namespace}\\{$resourceClass}", 250 | 'resourceClass' => $resourceClass, 251 | 'resourcePageClass' => $viewResourcePageClass, 252 | ]); 253 | 254 | $editPageActions[] = 'Actions\ViewAction::make(),'; 255 | } 256 | 257 | $editPageActions[] = 'Actions\DeleteAction::make(),'; 258 | 259 | if ($this->option('soft-deletes')) { 260 | $editPageActions[] = 'Actions\ForceDeleteAction::make(),'; 261 | $editPageActions[] = 'Actions\RestoreAction::make(),'; 262 | } 263 | 264 | $editPageActions = implode(PHP_EOL, $editPageActions); 265 | 266 | $this->copyStubToApp('ResourceEditPage', $editResourcePagePath, [ 267 | 'actions' => $this->indentString($editPageActions, 3), 268 | 'namespace' => "{$namespace}\\{$resourceClass}\\Pages", 269 | 'resource' => "{$namespace}\\{$resourceClass}", 270 | 'resourceClass' => $resourceClass, 271 | 'resourcePageClass' => $editResourcePageClass, 272 | ]); 273 | } 274 | 275 | $this->info("Successfully created {$resource}!"); 276 | 277 | return static::SUCCESS; 278 | } 279 | } 280 | -------------------------------------------------------------------------------- /src/Commands/ModuleMakeWidgetCommand.php: -------------------------------------------------------------------------------- 1 | ensureArg('module', 'Module Name (e.g. `User`)'); 27 | $module_name = Str::of($module_name)->studly()->toString(); 28 | $module = null; 29 | 30 | $base = config('filament-modular.livewire.path'); 31 | 32 | try { 33 | /** 34 | * @var Module $module 35 | */ 36 | $module = app('modules')->findOrFail($module_name); 37 | } catch (\Throwable $exception) { 38 | $this->error('module not found'); 39 | 40 | return static::INVALID; 41 | } 42 | 43 | $_directory_format = '%s/'.Str::replaceFirst('/', '', config('filament-modular.livewire.path')); 44 | $_namespace_format = '%s\\%s\\'.Str::replaceFirst('\\', '', config('filament-modular.livewire.namespace')); 45 | $_module_namespace = config('filament-modular.modules.namespace'); 46 | 47 | $module_path = $module->getPath(); 48 | $module_name = $module->getName(); 49 | 50 | $module_directory = sprintf($_directory_format, $module_path); 51 | $module_namespace = sprintf($_namespace_format, $_module_namespace, $module_name); 52 | 53 | $_widgets_format = '%s/'.Str::replaceFirst('/', '', config('filament-modular.widgets.path')); 54 | $_resources_format = '%s/'.Str::replaceFirst('/', '', config('filament-modular.resources.path')); 55 | $_pages_format = '%s/'.Str::replaceFirst('/', '', config('filament-modular.pages.path')); 56 | 57 | $widgets_path = sprintf($_widgets_format, $module_directory); 58 | $resources_path = sprintf($_resources_format, $module_directory); 59 | $pages_path = sprintf($_pages_format, $module_directory); 60 | 61 | $_widgets_namespace_format = '%s\\'.Str::replaceFirst('\\', '', config('filament-modular.widgets.namespace')); 62 | $_resources_namespace_format = '%s\\'.Str::replaceFirst('\\', '', config('filament-modular.resources.namespace')); 63 | $_pages_namespace_format = '%s\\'.Str::replaceFirst('\\', '', config('filament-modular.pages.namespace')); 64 | 65 | $widgets_namespace = sprintf($_widgets_namespace_format, $module_namespace); 66 | $resources_namespace = sprintf($_resources_namespace_format, $module_namespace); 67 | $pages_namespace = sprintf($_pages_namespace_format, $module_namespace); 68 | 69 | $views_namespace = config('filament-modular.views.namespace'); 70 | 71 | $theme_dir = config('filament-modular.views.path'); 72 | $in_module = config('filament-modular.views.in_module'); 73 | 74 | $path = $widgets_path; 75 | $resourcePath = $resources_path; 76 | $namespace = $widgets_namespace; 77 | $resourceNamespace = $resources_namespace; 78 | 79 | $widget = (string) Str::of($this->argument('name') ?? $this->askRequired('Name (e.g. `BlogPostsChart`)', 'name')) 80 | ->trim('/') 81 | ->trim('\\') 82 | ->trim(' ') 83 | ->replace('/', '\\'); 84 | 85 | $widgetClass = (string) Str::of($widget)->afterLast('\\'); 86 | $widgetNamespace = Str::of($widget)->contains('\\') ? 87 | (string) Str::of($widget)->beforeLast('\\') : 88 | ''; 89 | 90 | $resource = null; 91 | $resourceClass = null; 92 | 93 | $resourceInput = $this->option('resource') ?? $this->ask('(Optional) Resource (e.g. `BlogPostResource`)'); 94 | 95 | if (null !== $resourceInput) { 96 | $resource = (string) Str::of($resourceInput) 97 | ->studly() 98 | ->trim('/') 99 | ->trim('\\') 100 | ->trim(' ') 101 | ->replace('/', '\\'); 102 | 103 | if (!Str::of($resource)->endsWith('Resource')) { 104 | $resource .= 'Resource'; 105 | } 106 | 107 | $resourceClass = (string) Str::of($resource) 108 | ->afterLast('\\'); 109 | } 110 | 111 | $view = Str::of($widget)->prepend( 112 | (string) Str::of(null === $resource ? "{$namespace}\\" : "{$resourceNamespace}\\{$resource}\\widgets\\") 113 | ->replace('App\\', '') 114 | ) 115 | ->replace('\\', '/') 116 | ->replace('Modules/', '') 117 | ->explode('/') 118 | ->map(fn ($segment) => Str::lower(Str::kebab($segment))) 119 | ->implode('.'); 120 | 121 | $path = (string) Str::of($widget) 122 | ->prepend('/') 123 | ->prepend(null === $resource ? $path : "{$resourcePath}\\{$resource}\\Widgets\\") 124 | ->replace('\\', '/') 125 | ->replace('//', '/') 126 | ->append('.php'); 127 | 128 | if ($in_module) { 129 | $kebab = explode('.', $view)[0]; 130 | 131 | $viewPath = Str::of($view) 132 | ->replace('.', '/') 133 | ->replace("$kebab/", '') 134 | ->prepend($module_path.'/Resources/views/') 135 | ->append('.blade.php') 136 | ->replace('modules/', '') 137 | ->toString(); 138 | 139 | $view = Str::of($view)->replace("$kebab.", '')->toString(); 140 | $namespaced_view = "$kebab::$view"; 141 | } else { 142 | $viewPath = Str::of($view) 143 | ->replace('.', '/') 144 | ->prepend($theme_dir) 145 | ->append('.blade.php') 146 | ->replace('modules/', '') 147 | ->toString(); 148 | $namespaced_view = "$views_namespace::$view"; 149 | } 150 | 151 | if (!$this->option('force') && $this->checkForCollision([ 152 | $path, 153 | ($this->option('stats-overview') || $this->option('chart')) ?: $viewPath, 154 | ])) { 155 | return static::INVALID; 156 | } 157 | 158 | if ($this->option('chart')) { 159 | $chart = $this->choice( 160 | 'Chart type', 161 | [ 162 | 'Bar chart', 163 | 'Bubble chart', 164 | 'Doughnut chart', 165 | 'Line chart', 166 | 'Pie chart', 167 | 'Polar area chart', 168 | 'Radar chart', 169 | 'Scatter chart', 170 | ], 171 | ); 172 | 173 | $this->copyStubToApp('ChartWidget', $path, [ 174 | 'class' => $widgetClass, 175 | 'namespace' => filled($resource) ? "{$resourceNamespace}\\{$resource}\\Widgets".('' !== $widgetNamespace ? "\\{$widgetNamespace}" : '') : $namespace.('' !== $widgetNamespace ? "\\{$widgetNamespace}" : ''), 176 | 'chart' => Str::studly($chart), 177 | ]); 178 | } elseif ($this->option('table')) { 179 | $this->copyStubToApp('TableWidget', $path, [ 180 | 'class' => $widgetClass, 181 | 'namespace' => filled($resource) ? "{$resourceNamespace}\\{$resource}\\Widgets".('' !== $widgetNamespace ? "\\{$widgetNamespace}" : '') : $namespace.('' !== $widgetNamespace ? "\\{$widgetNamespace}" : ''), 182 | ]); 183 | } elseif ($this->option('stats-overview')) { 184 | $this->copyStubToApp('StatsOverviewWidget', $path, [ 185 | 'class' => $widgetClass, 186 | 'namespace' => filled($resource) ? "{$resourceNamespace}\\{$resource}\\Widgets".('' !== $widgetNamespace ? "\\{$widgetNamespace}" : '') : $namespace.('' !== $widgetNamespace ? "\\{$widgetNamespace}" : ''), 187 | ]); 188 | } else { 189 | $this->copyStubToApp('Widget', $path, [ 190 | 'class' => $widgetClass, 191 | 'namespace' => filled($resource) ? "{$resourceNamespace}\\{$resource}\\Widgets".('' !== $widgetNamespace ? "\\{$widgetNamespace}" : '') : $namespace.('' !== $widgetNamespace ? "\\{$widgetNamespace}" : ''), 192 | 'view' => $namespaced_view, 193 | ]); 194 | 195 | $this->copyStubToApp('WidgetView', $viewPath); 196 | } 197 | 198 | $this->info("Successfully created {$widget}!"); 199 | 200 | if (null !== $resource) { 201 | $this->info("Make sure to register the widget in `{$resourceClass}::getWidgets()`, and then again in `getHeaderWidgets()` or `getFooterWidgets()` of any `{$resourceClass}` page."); 202 | } 203 | 204 | return static::SUCCESS; 205 | } 206 | } 207 | -------------------------------------------------------------------------------- /src/FilamentModularServiceProvider.php: -------------------------------------------------------------------------------- 1 | name('filament-modular') 34 | ->hasConfigFile() 35 | ->hasCommands($this->getCommands()); 36 | 37 | $this->app->booting(fn () => $this->initModules()); 38 | $this->loadViews(); 39 | } 40 | 41 | /** 42 | * Get modular commands. 43 | * 44 | * @return string[] 45 | */ 46 | protected function getCommands(): array 47 | { 48 | $commands = $this->getMakeCommands(); 49 | 50 | $aliases = []; 51 | 52 | foreach ($commands as $command) { 53 | $class = 'RealMrHex\\FilamentModular\\Commands\\Aliases\\'.class_basename($command); 54 | 55 | if (class_exists($class)) { 56 | $aliases[] = $class; 57 | } 58 | } 59 | 60 | return array_merge($commands, $aliases); 61 | } 62 | 63 | /** 64 | * Load Views. 65 | */ 66 | private function loadViews(): void 67 | { 68 | $path = config('filament-modular.views.path'); 69 | $namespace = config('filament-modular.views.namespace'); 70 | $this->loadViewsFrom($path, $namespace); 71 | } 72 | 73 | /** 74 | * Get make:x commands. 75 | * 76 | * @return string[] 77 | */ 78 | private function getMakeCommands(): array 79 | { 80 | return [ 81 | ModuleMakeBelongsToManyCommand::class, 82 | ModuleMakeHasManyCommand::class, 83 | ModuleMakeHasManyThroughCommand::class, 84 | ModuleMakeMorphManyCommand::class, 85 | ModuleMakeMorphToManyCommand::class, 86 | ModuleMakePageCommand::class, 87 | ModuleMakeRelationManagerCommand::class, 88 | ModuleMakeResourceCommand::class, 89 | ModuleMakeWidgetCommand::class, 90 | ]; 91 | } 92 | 93 | /** 94 | * Fetch all active modules and initialize them. 95 | */ 96 | private function initModules(): void 97 | { 98 | /** 99 | * List of all enabled modules. 100 | * 101 | * @var Module[] $modules 102 | */ 103 | $modules = $this->app['modules']->allEnabled(); 104 | 105 | foreach ($modules as $module) { 106 | $this->scanModule($module); 107 | } 108 | } 109 | 110 | /** 111 | * Scan current module for finding pages, widgets, resources, etc. 112 | */ 113 | private function scanModule(Module $module): void 114 | { 115 | /** 116 | * Filesystem instance. 117 | * 118 | * @var Filesystem $filesystem 119 | */ 120 | $filesystem = resolve(Filesystem::class); 121 | 122 | $_directory_format = '%s/'.Str::replaceFirst('/', '', config('filament-modular.livewire.path')); 123 | $_namespace_format = '%s\\%s\\'.Str::replaceFirst('\\', '', config('filament-modular.livewire.namespace')); 124 | $_module_namespace = config('filament-modular.modules.namespace'); 125 | 126 | $module_path = $module->getPath(); 127 | $module_name = $module->getName(); 128 | 129 | $module_directory = sprintf($_directory_format, $module_path); 130 | $module_namespace = sprintf($_namespace_format, $_module_namespace, $module_name); 131 | 132 | $_widgets_format = '%s/'.Str::replaceFirst('/', '', config('filament-modular.widgets.path')); 133 | $_resources_format = '%s/'.Str::replaceFirst('/', '', config('filament-modular.resources.path')); 134 | $_pages_format = '%s/'.Str::replaceFirst('/', '', config('filament-modular.pages.path')); 135 | 136 | $widgets_path = sprintf($_widgets_format, $module_directory); 137 | $resources_path = sprintf($_resources_format, $module_directory); 138 | $pages_path = sprintf($_pages_format, $module_directory); 139 | 140 | // Return if module directory not exists 141 | if (!$filesystem->isDirectory($module_directory)) { 142 | return; 143 | } 144 | 145 | // Loop on directory files 146 | foreach ($filesystem->allFiles($module_directory) as $file) { 147 | // Generate file class 148 | $fileClass = Str::of($module_namespace) 149 | ->append('\\', $file->getRelativePathname()) 150 | ->replace(['/', '.php'], ['\\', '']) 151 | ->toString(); 152 | 153 | // Continue in case of "abstract class" or when class "is not exists" 154 | if (!class_exists($fileClass) || (new \ReflectionClass($fileClass))->isAbstract()) { 155 | continue; 156 | } 157 | 158 | // Get the file path 159 | $filePath = Str::of($module_directory.'/'.$file->getRelativePathname()); 160 | 161 | // Check for Resource instance 162 | if ($filePath->startsWith($resources_path) && is_subclass_of($fileClass, Resource::class)) { 163 | $this->resources[] = $fileClass; 164 | continue; 165 | } 166 | 167 | // Check for Page instance 168 | if ($filePath->startsWith($pages_path) && is_subclass_of($fileClass, Page::class)) { 169 | $this->pages[] = $fileClass; 170 | continue; 171 | } 172 | 173 | // Check for Widget instance 174 | if ($filePath->startsWith($widgets_path) && is_subclass_of($fileClass, Widget::class)) { 175 | $this->widgets[] = $fileClass; 176 | continue; 177 | } 178 | 179 | // Continue on RelationManger subclasses 180 | if (is_subclass_of($fileClass, RelationManager::class)) { 181 | continue; 182 | } 183 | 184 | // Continue if it's not a Livewire Component 185 | if (!is_subclass_of($fileClass, Component::class)) { 186 | continue; 187 | } 188 | 189 | // Generate the Alias 190 | $_livewire_alias = Str::of($fileClass) 191 | ->after($module_namespace.'\\') 192 | ->replace(['/', '\\'], '.') 193 | ->prepend('filament.') 194 | ->explode('.') 195 | ->map([Str::class, 'kebab']) 196 | ->implode('.'); 197 | 198 | // Register livewire component via its alias 199 | $this->livewireComponents[$_livewire_alias] = $fileClass; 200 | } 201 | } 202 | } 203 | -------------------------------------------------------------------------------- /stubs/ChartWidget.stub: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /stubs/RelationManager.stub: -------------------------------------------------------------------------------- 1 | schema([ 23 | Forms\Components\TextInput::make('{{ recordTitleAttribute }}') 24 | ->required() 25 | ->maxLength(255), 26 | ]); 27 | } 28 | 29 | public static function table(Table $table): Table 30 | { 31 | return $table 32 | ->columns([ 33 | Tables\Columns\TextColumn::make('{{ recordTitleAttribute }}'), 34 | ]) 35 | ->filters([ 36 | {{ tableFilters }} 37 | ]) 38 | ->headerActions([ 39 | {{ tableHeaderActions }} 40 | ]) 41 | ->actions([ 42 | {{ tableActions }} 43 | ]) 44 | ->bulkActions([ 45 | {{ tableBulkActions }} 46 | ]); 47 | }{{ eloquentQuery }} 48 | } 49 | -------------------------------------------------------------------------------- /stubs/Resource.stub: -------------------------------------------------------------------------------- 1 | schema([ 26 | {{ formSchema }} 27 | ]); 28 | } 29 | 30 | public static function table(Table $table): Table 31 | { 32 | return $table 33 | ->columns([ 34 | {{ tableColumns }} 35 | ]) 36 | ->filters([ 37 | {{ tableFilters }} 38 | ]) 39 | ->actions([ 40 | {{ tableActions }} 41 | ]) 42 | ->bulkActions([ 43 | {{ tableBulkActions }} 44 | ]); 45 | } 46 | {{ relations }} 47 | public static function getPages(): array 48 | { 49 | return [ 50 | {{ pages }} 51 | ]; 52 | }{{ eloquentQuery }} 53 | } 54 | -------------------------------------------------------------------------------- /stubs/ResourceEditPage.stub: -------------------------------------------------------------------------------- 1 | 2 | 3 | {{-- Widget content --}} 4 | 5 | 6 | -------------------------------------------------------------------------------- /tests/TestCase.php: -------------------------------------------------------------------------------- 1 |