From 45327452ca78efc4299324751d17344b26e4b647 Mon Sep 17 00:00:00 2001 From: Brandon Date: Sun, 17 May 2020 02:51:43 -0500 Subject: [PATCH] v1.0.1 (#1) * enhance(console): Add make:form command * chore(docs): Add console command to docs * chore(docs): Add error message customization to docs * fix(component): Fix hidden slot * fix(ci): Fix CircleCI --- .circleci/config.yml | 65 +++++--- README.md | 28 +++- .../views/components/html-forms.blade.php | 2 +- src/Console/FormMakeCommand.php | 148 ++++++++++++++++++ src/Console/stubs/views/default.stub | 20 +++ src/HtmlFormsServiceProvider.php | 5 + src/View/Components/HtmlForms.php | 2 + 7 files changed, 244 insertions(+), 26 deletions(-) create mode 100644 src/Console/FormMakeCommand.php create mode 100644 src/Console/stubs/views/default.stub diff --git a/.circleci/config.yml b/.circleci/config.yml index 864e1f8..6c2e963 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,29 +1,52 @@ version: 2.1 orbs: + php: circleci/php@0.1 + +# To be removed after https://github.com/CircleCI-Public/php-orb/pull/11 +executors: default: - executors: - php-73: - docker: - - image: 'circleci/php:7.3-stretch' - jobs: - build-php: - executor: php-73 - steps: - - run: php -v - - checkout - - restore_cache: - keys: - - composer-v1-{{ checksum "composer.lock" }} - - composer-v1- - - run: composer install -n --prefer-dist --no-scripts --no-suggest - - run: composer lint - - save_cache: - key: composer-v1-{{ checksum "composer.lock" }} - paths: - - vendor + description: The official next-gen CircleCI PHP Docker image. + parameters: + tag: + description: The `cimg/php` Docker image version tag. + type: string + docker: + - image: 'cimg/php:<< parameters.tag >>' + +# To be removed after https://github.com/CircleCI-Public/php-orb/issues/23 +jobs: + build-php: + parameters: + version: + default: '7.4' + description: The `cimg/php` Docker image version tag. + type: string + executor: + name: default + tag: << parameters.version >> + steps: + # Because squizlabs/php_codesniffer requires ext-simplexml. + # To be removed after https://github.com/CircleCI-Public/cimg-php/pull/51 + - run: sudo apt-get update + - run: sudo apt-get install -y php<< parameters.version >>-xml + - run: php -v + - run: composer --version + # To be removed after https://github.com/CircleCI-Public/cimg-php/pull/52 + - run: sudo chown $(whoami):$(whoami) ~/.composer + - checkout + - php/load-cache: + key: v1 + - run: composer install -n --prefer-dist + - run: composer lint + - php/save-cache: + key: v1 workflows: build: jobs: - - default/build-php + - build-php: + name: build-php-<< matrix.version >> + matrix: + parameters: + version: ['7.4', '7.3', '7.2'] diff --git a/README.md b/README.md index 4559f62..63debb1 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ ![Build Status](https://img.shields.io/circleci/build/github/Log1x/sage-html-forms?style=flat-square) ![Total Downloads](https://img.shields.io/packagist/dt/log1x/sage-html-forms?style=flat-square) -This is a simple package for [HTML Forms](https://github.com/ibericode/html-forms) that allows you to easily render forms using a corresponding Blade view (if one is present) with Sage 10. +This is a simple package for the plugin [HTML Forms](https://github.com/ibericode/html-forms) that allows you to easily render forms using a corresponding Blade view (if one is present) with Sage 10. A few additional opinionated tweaks include: @@ -35,7 +35,13 @@ You can leave the "Form code" blank as it will not be used if a corresponding Bl ### Creating a View -Once your form is created, create a Blade view in `views/forms/contact-us.blade.php` where `contact-us` is equal to your form's slug. +Once your form is created, simply generate a view using the slug assigned to your form: + +```bash +$ wp acorn make:form contact-us +``` + +You will find the generated form view in `resources/views/forms/contact-us.blade.php` containing a simple form component: ```php @@ -47,7 +53,7 @@ Once your form is created, create a Blade view in `views/forms/contact-us.blade. > ``` -...and you're done. Form action variables simply reference the input `name` so `[NAME]` and `[COOL_EMAIL]` will now be readily available. +When HTML Forms processes "Form Actions" – it simply fetches each input name to create the usable variables. + +That being said, the default view would provide `[NAME]` and `[EMAILADDRESS]`. + +#### Error Messages + +Outside of defining your error messages on the options page, you can optionally provide them to the `` component directly: + +```php + +``` ## Bug Reports diff --git a/resources/views/components/html-forms.blade.php b/resources/views/components/html-forms.blade.php index be31385..78a2076 100644 --- a/resources/views/components/html-forms.blade.php +++ b/resources/views/components/html-forms.blade.php @@ -2,7 +2,7 @@
- {!! $hiddenFields !!} + {!! $hidden !!}
{!! $slot !!} diff --git a/src/Console/FormMakeCommand.php b/src/Console/FormMakeCommand.php new file mode 100644 index 0000000..4b86ee8 --- /dev/null +++ b/src/Console/FormMakeCommand.php @@ -0,0 +1,148 @@ +task("Generating {$this->getViewName()}", function () { + if (! $this->files->exists($this->getViewPath())) { + $this->files->makeDirectory($this->getViewPath()); + } + + if ($this->files->exists($this->getView())) { + return; + } + + $this->files->put($this->getView(), $this->files->get($this->getViewStub())); + }); + + return $this->summary(); + } + + /** + * Return the full view destination. + * + * @return string + */ + public function getView() + { + return Str::finish($this->getViewPath(), $this->getViewName()); + } + + /** + * Return the view destination filename. + * + * @return string + */ + public function getViewName() + { + return Str::finish( + str_replace('.', '/', Str::slug(Str::snake($this->getNameInput()))), + '.blade.php' + ); + } + + /** + * Return the view destination path. + * + * @return string + */ + public function getViewPath() + { + return Str::finish($this->getPaths(), '/forms/'); + } + + /** + * Get the view stub file for the generator. + * + * @return string + */ + protected function getViewStub() + { + return __DIR__ . "/stubs/views/{$this->view}.stub"; + } + + /** + * Return the applications view path. + * + * @param string $name + * @return void + */ + protected function getPaths() + { + $paths = $this->app['view.finder']->getPaths(); + + if (count($paths) === 1) { + return head($paths); + } + + return $this->choice('Where do you want to create the view(s)?', $paths, head($paths)); + } + + /** + * Return the block creation summary. + * + * @return void + */ + protected function summary() + { + $this->line(''); + $this->line('Form View Created'); + $this->line(" ⮑ {$this->shortenPath($this->getView(), 4)}"); + } + + /** + * Returns a shortened path. + * + * @param string $path + * @param int $i + * @return string + */ + protected function shortenPath($path, $i = 3) + { + return collect( + explode('/', $path) + )->slice(-$i, $i)->implode('/'); + } + + /** + * Get the stub file for the generator. + * + * @return string + */ + protected function getStub() + { + // + } +} diff --git a/src/Console/stubs/views/default.stub b/src/Console/stubs/views/default.stub new file mode 100644 index 0000000..c11ba19 --- /dev/null +++ b/src/Console/stubs/views/default.stub @@ -0,0 +1,20 @@ + + + + + + + diff --git a/src/HtmlFormsServiceProvider.php b/src/HtmlFormsServiceProvider.php index 5d39ece..bcaabcf 100644 --- a/src/HtmlFormsServiceProvider.php +++ b/src/HtmlFormsServiceProvider.php @@ -5,6 +5,7 @@ use Roots\Acorn\ServiceProvider; use Illuminate\View\Compilers\BladeCompiler; use Log1x\HtmlForms\HtmlForms; +use Log1x\HtmlForms\Console\FormMakeCommand; use Log1x\HtmlForms\View\Components\HtmlForms as HtmlFormsComponent; class HtmlFormsServiceProvider extends ServiceProvider @@ -28,6 +29,10 @@ public function register() */ public function boot() { + $this->commands([ + FormMakeCommand::class, + ]); + $this->loadViewsFrom(__DIR__ . '/../resources/views', 'HtmlForms'); $this->callAfterResolving(BladeCompiler::class, function ($view) { diff --git a/src/View/Components/HtmlForms.php b/src/View/Components/HtmlForms.php index 0893377..3516d9e 100644 --- a/src/View/Components/HtmlForms.php +++ b/src/View/Components/HtmlForms.php @@ -36,6 +36,8 @@ public function __construct( $hidden = null ) { $this->form = hf_get_form($form); + $this->hidden = $hidden; + $this->form->attributes = collect($this->form->messages)->merge( collect($messages)->keyBy(function ($value, $key) { return Str::snake($key);