Skip to content

Commit

Permalink
added facades, removed laracasts/formvalidator
Browse files Browse the repository at this point in the history
  • Loading branch information
slider23 committed Aug 22, 2014
1 parent a07de3c commit 8c934de
Show file tree
Hide file tree
Showing 22 changed files with 321 additions and 47 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
composer.phar
composer.lock
.DS_Store
.idea
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ After updating composer, add the ServiceProvider to the providers array in `app/

Run Artisan command:

php artisan modulator --path=app/Acme User
php artisan modulator --path=app/Acme User --template=default

where `Acme` - namespace of your application (must be in `autoload` section of `composer.json`) and `User` - name of module.
where
`Acme` - namespace of your application (must be in `autoload` section of `composer.json`)
`User` - name of module for create.
`default` - folder with files of template, defined in config.php . 'default' is devault value, also available template 'formvalidation' with validator an model presenter of Jeffrey Way (https://github.com/laracasts). `--template` is optional.

In folder `app/Acme` will be created:

![Module structure](https://monosnap.com/image/wTflxvS5IZZdxPTc7DQg7LAvjzY158.png)
![Module structure](https://monosnap.com/image/ZYKQ4udjW08d1T76iyDu7RDMfl0WHt.png)

Add `Acme\User\UserServiceProvider` to the providers array in `app/config/app.php`. Module is ready to work !

Expand All @@ -33,4 +36,14 @@ To change module structure clone config to your app:

php artisan config:publish slider23/laravel-modulator

and change path to folder of template.
and add path to your folder of template to `app/config/packages/slider23/laravel-modulator/config.php`:
```
return array(
'templates_path' =>
array(
'default' => "vendor/slider23/laravel-modulator/src/Slider23/LaravelModulator/templates/default/",
'formvalidation' => "vendor/slider23/laravel-modulator/src/Slider23/LaravelModulator/templates/formvalidation/",
'myowntemplate' => "app/storage/my_module_template/"
)
);
```
18 changes: 16 additions & 2 deletions src/Slider23/LaravelModulator/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Finder\Finder;
use Config;
use Exception;

class RunCommand extends Command {

Expand Down Expand Up @@ -48,12 +50,15 @@ public function fire()
{
$module_name = $this->argument("modulename");
$module_folder_path = $this->option("path");
$template_name = $this->option("template");
if( ! $template_name) $template_name = "default";
$is_debug = $this->option("debug");

if(!$module_name OR !$module_folder_path){
$this->info("Creating new module for your laravel application (app/NamespaceRoot/[Somefolder/]Somemodule).");
if(!$module_folder_path) $module_folder_path = $this->ask('Path to your module folder (for example app/NamespaceRoot/Somefolder , all folders must be exist): ');
if(!$module_name) $module_name = $this->ask("Module name (for example Somemodule): ");
if(!$template_name) $template_name = $this->ask("Module template [default]: ");
}

$module_name_lower = strtolower($module_name);
Expand All @@ -68,8 +73,16 @@ public function fire()
if($is_debug) $this->info("module_base_namespace = $module_base_namespace");


//$templates_path = __DIR__.DIRECTORY_SEPARATOR."template".DIRECTORY_SEPARATOR;
$templates_path = str_replace(array('/','\\'), DIRECTORY_SEPARATOR, $this->laravel['path.base'].DIRECTORY_SEPARATOR.\Config::get("laravel-modulator::config.templates_path"));
$config_template_path = Config::get("laravel-modulator::config.templates_path");
if(is_array($config_template_path)){
if( ! isset($config_template_path[$template_name])){
throw new Exception("Path to template $template_name not found. Check laravel-modulator`s config.php.");
}
$templates_path = $config_template_path[$template_name];
}else{
$templates_path = $config_template_path;
}
$templates_path = str_replace(array('/','\\'), DIRECTORY_SEPARATOR, $this->laravel['path.base'].DIRECTORY_SEPARATOR.$templates_path);
if($is_debug) $this->info("templates_path = $templates_path");

$destination_path = $this->laravel['path.base'].DIRECTORY_SEPARATOR.$module_folder_path.DIRECTORY_SEPARATOR.$module_name.DIRECTORY_SEPARATOR;
Expand Down Expand Up @@ -147,6 +160,7 @@ protected function getOptions()
{
return array(
array('path', null, InputOption::VALUE_OPTIONAL, 'Path to module folder (\'app/Acme\' for example).', null),
array('template', null, InputOption::VALUE_OPTIONAL, "Module template folder (see config.php).", null),
array('debug', 0, InputOption::VALUE_OPTIONAL, 'Debug output.', null),
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,17 @@
<?php namespace {{namespace}}\Controllers;

use {{namespace}}\Forms\{{Modulename}}Form;
use Laracasts\Validation\FormValidationException;
use BaseController;

class {{Modulename}}Controller extends \BaseController {
class {{Modulename}}Controller extends BaseController {

private ${{modulename}}Form;

public function __construct({{Modulename}}Form ${{modulename}}Form)
public function __construct()
{
$this->{{modulename}}Form = ${{modulename}}Form;

}

public function getExample()
{
return \View::make("{{modulename}}/example");
}

public function postExample()
{
$input = \Input::only('field1', 'field2');
try{
$this->{{modulename}}Form->valdate($input);
}
catch (FormValidationException $e)
{
return \Redirect::back()->withInput()->withErrors($e->getErrors());
}

return \Redirect::back()->withSuccess(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php namespace {{namespace}}\Facades;

use Illuminate\Support\Facades\Facade;

class {{Modulename}} extends Facade{

protected static function getFacadeAccessor()
{
return "{{modulename}}facade";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php namespace {{namespace}}\Facades;

class {{Modulename}}FacadeClass {

public function __construct()
{

}

public function example()
{
return "This is string from facade class";
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php namespace {{namespace}}\Models;

use Eloquent;
use Illuminate\Database\Eloquent\SoftDeletingTrait;
use Laracasts\Presenter\PresentableTrait; // https://github.com/laracasts/Presenter

class {{Modulename}} extends \Eloquent {
class {{Modulename}} extends Eloquent {

protected $table = '{{modulenames}}';
protected $primaryKey = 'id';
Expand All @@ -16,9 +16,6 @@ class {{Modulename}} extends \Eloquent {
use SoftDeletingTrait;
protected $dates = ['deleted_at'];

use PresentableTrait;
protected $presenter = '{{namespace}}\Presenters\{{Modulename}}Presenter';

public static function boot()
{
parent::boot();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace {{namespace}};

use Illuminate\Support\ServiceProvider;
use Illuminate\Foundation\AliasLoader;

class {{Modulename}}ServiceProvider extends ServiceProvider {

Expand Down Expand Up @@ -33,7 +34,14 @@ class {{Modulename}}ServiceProvider extends ServiceProvider {
include __DIR__.DIRECTORY_SEPARATOR.'{{modulename}}_events.php';
include __DIR__.DIRECTORY_SEPARATOR.'{{modulename}}_filters.php';

// ...
// Registering facade
$this->app->bind('{{modulename}}facade', function($app){
return new \{{namespace}}\Facades\{{Modulename}}FacadeClass();
});
// Registering alias for facade
$this->app->booting(function(){
AliasLoader::getInstance()->alias('{{Modulename}}', '{{namespace}}\Facades\{{Modulename}}');
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
This is example view for module {{Modulename}}.<br>
Created by laravel-modulator.<br>
<br>
<h4>Example for form validation</h4>
<form action="" method="POST">

Field1: <?php echo Form::text('field1', Input::old('field1')); ?><br>
<?php if($errors->first('field1')) echo '<span style="color:red">'.$errors->first('field1').'</span>'; ?>

Field2: <?php echo Form::text('field2', Input::old('field2')); ?><br>
<?php if($errors->first('field2')) echo '<span style="color:red">'.$errors->first('field2').'</span>'; ?>

<input type="submit">

</form>

<?php if(isset($success)){ echo "Validation passed."; } ?>
Example facade use: <?php echo {{Modulename}}::example() ?>
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ $namespace = '{{namespace}}\Controllers';
*/

Route::get( '{{modulename}}/example', ['uses'=>$namespace.'\{{Modulename}}Controller@getExample', 'as'=>'{{modulename}}.example']);
Route::post('{{modulename}}/example', ['uses'=>$namespace.'\{{Modulename}}Controller@postExample', 'as'=>'{{modulename}}.example.save']);


Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php namespace {{namespace}}\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class {{Modulename}}Command extends Command {

/**
* The console command name.
*
* @var string
*/
protected $name = 'command:name';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description.';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
//
}

/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array(
array('example', InputArgument::REQUIRED, 'An example argument.'),
);
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return array(
array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php namespace {{namespace}}\Controllers;

use {{namespace}}\Forms\{{Modulename}}Form;
use Laracasts\Validation\FormValidationException;

class {{Modulename}}Controller extends \BaseController {

private ${{modulename}}Form;

public function __construct({{Modulename}}Form ${{modulename}}Form)
{
$this->{{modulename}}Form = ${{modulename}}Form;
}

public function getExample()
{
return \View::make("{{modulename}}/example");
}

public function postExample()
{
$input = \Input::only('field1', 'field2');
try{
$this->{{modulename}}Form->valdate($input);
}
catch (FormValidationException $e)
{
return \Redirect::back()->withInput()->withErrors($e->getErrors());
}

return \Redirect::back()->withSuccess(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php namespace {{namespace}}\Models;

use Illuminate\Database\Eloquent\SoftDeletingTrait;
use Laracasts\Presenter\PresentableTrait; // https://github.com/laracasts/Presenter

class {{Modulename}} extends \Eloquent {

protected $table = '{{modulenames}}';
protected $primaryKey = 'id';
public $timestamps = true;

protected $fillable = [];
protected $guarded = [];
protected $hidden = [];

use SoftDeletingTrait;
protected $dates = ['deleted_at'];

use PresentableTrait;
protected $presenter = '{{namespace}}\Presenters\{{Modulename}}Presenter';

public static function boot()
{
parent::boot();
// Setup event bindings...
}

};
Loading

0 comments on commit 8c934de

Please sign in to comment.