sendy / modularizer
Requires
- php: >=5.3.0
- illuminate/filesystem: 4.*
- illuminate/support: 4.*
Requires (Dev)
- behat/behat: 2.5.*@stable
- behat/mink: v1.5.0
- behat/mink-extension: *
- behat/mink-goutte-driver: *
- behat/mink-selenium2-driver: *
- phpunit/phpunit: 3.*
This package is not auto-updated.
Last update: 2024-09-14 16:39:47 UTC
README
https://github.com/sendyhalim/laravel-modularizer
此包仍处于早期开发阶段,但已可使用且经过测试。
Modularizer 是一个 Laravel 4 插件。基本上,它是一个 artisan 命令,根据给定输入创建模块和自动验证仓库。它受到了这些杰出人士的启发
###安装
将 "sendy/modularizer": "dev-master"
添加到您的 composer.json 中,然后在终端中运行 composer update
。
分别将 Sendy\Modularizer\ModularizerCommandServiceProvider
和 Sendy\Modularizer\ModularizerModulesServiceProvider
添加到 app/config/app.php
服务提供者中。
Sendy\Modularizer\ModularizerCommandServiceProvider
用于注册命令。Sendy\Modularizer\ModularizerModulesServiceProvider
用于注册活动模块(注册模块的路由和视图命名空间)。
最后,在 composer.json 中包含以下内容以进行自动加载(使用默认配置)
"psr-0":{
"Modules": "app/modules"
}
###使用...
首先,您需要发布配置,打开终端并输入
php artisan config:publish sendy/modularizer
它将在 app/config/packages/sendy/modularizer/module.php
内生成 modularizer 配置
在配置中,有
<?php
return [
'base_path' => app_path() . '/modules',
'base_directory' => 'Modules',
'active' => [
],
];
base_path
是您的模块将被创建的路径。base_directory
是 base_path 内的基目录,默认情况下,它将在app/modules/Modules
中创建模块。有了这个,通过使用Modules
作为基命名空间,可以轻松地包含 composer 自动加载命名空间。active
是活动模块的数组,可以将其视为模块注册。
##可用命令
####创建一个模块
php artisan modularizer:create-module <ModuleName>
参数
- 模块名称
选项
--path
要创建的模块的基路径,默认为app/modules
。--basedirectory
模块的基目录,默认为Modules
,使用默认配置,模块将在app/modules/Modules
中创建,基命名空间为Modules
。
####为模块准备 modularizer,为您的模块创建 Core
php artisan modularizer:prepare
选项
--path
要创建的模块的基路径,默认为app/modules
。--basedirectory
模块的基目录,默认为Modules
,使用默认配置,Core
将在app/modules/Modules
中创建,基命名空间为Modules
。
创建仓库
php artisan modularizer:create-repository <ModelName> <ModuleName>
参数
- 模型名称
- 模块名称
选项
--path
要创建的模块的基路径,默认为app/modules
。--basedirectory
模块的基目录,默认为Modules
,使用默认配置,模块将在app/modules/Modules
中创建,基命名空间为Modules
。--basenamespace
基命名空间,默认Modules
。
为模块创建迁移
此命令接受输入并调用 php artisan migrate:make
命令,并将路径修改为模块路径,因此您需要指定模块名称
php artisan modularizer:make-migration <MigrationName> <ModuleName>
参数
- 迁移名称
- 模块名称
选项
--path
迁移文件的路径,使用默认配置,它将在app/modules/ModuleName/database/migrations
中创建。--basedirectory
模块的基目录,默认为Modules
--create
如果迁移是创建新表,则指定此选项。(与 Laravel 迁移选项相同)--table
如果迁移是修改新表,则指定此选项。(与 Laravel 迁移选项相同)
##示例
创建 Admin
模块
php artisan modularizer:create-module Admin
使用默认配置,它将在 app/modules/Modules
中创建 Admin 模块
- Admin
- Controllers
- AdminBaseController.php
- Repositories
- Read
- Write
- RepositoryInterfaces
- Read
- Write
- database
- migrations
- views
- routes.php
Admin 视图已注册,其命名空间为 admin::view-file
,例如,我有一个视图文件 Admin/views/layout.blade.php
,要获取它,您需要使用 admin::layout
Modularizer 还附带自动验证仓库,首先您需要发布 Core
(我喜欢这样称呼它,但您可以根据需要配置任何名称)
php artisan modularizer:prepare
使用默认配置,它将在 app/modules/Modules
中创建 Core
- Core
- Repositories
- Read
- BasicRepositoryReader.php
- Write
- BasicRepositoryWriter.php
- RepositoryInterfaces
- Read
- BasicRepositoryReaderInterface.php
- Write
- BasicRepositoryWriterInterface.php
- Validators
- Interfaces
- ValidatorInterface.php
所有由模块化工具创建的仓库和接口将自动扩展BasicRepository(Reader/Writer),其接口将自动扩展BasicRepository(Reader/Writer)接口
注意 ValidatorInterface.php
,每次我们使用仓库进行保存(创建/更新)时,都需要传递一个实现了ValidatorInterface
的类给仓库。首先,我们为模型User
创建一个验证器
<?php
namespace Modules\Admin\Validators;
use Modules\Core\Validators\Interfaces\ValidatorInterface;
use Validator;
class UserValidator implements ValidatorInterface
{
public function validate(array $input)
{
// do validation here
// if success return true
// else return false
}
}
现在我们需要一个UserRepository,使用模块化工具可以快速完成
php artisan modularizer:create-repository User Admin
上面的命令将在Admin
模块内部为模型User
创建仓库(读取和写入)。将为您创建4个文件
Admin/RepositoryInterfaces/Read/UserRepositoryReaderInterface
Admin/RepositoryInterfaces/Write/UserRepositoryWriterInterface
Admin/Repositories/Read/UserRepositoryReader
Admin/Repositories/Write/UserRepositoryWriter
仓库接口将与用户仓库相关联
App::singleton('Modules\Admin\RepositoryInterfaces\Write\UserRepositoryWriterInterface', 'Modules\Admin\Repositories\Write\UserRepositoryWriter')
App::singleton('Modules\Admin\RepositoryInterfaces\Read\UserRepositoryReaderInterface', 'Modules\Admin\Repositories\Read\UserRepositoryReader');
这样就完成了!我们现在可以使用UserRepository
了
$userValidator = App::make('Modules\Admin\Validators\UserValidator');
$repo = App::make('Modules\Admin\RepositoryInterfaces\Write\UserRepositoryWriterInterface');
if ($repo->create($input, $userValidator))
{
// success
}
else
{
// fail
}