mingomax / symfony-flex-backend

REST API with Symfony Flex

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

What is this

MIT licensed Build Status Coverage Status Scrutinizer Code Quality Maintainability Quality Gate SonarCloud Bugs SonarCloud Vulnerabilities SensioLabsInsight

JSON REST API which is build on top of Symfony framework.

Note that this project is build with Symfony 4 and Symfony Flex.

Table of Contents

Requirements

Installation

1. Clone repository

Use your favorite IDE and get checkout from GitHub or just use following command

git clone https://github.com/tarlepp/symfony-flex-backend.git

2. Configuration

Next you need to create .env file, which contains all the necessary environment variables that application needs. You can create it by following command (in folder where you cloned this project):

cp .env.dist .env

Then open that file and make necessary changes to it. Note that this .env file is ignored on VCS.

3. Dependencies installation

Next phase is to install all needed dependencies. This you can do with following command, in your project folder:

composer install

Or if you haven't installed composer globally

curl -sS https://getcomposer.org/installer | php
php composer.phar install

4. Create JWT auth keys

Application uses JWT to authenticate users, so we need to create public and private keys to sign those. You can create new keys with following command.

make generate-jwt-keys

5. File permissions

Next thing is to make sure that application var directory has correct permissions. Instructions for that you can find here.

I really recommend that you use ACL option in your development environment.

6. Environment checks

To check that your environment is ready for this application. You need to make two checks; one for CLI environment and another for your web-server environment.

CLI environment

You need to run following command to make all necessary checks.

./vendor/bin/requirements-checker

Web-server environment

Open terminal and go to project root directory and run following command to start standalone server.

./bin/console server:start

Open your favorite browser with http://127.0.0.1:8000/check.php url and check it for any errors.

Apache

To get JWT authorization headers to work correctly you need to make sure that your Apache config has mod_rewrite enabled. This you can do with following command:

sudo a2enmod rewrite

7. Other (optionally)

Allow other IP's to access dev environment

If you want to allow another IP addresses or all to your dev environment see /allowed_addresses.php file for detailed information how you can allow certain IP addresses to have access to your dev environment.

Commands

Makefile

Symfony Flex comes with Makefile configuration so that you can easily run some generic commands via make command. Below is a list of currently supported (main commands) make commands, note that you can get this same list with just running make command:

cache-clear       Clears the cache
cache-warmup      Warms up an empty cache
generate-jwt-keys Generates JWT auth keys
phpmetrics        Generates PhpMetrics static analysis
run-tests-fastest Runs all test via fastest
run-tests         Runs all tests via phpunit
serve             Runs a local web server

Symfony console

You can list all Symfony console commands via following command:

./bin/console

Custom commands

Project contains following custom console commands to help eg. user management:

./bin/console user:management     # To manage your users and user groups
./bin/console api-key:management  # To manage your API keys
./bin/console make:rest-api       # To create skeleton classes for new REST resource

user:management

This command is just a wrapper for following commands:

./bin/console user:create         # To create user
./bin/console user:create-group   # To create user group
./bin/console user:create-roles   # To initialize user group roles
./bin/console user:edit           # To edit user
./bin/console user:edit-group     # To edit user group
./bin/console user:remove         # To remove user
./bin/console user:remove-group   # To remove user group
./bin/console user:list           # To list current users
./bin/console user:list-groups    # To list current user groups

api-key:management

This command is just a wrapper for following commands:

./bin/console api-key:create          # To create API key
./bin/console api-key:edit            # To edit API key
./bin/console api-key:change-token    # To change API key token
./bin/console api-key:remove          # To remove API key
./bin/console api-key:list            # To list API keys

Structure

todo

Development

IDE

I highly recommend that you use "proper" IDE to development your application. Below is short list of some popular IDEs that you could use.

Personally I recommend PhpStorm, but just choose one which is the best for you. Also note that project contains .idea folder that holds default settings for PHPStorm.

PHP Code Sniffer

It's highly recommended that you use this tool while doing actual development to application. PHP Code Sniffer is added to project dev dependencies, so all you need to do is just configure it to your favorite IDE. So the phpcs command is available via following example command.

./vendor/bin/phpcs -i

If you're using PhpStorm following links will help you to get things rolling.

Database changes

When you start a new project where you use this project as a "seed" first thing to do is to run following command:

./bin/console doctrine:migrations:diff

This will create a migration file which contains all necessary database changes to get application running with default database structure. You can migrate these changes to your database with following command:

./bin/console doctrine:migrations:migrate

After that you can start to modify or delete existing entities or create your own ones. Easiest way to make this all work is to follow below workflow:

  1. Make your changes (create, delete, modify) to entities in /src/Entity/ folder
  2. Run diff command to create new migration file
  3. Run migrate command to make actual changes to your database
  4. Run validate command to validate your mappings and actual database structure

Those commands you can run with ./bin/console doctrine:migrations:<command>.

With this workflow you get easy approach to generic database changes on your application. And you don't need to make any migrations files by hand (just let Doctrine handle those). Although remember to really take a closer look of those generated migration files to make sure that those doesn't contain anything that you really don't want.

Testing

Project contains bunch of tests (Functional, Integration, Unit) which you can run simply by following command:

make run-tests
# or alternative
./vendor/bin/phpunit

And if you want to run tests with fastest library use following command:

make run-tests-fastest
# or alternative
./vendor/bin/fastest -x phpunit.xml.dist
# or another alternative
find tests/ -name "*Test.php" | ./vendor/bin/fastest -v

Note that you need to create .env.test file to define your testing environment. This file has the same content as the main .env file, just change database and others to match your testing environment.

Or you could easily configure your IDE to run these for you.

Metrics

Project also contains PhpMetrics to make some analyze of your code. You can run this by following command:

make phpmetrics
# or alternative
./vendor/bin/phpmetrics --report-html=build/phpmetrics .

And after that open build/phpmetrics/index.html with your favorite browser.

Links / resources

Notes

User password hashing

By default this application uses Argon2 to hash all user passwords, and that is why application requires that your PHP has support for Sodium crypto library (libsodium) library.

If you cannot or don't want to use this as password hashing method you need to change security.yaml before installation step.

Authors

Tarmo Leppänen

License

The MIT License (MIT)

Copyright (c) 2017 Tarmo Leppänen

About

REST API with Symfony Flex

License:MIT License


Languages

Language:PHP 99.7%Language:Makefile 0.2%Language:HTML 0.1%