Download the PHP package tribeos/flight-php-swagger without Composer
On this page you can find all versions of the php package tribeos/flight-php-swagger. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tribeos/flight-php-swagger
More information about tribeos/flight-php-swagger
Files in tribeos/flight-php-swagger
Package flight-php-swagger
Short Description FlightPHP micro-framework bundled with Swagger and other useful utilities.
License MIT
Informations about the package flight-php-swagger
Flight/Swagger bundle
Flight micro-framework bundled with Swagger, PHPUnit and other useful utilities. Designed to help kick-start the development of APIs built with the aforementioned tools by including the basic project setup, test routes and models, as well as the template for unit tests.
Getting started
Prerequisites
This guides assumes that you are currently using (or planning to use) the following technologies in your project:
Installing
- First, make sure that you have installed and configured a running web server (such as Apache) with PHP 7.1 or higher.
-
Use
composer
to download the project skeleton, install all required dependencies, and run the sample unit tests:composer create-project tribeos/flight-php-swagger path/to/project
Enable URL rewriting
Since Flight depends on Apache 2's mod_rewrite
module in order to function, you should first enable it via a2enmod
and then restart the server:
sudo a2enmod rewrite
sudo systemctl restart apache2
Afterwards, you should also edit Apache's default configuration file, as it initially prohibits using an .htaccess
file to apply rewrite rules. The configuration file is usually located at /etc/apache2/apache2.conf
. In it, locate the block referring to the directory /var/www
and make sure that AllowOverride
is set to All
and that you Require all granted
.
After saving the changes, restart the Apache server.
In case you have, or want to have, multiple site configurations, you can also enable rewrite rules on a per-site basis, by editing the desired configuration file in /etc/apache2/sites-available
. You can refer to the following tutorial for more information.
Note: The aforementioned instructions assume you are using an Ubuntu distribution. In case you are using a different operating system, please refer to the appropriate tutorial on enabling the rewrite module.
Install missing PHP extensions
The project and its dependencies require certain PHP extensions to be installed. Common extensions that you may be missing could be some of the following:
Issue | Solution |
---|---|
mbstring |
sudo apt install php-mbstring |
zip and unzip related issues |
sudo apt install zip unzip php-zip |
Note: As with the previous Apache 2 section, these instructions relate to Ubuntu distributions. Refer to an appropriate alternative command if using a different operating system.
Project structure
Once the installation is finished, you will find this structure in your project:
Rundown of main project components:
app
: the main application foldermodels
: models used for requests/responsesroutes
: definition of API routesutils
: application utilites, such as validtors and loggers
config
: configuration file(s)docs
: Swagger documentation fileslogs
: logs storagetests
: application tests folderbuild
: any compiled files resulting form tests (code coverage, etc.)src
: test source files
vendor
: location of all libraries and third-party codeautoload.php
: A bootstrapping file that loads Composer dependencies, and includes all other project filesindex.php
: API entry pointphpunit.xml
: PHPUnit testing configuration
Working on the project
Project configuration
All main configuration related to the project (and Swagger documentation) is handled inside the config/config.php
file.
Change/add the configuration constants according to your project needs.
Another important file is index.php
, which serves as the API entry point. It requires the file autoload.php
, which contains the code to load Composer dependencies, as well as all other necessary project files. Moreover, it starts the Flight framework. The files imported by default are all models
, routes
and utils
, as well as the project configuration file (config/config.php
), Swagger-PHP configuration file (docs/swagger.php
) and the Composer's vendor/autoload.php
.
Change/add files to be imported according to your project needs.
Flight
Flight is an extensible and easy-to-use PHP framework which allows for fast creation of RESTful web APIs. It enables you to create detailed and functional API endpoints, coupled with middleware functionalities and the ability to override/create custom methods.
All routing in the project is done via Flight. The initial Flight setup (FlightSetup.php
), as well as all project routes are handled inside the routes
folder, and can be changed to suit your needs.
Extensive usage documentation and examples about Flight are available on the Flight homepage - "Learn" area. For more details about the Flight framework itself, you can visit its GitHub repository.
Swagger-PHP
After creating a route in the routes
folder, you can begin writing and generating OpenAPI documentation for your RESTful API.
Documentation is written inside DocBlocks, a.k.a PHP comment blocks, above the corresponding route.
You can define the path, tags, short summary, parameters, default parameter values, request body, available responses, security authentication and numerous other options for every RESTful endpoint.
General Swagger configuration, such as the project name, project author(s), API server URL, specification folders, etc. can be found and configured in config/config.php
, under the "Swager configuration constants" area.
The project skeleton comes coupled with the docs
folder, which holds all necessary files for Swagger documentation (PHP setup files, HTML and CSS). The documentation is accessed by navigating to the root route (/
) of your project via a browser.
For more information about Swagger in general, and the OpenAPI standard, refer to Swagger official homepage. Extensive usage documentation and examples regarding the PHP implementation are available on the swagger-php official homepage.
Project utilities
Logger
The project comes bundled with http-logger, which is a HTTP request, response and error logger, that will (by default) log every incoming request and its corresponding response into logs/debug.log
, as tab-separated values (TSV). Detailed logger information and configuration details can be found at its GitHub page.
The logger requires the location of the log file (which is by default configured via a constant in config/config.php
). Moreover, in order to fully utilize the logger's capabilities, Flight's internal error handler should be disabled (if enabled, it will catch certain types of errors before http-logger).
The logger is included via routes/FlightSetup.php
, and configured inside the Flight middleware function Flight::after()
, which runs after every request (so it is able to fetch the appropriate response). You can change the logger functionalities, as well as the routes it will watch, at your disclosure.
Flight::output()
Flight::output()
is a custom mapped method that combines the functionalities of Flight::json()
(JSON response output), and the custom HTTP Logger. Any API response that is sent out via this method will automatically be logged, according to the preferences set in the logger. The method is defined and configured in routes/FlightSetup.php
.
It is useful when you want the logging functionality for your request/response pairs without using Flight middleware to intercept.
Flight::output()
takes two parameters: the actual data to be sent out (required), and the response code (optional, 200 by default).
Flight::validate()
Flight::validate()
uses Swagger-PHP analytics to determine whether a passed-in request body is valid (containing all required attributes) according to the expected OpenAPI model specification.
All models are declared as classes, and their attributes are set to public. The models are defined in app/models
, and follow this structure:
In this concrete example, description
is used to give a brief overview of the model's property, required
determines whether the property is mandatory or not, and example
assigns it a default value. For more information about model schemas, refer to Swagger-PHP documentation.
The model validator class is declared and defined in utils/Validator.php
, and included as a custom mapped Flight method in routes/FlightSetup.php
. It takes two parameters: the class against which the request body is to be validated, and the request body itself.
The method should be called inside an API route, before any additional methods are called. If the model is valid, the request will continue with execution. In case the model fails to validate, it will output a 400 Bad Request
response code, terminating the request.
Note: Flight::validate()
depends on Flight::output()
, so if you plan on removing the custom output method from the project, you should rewrite the validator mapped method as required.
Testing the project
The project comes included with PHPUnit, a testing framework for PHP which, in addition to testing itself, also allows for code coverage generation.
Test setup
All setup related to testing with PHPUnit is located in phpunit.xml
. There are several options available for setting up:
-
The location of test files Inside the
testsuites
tag, the location which will be scanned for tests can be defined. By default, this istests/unit
. When a test directory is defined (viadirectory
tag), the test files will be run in alphabetical order during test execution. If needed, we can also define the test files in a custom order viafile
tag. -
Types of test coverage reports Different types of testing reports can be generated after the tests are run. The log types are defined in the
logging
tag. All compiled test files are, by default, located insidetests/build
. - Files to be included/excluded from code coverage
You can also choose which files will be included in (or excluded from) the code coverage report. That is done through the
filter
tag, inside thewhitelist
tag. By default, the project's PHPUnit setup covers files within theapp
directory, excludingmodels
androutes
(as these folders do not contain code that can be effectively tested for code coverage). You can also define your custom inclusion/exclusion folders and files.
Writing the tests
All unit tests should be, by default, written inside the tests/unit
directory. Test file names should end with Test.php
, so that they are discoverable by PHPUnit (as defined in phpunit.xml
), e.g. SampleTest.php
.
A test class is defined as extending the TestCase
class, and it can include various test methods. When naming the tests, you can use the following convention: test
+ description of the test in camel case (e.g. testThisIsASampleTest
). This will allow the test case name to be converted to a descriptive sentence during test execution, giving additional overview to the developer about what the test should do.
Test execution
To execute all tests, run vendor/bin/phpunit --testdox
inside the project's root directory.
To run only a single test file, supply its path: vendor/bin/phpunit --testdox tests/unit/SampleTest.php
The flag --testdox
is optional, but recommended, as it will generate descriptive sentences about the tests based on their names (as discussed in the section above), making it easier to understand what is going on inside the tests.
After executing the tests, the code coverage report is generated inside tests/build/coverage
directory, and can be viewed by opening this path via a browser.
Authors
- Aldin Kovačević, initial work on the skeleton and documentation - Aldin-SXR
License
The skeleton is licensed under the MIT license. See the LICENSE file for details.
Work in progress.
All versions of flight-php-swagger with dependencies
mikecao/flight Version ^1.3
zircote/swagger-php Version ^3.0
tribeos/http-logger Version ^0.5.0