Skip to content

Commit

Permalink
service provider in process, default aliases config moved to factory …
Browse files Browse the repository at this point in the history
…property
  • Loading branch information
dobrik committed Jun 11, 2019
1 parent b552f41 commit 9e62d8c
Show file tree
Hide file tree
Showing 25 changed files with 98 additions and 193 deletions.
9 changes: 9 additions & 0 deletions src/Exceptions/InvalidAliasException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php


namespace Dobrik\LaravelEasyForm\Exceptions;

class InvalidAliasException extends \Exception
{

}
28 changes: 0 additions & 28 deletions src/Form/Inputs/Video.php

This file was deleted.

50 changes: 0 additions & 50 deletions src/Form/Plugins/CkeditorBlockButton.php

This file was deleted.

24 changes: 19 additions & 5 deletions src/Form/Generator.php → src/Forms/Creator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* Class Generator
* @package Dobrik\LaravelEasyForm\Forms
*/
class Generator
class Creator
{
/**
* @var array
Expand All @@ -31,13 +31,27 @@ class Generator
/**
* Generator constructor.
* @param Factory $factory
* @param Request $request
*/
public function __construct(Factory $factory, Request $request)
public function __construct(Factory $factory)
{
$this->factory = $factory;
}

public function registerFormConfig(array $config)
{
$this->config = array_merge($this->config, $config);
return $this;
}

public function setRequest(Request $request)
{
$this->request = $request;
$this->config = config('forms');
return $this;
}

private function getRequest()
{
return $this->request;
}

/**
Expand Down Expand Up @@ -126,7 +140,7 @@ private function prepareField(array $field_data, Model $model = null, string $lo
$field = $this->factory->autoMake($input_name, $field_data['type'], $field_data['title']);

switch (true) {
case ($value = $this->request->old(array_key(Arr::dot($input_name)))) !== null:
case ($value = $this->getRequest()->old(array_key(Arr::dot($input_name)))) !== null:
break;
case $model !== null:
$value = null === $locale ? $model->getAttribute($field_data['name']) : $model->getLocalizedAttribute($locale, $field_data['name']);
Expand Down
39 changes: 35 additions & 4 deletions src/Form/Factory.php → src/Forms/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Dobrik\LaravelEasyForm\Forms;

use Dobrik\LaravelEasyForm\Exceptions\InvalidAliasException;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Dobrik\LaravelEasyForm\Forms\Interfaces\PluginInterface;
Expand All @@ -12,14 +13,44 @@
*/
class Factory
{
private $aliases = [
'plugins' => [
'ckeditor' => Plugins\Ckeditor::class,
'color_picker' => Plugins\ColorPicker::class,
'datetimepicker' => Plugins\Datetimepicker::class,
'select2' => Plugins\Select2::class,
],
'html' => [
'div' => Html\Div::class,
'tab' => Html\Tab::class,
'tabs' => Html\Tabs::class,
],
'inputs' => [
'button' => Inputs\Button::class,
'form' => Inputs\Form::class,
'image' => Inputs\Image::class,
'input' => Inputs\Input::class,
'select' => Inputs\Select::class,
'textarea' => Inputs\Textarea::class,
],
];

public function __construct(array $aliases = [])
{
$this->aliases = array_merge($this->aliases, $aliases);
}

/**
* @param string $class Name of input|html|plugin class.
* @param string $alias Name of input|html|plugin class.
* @param string $type Part of namespace inputs|html|plugins.
*/
public function make(string $class, string $type)
public function make(string $alias, string $type)
{
$class = ucfirst(camel_case(trim($class)));
$class = "Dobrik\\LaravelEasyForm\\Forms\\$type\\$class";
if (!array_key_exists($alias, $this->aliases[$type])) {
throw new InvalidAliasException(sprintf('Alias "%s" for type "%s" not registered', $alias, $type));
}

$class = $this->aliases[$type][$alias];
return new $class($this);
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
35 changes: 35 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,45 @@

namespace Dobrik\LaravelEasyForm;

use Dobrik\LaravelEasyForm\Forms\Creator;
use Dobrik\LaravelEasyForm\Forms\Factory;

class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
public function register()
{
$this->mergeConfigFrom($this->getAliasesConfigPath(), 'easy_form/aliases');
$this->mergeConfigFrom($this->getFormsConfigPath(), 'easy_form/forms');

$this->app->singleton(Creator::class, function () {
$creator = new Creator(new Factory());
return $creator->setRequest($this->app->make(\Illuminate\Http\Request::class))
->registerFormConfig($this->app->make('config')->get('easy_form.forms'));
}
);
}

public function boot()
{
$this->loadViewsFrom(__DIR__ . '/resources/views', 'easy_form');

$this->publishes([
__DIR__ . '/resources/views' => resource_path('views/vendor/easy_form'),
]);

$this->publishes([
$this->getAliasesConfigPath() => config_path('easy_form/aliases.php'),
$this->getFormsConfigPath() => config_path('easy_form/forms.php')
]);
}

private function getAliasesConfigPath()
{
return __DIR__ . '/resources/config/aliases.php';
}

private function getFormsConfigPath()
{
return __DIR__ . '/resources/config/forms.php';
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php

return [
'locales' => config('app.locales'),

'html' => [

],
Expand Down
File renamed without changes.
35 changes: 0 additions & 35 deletions src/resources/views/inputs/video.blade.php

This file was deleted.

69 changes: 0 additions & 69 deletions src/resources/views/plugins/ckeditor_block_button.blade.php

This file was deleted.

0 comments on commit 9e62d8c

Please sign in to comment.