Skip to content

Commit

Permalink
Merge 26d77be into bde96e0
Browse files Browse the repository at this point in the history
  • Loading branch information
MGatner committed Jul 28, 2022
2 parents bde96e0 + 26d77be commit 5d1bd42
Show file tree
Hide file tree
Showing 23 changed files with 170 additions and 166 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; This file is for unifying the coding style for different editors and IDEs.
; More information at http://editorconfig.org

root = true

[*]
charset = utf-8
indent_size = 4
indent_style = space
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.{yml,yaml}]
indent_size = 2
2 changes: 1 addition & 1 deletion .github/workflows/deptrac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.0'
php-version: '8.1'
tools: phive
extensions: intl, json, mbstring, xml
coverage: none
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/infection.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.0'
php-version: '8.1'
tools: infection, phpunit
extensions: intl, json, mbstring, gd, xml, sqlite3
coverage: xdebug
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/phpcpd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.0'
php-version: '8.1'
tools: phpcpd
extensions: dom, mbstring
coverage: none
Expand Down
8 changes: 2 additions & 6 deletions .github/workflows/phpcsfixer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@ on:

jobs:
build:
name: PHP ${{ matrix.php-versions }} Coding Standards
name: Coding Standards
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[ci skip]')"
strategy:
fail-fast: false
matrix:
php-versions: ['7.4', '8.0', '8.1']

steps:
- name: Checkout
Expand All @@ -31,7 +27,7 @@ jobs:
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
php-version: '8.1'
extensions: json, tokenizer
coverage: none
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/psalm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.0'
php-version: '8.1'
tools: phpstan, phpunit
extensions: intl, json, mbstring, xml
coverage: none
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/rector.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,5 @@ jobs:
- name: Analyze for refactoring
run: |
composer global require --dev rector/rector:^0.12.16
composer global require --dev rector/rector:^0.13.8
rector process --dry-run --no-progress-bar
59 changes: 59 additions & 0 deletions .github/workflows/unused.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Unused

on:
pull_request:
branches:
- develop
paths:
- '**.php'
- 'composer.*'
- '.github/workflows/unused.yml'
push:
branches:
- develop
paths:
- '**.php'
- 'composer.*'
- '.github/workflows/unused.yml'

jobs:
build:
name: Unused Package Detection
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, '[ci skip]')"

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
tools: composer, composer-unused
extensions: intl, json, mbstring, xml
coverage: none
env:
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache composer dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install dependencies
run: |
if [ -f composer.lock ]; then
composer install --no-progress --no-interaction --prefer-dist --optimize-autoloader
else
composer update --no-progress --no-interaction --prefer-dist --optimize-autoloader
fi
- name: Detect unused packages
run: composer-unused -vvv --output-format=github --ansi --no-interaction --no-progress
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
->append([__FILE__]);

$overrides = [
// Some tests need to be rewritten to support assertSame instead of assertEquals
'php_unit_strict' => false,
];

Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 Tatter Software
Copyright (c) 2019-2022 Tatter Software

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
90 changes: 47 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Tatter\Forms
RESTful AJAX forms for CodeIgniter 4

[![](https://github.com/tattersoftware/codeigniter4-forms/workflows/PHPUnit/badge.svg)](https://github.com/tattersoftware/codeigniter4-forms/actions?query=workflow%3A%22PHPUnit)
[![](https://github.com/tattersoftware/codeigniter4-forms/workflows/PHPStan/badge.svg)](https://github.com/tattersoftware/codeigniter4-forms/actions?query=workflow%3A%22PHPStan)
[![](https://github.com/tattersoftware/codeigniter4-forms/workflows/PHPUnit/badge.svg)](https://github.com/tattersoftware/codeigniter4-forms/actions/workflows/phpunit.yml)
[![](https://github.com/tattersoftware/codeigniter4-forms/workflows/PHPStan/badge.svg)](https://github.com/tattersoftware/codeigniter4-forms/actions/workflows/phpstan.yml)
[![](https://github.com/tattersoftware/codeigniter4-forms/workflows/Deptrac/badge.svg)](https://github.com/tattersoftware/codeigniter4-forms/actions/workflows/deptrac.yml)
[![Coverage Status](https://coveralls.io/repos/github/tattersoftware/codeigniter4-forms/badge.svg?branch=develop)](https://coveralls.io/github/tattersoftware/codeigniter4-forms?branch=develop)

## Quick Start
Expand All @@ -20,33 +21,28 @@ AJAX forms with CodeIgniter 4's native RESTful implementation.

Install easily via Composer to take advantage of CodeIgniter 4's autoloading capabilities
and always be up-to-date:
> composer require tatter/forms
```shell
> composer require tatter/forms
```

Or, install manually by downloading the source files and adding the directory to
`app/Config/Autoload.php`.

After installation you will need to publish the required assets to your **public** folder,
as defined by the [manifest files](src/Manifests/):
php spark assets:publish
**app/Config/Autoload.php**.

> *Tip*: You can add the publish command to your **composer.json**'s `post-update-cmd` section to ensure your assets are always in sync with the package source.
After installation you will need to copy or publish the required assets to your
**public/** folder. If you want to automate this process check out the
[Assets Library](https://github.com/tattersoftware/codeigniter4-assets).

Finally, notify your view/layout of your intention to use the JavaScript for your forms (your paths
may vary):
```
<link href="assets/vendor/bootstrap/bootstrap.min.css" rel="stylesheet" type="text/css" />
Finally, notify your view/layout of your intention to use the JavaScript for your forms
(your paths may vary):
```html
<script src="assets/vendor/forms/forms.js" type="text/javascript"></script>
<script src="assets/vendor/bootstrap/bootstrap.bundle.min.js" type="text/javascript"></script>
<script>
var baseUrl = "<?= base_url() ?>";
var siteUrl = "<?= site_url() ?>";
var apiUrl = "<?= site_url(config('forms')->apiUrl) ?>";
</script>
```

> *Tip*: **Forms** includes [Tatter\Assets](https://github.com/tattersoftware/codeigniter4-assets) which you can use to auto-include assets via the Config file

## Configuration (optional)

The library's default behavior can be overridden or augment by its config file. Copy
Expand All @@ -55,12 +51,12 @@ comments. If no config file is found the library will use its defaults.

## Usage

*N.B. Please consider this module to be modular itself - you need not use every piece!*
***Note:*** Please consider this module to be modular itself - you need not use every piece!*
*Treat portions of the code that you do not use as examples for how to implement this in your own app.*

After the initial installation there are a few pieces to implement. **Forms** will run
CRUD-style operations for you by interfacing views with the **ResourcePresenter** or
**ResourceController** depending on the method of interaction (i.e. page load versus AJAX).
CRUD-style operations for you by interfacing views with the `ResourcePresenter` or
`ResourceController` depending on the method of interaction (i.e. page load versus AJAX).
Not surprisingly, you will need some **Views** and two **Controllers** per resource.

### Naming
Expand All @@ -69,11 +65,11 @@ Not surprisingly, you will need some **Views** and two **Controllers** per resou
If you are creating a game, your resource names might be *hero(es)*, *level(s)*, *reward(s)*,
etc. The naming convention is important for autoloading resources and their endpoints. By
default, **Forms** will use the name of the model associated with your resource. So a URL of
`heroes/new` would route to the **HeroController** which uses **HeroModel** and the whole
resource would be dubbed **hero** off that model.
`heroes/new` would route to the `HeroController` which uses `HeroModel` and the whole
resource would be dubbed "hero" off that model.

If you need to set your own names, do so with your model's `$name` property:
```
```php
class HeroModel extends \CodeIgniter\Model
{
public $name = 'superhero';
Expand All @@ -97,38 +93,49 @@ for each resource (where {names} is the plural of your resource name):

As you can see **Forms** expects some views that are part of a full page load layout and
some that can be injected into an existing page via AJAX (e.g. in a modal). See
[examples](examples/Views/) for a full set of example view files.
[examples](examples/Views/) for a full set of example view files (note: these are presented
"as is" and may not always be the best solution for all use cases).

### Controllers

In addition to the views, you will need two controllers for each resource:
* **Controllers/{names}.php** - Your presenter for page loads, extends `\Tatter\Forms\Controllers\ResourcePresenter.php`
* **Controllers/API/{names}.php** - Your controller for AJAX calls, extends `\Tatter\Forms\Controllers\ResourceController.php`
* **Controllers/{names}.php** - Your presenter for page loads, extends `Tatter\Forms\Controllers\ResourcePresenter`
* **Controllers/API/{names}.php** - Your controller for AJAX calls, extends `Tatter\Forms\Controllers\ResourceController`

As with standard framework controllers, your controllers set their model via the `$modelName`
property:
```
<?php namespace App\Controllers;
As with other framework RESTful controllers, your controllers set their model via the
`$modelName` property:
```php
<?php

class Heroes extends \Tatter\Forms\Controllers\ResourcePresenter
namespace App\Controllers;

use App\Models\HeroModel;
use Tatter\Forms\Controllers\ResourcePresenter;

class Heroes extends ResourcePresenter
{
protected $modelName = 'App\Models\HeroModel';
protected $modelName = HeroModel::class;
...
```

Your resource controller can take an additional property, `$format`, to specify response
format (**Forms** currently only supports JSON):
```
<?php namespace App\Controllers\API;
```php
<?php

class Heroes extends \Tatter\Forms\Controllers\ResourceController
namespace App\Controllers\API;

use App\Models\HeroModel;
use Tatter\Forms\Controllers\ResourceController;

class Heroes extends ResourceController
{
protected $modelName = 'App\Models\BookModel';
protected $modelName = HeroModel::class;
protected $format = 'json';
...
```

See [RESTful Resource Handling](https://codeigniter4.github.io/userguide/incoming/restful.html)
See [RESTful Resource Handling](https://codeigniter.com/user_guide/incoming/restful.html)
in the CodeIgniter 4 User Guide for more info on using resource controllers.

### Routes
Expand All @@ -152,10 +159,7 @@ and controllers and auto-handling AJAX requests. If you want a more complete set
or fully automated object handler you will want to include your own third-party tools and
implement them in your view files.

## Appendix

### jQuery
#### jQuery

This package uses jQuery because Bootstrap 4 requires it. The Bootstrap team officially
announced plans to remove the jQuery dependency in Bootstrap 5, at which point this package
will transition to native JavaScript (via [Fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)).
This package uses jQuery (not included). Future versions will drop jQuery in favor of
native JavaScript (via [Fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)).
14 changes: 6 additions & 8 deletions composer-unused.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
use ComposerUnused\ComposerUnused\Configuration\PatternFilter;
use Webmozart\Glob\Glob;

return static function (Configuration $config): Configuration {
return $config
// ->addNamedFilter(NamedFilter::fromString('symfony/config'))
// ->addPatternFilter(PatternFilter::fromString('/symfony-.*/'))
->setAdditionalFilesFor('codeigniter4/framework', [
...Glob::glob(__DIR__ . '/vendor/codeigniter4/framework/system/Helpers/*.php'),
]);
};
return static fn (Configuration $config): Configuration => $config
// ->addNamedFilter(NamedFilter::fromString('symfony/config'))
// ->addPatternFilter(PatternFilter::fromString('/symfony-.*/'))
->setAdditionalFilesFor('codeigniter4/framework', [
...Glob::glob(__DIR__ . '/vendor/codeigniter4/framework/system/Helpers/*.php'),
]);
17 changes: 8 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"forms",
"REST",
"AJAX",
"javascript",
"JavaScript",
"CRUD"
],
"authors": [
Expand All @@ -22,15 +22,10 @@
],
"homepage": "https://github.com/tattersoftware/codeigniter4-forms",
"require": {
"php": "^7.4 || ^8.0",
"components/jquery": "^3.3",
"tatter/alerts": "^2.0",
"tatter/assets": "^2.0",
"twbs/bootstrap": "^4.3"
"php": "^7.4 || ^8.0"
},
"require-dev": {
"codeigniter4/framework": "^4.1",
"mikey179/vfsstream": "^1.6",
"tatter/tools": "^2.0"
},
"minimum-stability": "dev",
Expand All @@ -54,14 +49,18 @@
}
},
"scripts": {
"analyze": "phpstan analyze",
"analyze": [
"phpstan analyze",
"psalm",
"rector process --dry-run"
],
"ci": [
"Composer\\Config::disableProcessTimeout",
"@deduplicate",
"@analyze",
"@composer normalize --dry-run",
"@test",
"@inspect",
"rector process",
"@style"
],
"deduplicate": "phpcpd app/ src/",
Expand Down
2 changes: 1 addition & 1 deletion deptrac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ parameters:
Service:
- Config
- Vendor Config

# Ignore anything in the Vendor layers
Vendor Model:
- Config
Expand Down

0 comments on commit 5d1bd42

Please sign in to comment.