sendy/modularizer
Composer 安装命令:
composer require sendy/modularizer
包简介
README 文档
README
https://github.com/sendyhalim/laravel-modularizer
This package is still in early development, but it is usable and already tested.
Modularizer is a Laravel 4 plugin. Basically it is an artisan command that creates module and auto validation repository based on given input. It is inspired by these amazing people:
###Install
Add "sendy/modularizer": "dev-master" to your composer.json then run composer update in your terminal.
Add Sendy\Modularizer\ModularizerCommandServiceProvider and Sendy\Modularizer\ModularizerModulesServiceProvider respectively to app/config/app.php service providers.
Sendy\Modularizer\ModularizerCommandServiceProvideris for registering commands.Sendy\Modularizer\ModularizerModulesServiceProvideris for registering active modules(registers route and view namespace of module).
Last, (with default config)include this to your composer.json for autoloading
"psr-0":{
"Modules": "app/modules"
}
###Using it...
First make you need to publish the config, fire up your terminal and type
php artisan config:publish sendy/modularizer
it will generate modularizer config app/config/packages/sendy/modularizer/module.php
inside the config, there's
<?php
return [
'base_path' => app_path() . '/modules',
'base_directory' => 'Modules',
'active' => [
],
];
base_pathis the path where your module will be created.base_directoryis the base directory inside base path, by default it will make module inapp/modules/Modules. With this, it's easy to include composer autoloading namespace by usingModulesas base namespace.activeis an array of active modules, think of it as module registration.
##Available Commands
####Create a module:
php artisan modularizer:create-module <ModuleName>
arguments
- Module name
options
--pathbase path to modules to be created, default toapp/modules.--basedirectorybase directory of modules, default toModules, with default config modules will be created inapp/modules/Modulesand base namespace will beModules.
####Prepare modularizer, make Core for your modules
php artisan modularizer:prepare
options
--pathbase path to modules to be created, default toapp/modules.--basedirectorybase directory of modules, default toModules, with default configCorewill be created inapp/modules/Modulesand base namespace will beModules.
Create repository
php artisan modularizer:create-repository <ModelName> <ModuleName>
arguments
- Model name
- Module name
options
--pathbase path to modules to be created, default toapp/modules.--basedirectorybase directory of modules, default toModules, with default config modules will be created inapp/modules/Modulesand base namespace will beModules.--basenamespacebase namespace, defaultModules.
Create migration for a module
This command takes input and call php artisan migrate:make command with path edited to module path hence you need to specify your module name
php artisan modularizer:make-migration <MigrationName> <ModuleName>
arguments
- Migration name
- Module name
options
--pathpath for migration file, with default config it will beapp/modules/ModuleName/database/migrations.--basedirectorybase directory of modules, default toModules--createspecify this if the migration is to create new table.(Same as laravel migration option)--tablespecify this if the migration is to modify new table.(Same as laravel migration option)
##Examples
Creating Admin module
php artisan modularizer:create-module Admin
with default config it will create Admin module in app/modules/Modules
- Admin
- Controllers
- AdminBaseController.php
- Repositories
- Read
- Write
- RepositoryInterfaces
- Read
- Write
- database
- migrations
- views
- routes.php
Admin views is registered with its namespace admin::view-file, example I have a view file Admin/views/layout.blade.php, to get it you need to use admin::layout
Modularizer also comes with autovalidation repository, first you need to publish Core(I like to call it so, but you can configure whatever name you like)
php artisan modularizer:prepare
with default config it will create Core in app/modules/Modules
- Core
- Repositories
- Read
- BasicRepositoryReader.php
- Write
- BasicRepositoryWriter.php
- RepositoryInterfaces
- Read
- BasicRepositoryReaderInterface.php
- Write
- BasicRepositoryWriterInterface.php
- Validators
- Interfaces
- ValidatorInterface.php
All repositories and interfaces that are created by modularizer will automatically extend BasicRepository(Reader/Writer) and its interface will automatically extend BasicRepository(Reader/Writer)Interfaces
Notice ValidatorInterface.php, everytime we save(create/update) with a repository, we need to pass a class that implements ValidatorInterface to the repository. First let's make a validator for model 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
}
}
Now we need a UserRepository, with modularizer we can make it fast
php artisan modularizer:create-repository User Admin
the above command will make repository(read and write) for model User inside Admin module. 4 Files will be created for you
Admin/RepositoryInterfaces/Read/UserRepositoryReaderInterfaceAdmin/RepositoryInterfaces/Write/UserRepositoryWriterInterfaceAdmin/Repositories/Read/UserRepositoryReaderAdmin/Repositories/Write/UserRepositoryWriter
The repository interfaces will be tied with user repositories
App::singleton('Modules\Admin\RepositoryInterfaces\Write\UserRepositoryWriterInterface', 'Modules\Admin\Repositories\Write\UserRepositoryWriter')
App::singleton('Modules\Admin\RepositoryInterfaces\Read\UserRepositoryReaderInterface', 'Modules\Admin\Repositories\Read\UserRepositoryReader');
That's it! We can use the UserRepository now
$userValidator = App::make('Modules\Admin\Validators\UserValidator');
$repo = App::make('Modules\Admin\RepositoryInterfaces\Write\UserRepositoryWriterInterface');
if ($repo->create($input, $userValidator))
{
// success
}
else
{
// fail
}
sendy/modularizer 适用场景与选型建议
sendy/modularizer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 84 次下载、GitHub Stars 达 5, 最近一次更新时间为 2014 年 05 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 sendy/modularizer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sendy/modularizer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 84
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2014-05-09