neighborhoods / prefab
Neighborhoods Prefab builds Protean machinery.
Installs: 12 126
Dependents: 0
Suggesters: 0
Security: 0
Stars: 8
Watchers: 18
Forks: 2
Open Issues: 2
Requires
- php: ^7.3|^8.0
- doctrine/dbal: 2.7 - 2.12
- jsoumelidis/zend-sf-di-config: ^0.3|^0.4|^0.5
- laminas/laminas-code: ^3.3
- laminas/laminas-component-installer: ^2.1.1
- laminas/laminas-config-aggregator: ^1.0
- laminas/laminas-diactoros: ^1.7.1 || ^2.0
- laminas/laminas-http: ^2.14.2
- laminas/laminas-stdlib: ^3.1
- mezzio/mezzio: ^3.0.1
- mezzio/mezzio-fastroute: ^3.0
- mezzio/mezzio-helpers: ^5.0
- mezzio/mezzio-problem-details: ^1.0
- neighborhoods/buphalo: ^1.0
- neighborhoods/datadog-component: ^1.1
- neighborhoods/dependency-injection-container-builder: ^1.0
- symfony/config: ^4.4
- symfony/console: ^4.4
- symfony/dependency-injection: ^4.4
- symfony/expression-language: ^4.4
- symfony/filesystem: ^4.4
- symfony/finder: ^4.4
- symfony/yaml: ^4.4
Requires (Dev)
- mikey179/vfsstream: ^1.6
- phpunit/phpunit: ^7.4
- 8.x-dev
- 8.8.3
- 8.8.2
- 8.8.1
- 8.8.0
- 8.7.1
- 8.7.0
- 8.6.1
- 8.6.0
- 8.5.0
- 8.4.2
- 8.4.1
- 8.4.0
- 8.3.0
- 8.2.2
- 8.2.1
- 8.2.0
- 8.1.0
- 8.0.0
- 8.0.0-beta
- 8.0.0-alpha
- 7.x-dev
- 7.2.1
- 7.2.0
- 7.1.0
- 7.0.3
- 7.0.2
- 7.0.1
- 7.0.0
- 6.x-dev
- 6.1.3
- 6.1.2
- 6.1.1
- 6.1.0
- 6.0.0
- 5.x-dev
- 5.17.13
- 5.17.12
- 5.17.11
- 5.17.10
- 5.17.9
- 5.17.8
- 5.17.7
- 5.17.6
- 5.17.5
- 5.17.4
- 5.17.3
- 5.17.2
- 5.17.1
- 5.17.0
- 5.16.2
- 5.16.1
- 5.16.0
- 5.15.4
- 5.15.3
- 5.15.2
- 5.15.1
- 5.15.0
- 5.14.3
- 5.14.2
- 5.14.1
- 5.14.0
- 5.13.0
- 5.12.6
- 5.12.5
- 5.12.4
- 5.12.3
- 5.12.2
- 5.12.1
- 5.12.0
- 5.11.5
- 5.11.4
- 5.11.3
- 5.11.2
- 5.11.1
- 5.11.0
- 5.10.2
- 5.10.1
- 5.10.0
- 5.9.1
- 5.9.0
- 5.8.2
- 5.8.1
- 5.8.0
- 5.7.4
- 5.7.3
- 5.7.2
- 5.7.1
- 5.7.0
- 5.6.0
- 5.5.2
- 5.5.1
- 5.5.0
- 5.4.0
- 5.3.0
- 5.2.0
- 5.1.0
- 5.0.0
- 4.x-dev
- 4.1.x-dev
- 4.0.6
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1-BETA
- 4.0.0-BETA
- 3.0.1
- 0.0.0
- dev-fix/space-indentation-builder
- dev-MLSS-2212-search-criteria-validators
- dev-PREF-294
- dev-feature/MLSS-1923-search-criteria-validators
- dev-fix-sc-json-serialization
- dev-5.16-symfony-di-min-version
- dev-delete-bradfab-actors
- dev-feature/experiment-with-acceptance-tests
- dev-master
- dev-feature/PREF-191
This package is auto-updated.
Last update: 2024-12-13 22:06:09 UTC
README
A code generation tool. Takes the busywork out of building strongly-typed, patterned HTTP applications.
Table of Contents
- Getting Started
- Prefab Definition File Specification
- Search Criteria
- Supporting Actor Groups
- Subset Container Buildable Directories
- Semantic Versioning
- Debug Mode
- Migration guide from Prefab v6 to v7
- Migration guide from Prefab v7 to v8
Getting Started
Installation
Prefab can be installed using Composer:
composer require neighborhoods/prefab
Running Prefab
- In your composer file, ensure you have your project name defined. Use the
composer-example.json
file, found in the root of Prefab, as a template - Create your
Actor.prefab.definition.yml
file as outlined below. - From the root of your project run
./vendor/bin/prefab
- This will add all of the supporting files needed to create a working API endpoint
Prefab Fitness
To see working examples on how you can use Prefab, check out the Prefab Fitness repository for examples with fully self-contained environments that you can run locally.
Prerequisites
PSR-4 Namespace Definition
Prefab assumes users have defined a psr-4
namespace under the autoload
key in their composer.json
file. Namespaces MUST be defined as {VENDOR}\{PRODUCT_NAME}
. See the composer docs for more information. Prefab will write generated files under the fab/
directory in your project root. You should configure your autoloader to first look in src/
, then in fab/
. This will allow you to override generated files by placing updated copies of classes in the equivalent src/
location.
Example:
"autoload": {
"psr-4": {
"Neighborhoods\\MyPrefabbedProject\\": [
"src",
"fab"
]
}
}
Environment Variables
Prefab expects the following environment variables to be defined.
Project Structure
Prefab requires that all Prefab definition files be in a versioned directory under src/
. For example, src/V1/Actor.prefab.definition.yml
is valid while src/Actor.prefab.definition.yml
is not.
Materialized Views
Prefab puts an extremely high focus on performance. One of the ways Prefab achieves fast response times is by ensuring that all HTTP requests result in a single database query on an index, made possible through the use of materialized views. Prefab uses search criteria to interact with its database, which doesn't support table joins. That means all data for a given request MUST live in the same table.
Note: Since search criteria allows you to select which data you would like to return, a single materialized view with a superset of all data returned by your HTTP endpoints can be used.
Prefab Definition File Specification
The purpose of this document is to define the components needed to generate an HTTP endpoint for an actor from a .prefab.definition.yml
file
The file MUST be named {ACTORNAME}.prefab.definition.yml and saved under src/
. They should be stored in the same nested directory structure as you would like the machinery to be generated under fab/
.
table_name
- Name of the database table containing the data that populates the actor
supporting_actor_group
- The collection of supporting actors you need generated for the actor
- Can be one of
complete
,collection
,minimal
,handler
orrepository
- This field is optional and defaults to
complete
- See below for more information
tag_filter_fields_on_tracer
- This field is optional and default to
false
- Set to
true
if you want to tag on the default global tracer all the filter fields sent toMap\Repository\Handler
. - Note: to access the global tracer, the code uses the
neighborhoods/datadog-component
package. The Symfony container for your endpoint will not build unless you include the package source path in the list ofappended_paths
inside thehttp-buildable-directories.yml
file, under thetop_level_key
of your endpoint. The source path is usuallyvendor/neighborhoods/datadog-component/src
.
- This field is optional and default to
json_serialize_map_as_array
- This field is optional and default to
false
- Set to
true
when HTTP response containing actor map should be a JSON array rather than an object with numerical property names.
- This field is optional and default to
http_route
- The HTTP route to access the actor
- This field is optional and unnecessary if you don't want to expose the actor to HTTP traffic
http_verbs
- HTTP methods allowed for an actor. Can include
GET
,POST
,PUT
,PATCH
, andDELETE
. - Note: Since mutative and destructive actions are not yet patterned for repositories, you will need to override the generated handler to call the proper repository method.
- HTTP methods allowed for an actor. Can include
constants
- Additional user-defined constants to add to the actor interface.
- This field is optional
identity_field
- Name of the database column that uniquely identifies a record for the actor
properties
- The class properties of the actor. Each property should have:
data_type
- The type of object the property represents. This can be a primitive or a fully qualified namespaced object
- Note: This used to be called
php_type
which is maintained for backwards compatibility
record_key
- Name of the key containing the data that populates the class property
- If not set, defaults to property name
- Note: This used to be called
database_column_name
which is still maintained for backwards compatibility
nullable
- Whether or not this property can be null. If true, the builder method will surround this property with isset() before attempting to set the value on the actor
- If not set, defaults to false
created_on_insert
- This denotes properties that are not expected to be present before inserting the record into the database.
- If true, the buildForInsert() method will surround this property with isset() before attempting to set the value on the actor. However, the build() method will still require this property when building a record from the database.
- If not set, defaults to false
- The class properties of the actor. Each property should have:
Example Prefab Definition File:
Filename: User.prefab.definition.yml
table_name: mv1_user identity_field: id supporting_actor_group: complete http_route: /mv1/users/{searchCriteria:} http_verbs: - get - post - put - patch - delete constants: SOME_CONSTANT: some value NUMERIC_CONSTANT: 2.123 ARRAY_CONSTANT_WITH_KEYS: some_key: some_value some_nested_array: - 123 - test ARRAY_CONSTANT_WITHOUT_KEYS: - 1 - 3 - TEST properties: id: data_type: int nullable: false created_on_insert: true email: data_type: string nullable: true first_name: data_type: string nullable: false last_name: data_type: string nullable: false created_at: data_type: string nullable: false created_on_insert: true
Search Criteria
Search Criteria is a data mining query language (DMQL) utilized by Prefab applications to provide a flexible API that enables HTTP clients to define how they want to ask for data rather than services explicitly implementing each use case. Search Criteria consists of filters along with optional sort order and pagination instructions.
Filters
Search Criteria filters are used to define the WHERE
clause of a database query. Filters should be included as an array under the searchCriteria[filters] key. Each filter consists of the following:
Query Parameter Key: searchCriteria[filters]
Conditions
Sort Order
A sort order is used to define the order in which data should be returned. It consists of an array with field and a direction.
Query Parameter Key: searchCriteria[sortOrder]
Pagination
A page size can be defined in order to limit the number of results returned by a query. Set a page size by providing an integer under the key searchCriteria[pageSize]
. You can retrieve additional pages of data by supplying a current page. Set the current page by providing an integer under the key searchCriteria[currentPage]
.
Supporting Actor Groups
Prefab supports generating different subsets of supporting actors to support various use cases. For information on how to specify a supporting actor group, see the Prefab definition file specification. The following supporting actor groups are available:
complete
- Generates all supporting actors. This should be used when you need to be able to build an actor from storage and return it via HTTP.- Included supporting actors
- Factory
- Builder
- Aware Traits
- Repository
- Map
- Map Builder
- Handler
- Included supporting actors
collection
- Generates supporting actors for building and handling groups of typed objects. This is often used to represent a collection of actors in a JSON database column.- Included supporting actors
- Factory
- Builder
- Aware Traits
- Map
- Map Builder
- Included supporting actors
minimal
- Generates the minimum number of supporting actors to build an actor. This can be used to represent a single, typed object.- Included supporting actors
- Factory
- Builder
- Aware Traits
- Included supporting actors
handler
- Generates only Handler instance. This can be used in addition to Buphalo generated Actors to provide HTTP functionality. See Repository Handler docs- Included supporting actors
- Handler
- Included supporting actors
repository
- Generates the minimum number of supporting actors to have a map repository. This should be used when you need to retrieve actors from an external source (for example, from a HTTP API) but don’t need to expose an endpoint on the service itself.- Included supporting actors
- Factory
- Builder
- Aware Traits
- Repository
- Map
- Map Builder
- Included supporting actors
Subset Container Buildable Directories
As Symfony containers get bigger, the response times for HTTP requests increase. To prevent slow response times, Prefab supports user-defined subset container building. Prefab users can define what should be included in the Symfony container for each route, so only the necessary actors are initialized. This buildable directory file is optional and Prefab will build the entirety of src/
and fab/
by default if the file is not found. If the file is present, then all routes MUST have a corresponding key with directories to be built.
- Note: On the first request, Prefab will write this file to disk as a PHP array in the directory
data/cache/Opcache/HTTPBuildableDirectoryMap
. When making changes to the Buildable Directory File, the cached file MUST be deleted in order for changes to be reflected in the code. It is also highly recommended to ensure Opcache is enabled in production to prevent a read from disk on every HTTP request.
top_level_key
- should be the URI. If a key with the first two parts of the URI is not found, Prefab will then check for a key with only the first part.
- Examples
neighborhoods.com/mv1/property/{searchCriteria}
would build all directories under themv1/property
key in the example file belowneighborhoods.com/mv2/property/{searchCriteria}
would build all directories under themv2
key in the example file below
buildable_directories
- Represents the directories of HTTP actors you would like to include for a given request. Each buildable directory will include the corresponding path under both src/
and fab/
. Since the buildable_directories
represents paths, forward slashes (/) should be used as a separator.
- Example:
- A buildable directory of
MV1/property
will includefab/MV1/property
(if it exists) andsrc/MV1/property
(if it exists) in the container.
- A buildable directory of
welcome_baskets
- These are the namespaces of the HTTP machinery that Prefab generates that should be included in a request. Since welcome_baskets
represent namespaces, backslashes (\) should be used as a separator.
- Example:
Doctrine\DBAL
will include all files under theDoctrine\DBAL
namespace that are infab/Prefab5/Doctrine/DBAL
.
appended_paths
- These are any additional paths that you want included in your container that are not under fab/
or src/
. Paths are relative to the root of your project. Since appended_path
represents paths, forward slashes (/) should be used as a separator.
- Example:
vendor/neighborhoods/throwable-diagnostic-component/src
will include all files under thevendor/neighborhoods/throwable-diagnostic-component/src
folder in the container.
Example
# Place this file at the root of your application. mv1/property: # The URI to the component Handler. buildable_directories: - MV1/Property # The relative path to the directory of the component. welcome_baskets: - Doctrine\DBAL - PDO - Opcache - NewRelic - Zend\Expressive - SearchCriteria appended_paths: - vendor/neighborhoods/throwable-diagnostic-component/src mv2: # The URI to the component Handler. buildable_directories: - MV2 # The relative path to the directory of the component. welcome_baskets: - Doctrine\DBAL - PDO - Opcache - NewRelic - Zend\Expressive - SearchCriteria appended_paths: - some/other/dir
By default, Symfony containers are built on the first HTTP request. Since this can add a significant amount of time to the first request, it is recommended that you prebuild your containers before serving production traffic. This can be accomplished by adding the following script to your bin/
directory and including it as a post-update-cmd
and/or post-install-cmd
script in your composer.json
file after vendor/bin/prefab
.
#!/usr/bin/env php <?php declare(strict_types=1); error_reporting(E_ALL); require_once __DIR__ . '/../vendor/autoload.php'; use ReplaceThisWithYourVendorName\ReplaceThisWithTheNameOfYourProduct\Prefab5\HTTPBuildableDirectoryMap\ContainerBuilder; $primer = new ContainerBuilder\Primer(); $primer->primeContainers(); return;
Semantic Versioning
Since Prefab is a code generation tool that allows users to override specific files and behaviors as needed, Prefab can't make a guarantee a minor version upgrade will work with your overriden files. Prefab does guarantee that all minor version upgrades of a purely Prefabbed project (no overriden files) will not have any breaking changes. If you do override any files, it's important that you test your project to verify those files still work as expected with the new version of Prefab.
Debug Mode
Debug mode can be enabled by setting the environment variable DEBUG_MODE=true
.
Enabling debug mode will output additional details about exceptions and errors thrown during HTTP requests.
If there is an error during container building (eg. A missing Symfony service file), you will have the additional visibility provided by debug mode.
Debug mode will try to send the error to STDERR and also it will try to log the error in /Logs/HTTP.log