synapsestudios / synapse-base
Base code for the API template
Installs: 11 225
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 38
Forks: 4
Open Issues: 21
Requires
- bshaffer/oauth2-server-httpfoundation-bridge: v1.0
- bshaffer/oauth2-server-php: v1.0
- chrisboulton/php-resque: v1.2
- guzzle/guzzle: ~3.7
- ircmaxell/password-compat: dev-master
- jdesrosiers/silex-cors-provider: ~0.1
- lusitanian/oauth: ~0.2
- mailgun/mailgun-php: ^2.1
- mandrill/mandrill: 1.0.52
- monolog/monolog: ~1.18.2
- mustache/mustache: ~2.5.1
- php-http/guzzle6-adapter: ^1.0
- psr/log: v1.0.0
- rollbar/rollbar: ~0.14
- silex/silex: 1.2.5
- symfony/console: 2.5.2
- symfony/security: 2.5.2
- symfony/validator: 2.5.2
- tijsverkoyen/css-to-inline-styles: ^2.0@dev
- xamin/handlebars.php: ^0.10
- zendframework/zend-db: ~2.2.5
- zendframework/zend-stdlib: ~2.2.5
Requires (Dev)
- phpunit/phpunit: ~4.5
- sebastian/exporter: ~1.2
- squizlabs/php_codesniffer: dev-master
- symfony/browser-kit: 2.6.7
- symfony/dom-crawler: 2.6.7
- dev-master
- v5.0.5
- v5.0.4
- v5.0.3
- v5.0.2
- v5.0.1
- v5.0.0
- v4.1.1
- v4.1.0
- v4.0.6
- v4.0.5
- v4.0.4
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0.0
- v3.0.2
- v3.0.1
- v3.0.0
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.1
- v2.0.0
- v1.4.2
- v1.4.1
- v1.4.0
- v1.3.4
- v1.3.3
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.0
- v1.1.1
- v1.0.2
- v1.0.1
- v1.0.0
- v0.6.0
- v0.5.2
- v0.5.1
- v0.5.0
- v0.4.4
- v0.4.3
- v0.4.2
- v0.4.1
- v0.4.0
- v0.3.6
- v0.3.5
- v0.3.4
- v0.3.3
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.6
- v0.2.5
- v0.2.4
- v0.2.3
- v0.2.2
- v0.2.1
- v0.2.0
- 0.1.0
- dev-mailgun-sender-fixes
- dev-test-monolog-version
- dev-release/4.1.0
- dev-feature/225-configurable-rollbar-threshold
- dev-feature/202-internationalizable-validation-messages
- dev-feature/125-findby-table-name
- dev-release/1.4.2
This package is not auto-updated.
Last update: 2020-01-18 10:04:57 UTC
README
Overview
Synapse Base is a bootstrapping library for PHP applications build in the Silex microframework. It's intended for REST APIs that serve and consume JSON.
This library provides an opinionated, secure starting point for your REST API using the following tools and libraries:
- Silex
- bshaffer's OAuth2 server
- Zend DB
- chrisboulton's php-resque
- Mandrill PHP SDK for emails
- Symfony's Console, Security, and Validator components
- Monolog for logging
- And many others
Setting up your Project
Quick Start
For new projects, just clone the API Template as a starting point and composer install
.
Expectations of the Library
Architectural Expectations
- A MySQL server for app data
- A Redis server for job queues
Project Codebase Expectations
(If you just use API Template, you don't need to know most of this.)
- Some named constants are expected to exist.
- The
APP_ENV
environment variable should be set. (Todevelopment
,production
, orstaging
.) - Specific config files should be set up in
[APPDIR]/config/
. To set up environment-specific configuration overrides, put identical config files in[APPDIR]/config/development/
, etc. See API Template for examples.
Setting Up Required Config Files
The default config files contain sufficient documentation in DocBlocks. Just read those.
Database Interaction
Database Installs and Migrations
Use the console utility in API Template to perform DB installs and migrations.
- Install a clean copy of the database:
./console install:run --drop-tables
- Create a migration:
./console migrations:create "Add inventory table"
- Install any migrations that haven't been applied:
./console migrations:run
(Or just./console install:run
) - Generate a new database install file from the current state of the database:
./console install:generate
When you create a new migration, it's created in [APPDIR]/src/Application/Migrations/
. Use the Zend DB Adapter to perform queries like this.
Note about Generating a Database Install File: When you run ./console install:generate
, it generates 2 files -- (1) a DbStructure.sql
file with the table structure based on the current snapshot of your database, and (2) a DbData.sql
file with data from specific tables. Specify which tables in the install config.
How to Read/Write from the Database
Use Mappers like this to perform database queries. Database results are returned as Entities.
Authentication / Login System
bshaffer's OAuth2 server is used for authentication. The user POST
s to /oauth/token
with their email/password and receives an access token which can be used to make requests. (Per the OAuth2 specification.)
In order to secure endpoints, the Symfony Security module is used. Firewalls are used to constrain an endpoint to logged in users or to make it public. Access Rules are used to make an endpoint accessible only to users with certain roles. Read the Symfony Security docs for more details.
Notes:
- When you specify a listener in a firewall (
'anonymous' => true
,'oauth-optional' => true
), the code that runs is in the Listeners. (These are added in the OAuth2\SecurityServiceProvider.) - There is a catch-all firewall that constrains all endpoints to be protected by OAuth (non-public) unless specified otherwise. More details here.
Utility Classes
Array Helper
$people = [ ['name' => 'Linus', 'age' => 10], ['name' => 'Brendan', 'age' => 11], ['name' => 'Rasmus', 'age' => 12, 'colors' => ['Red', 'Green', 'Blue']], ]; Arr::get($people[0], 'name'); // Linus Arr::get($people[3], 'name'); // null Arr::pluck($people, 'name'); // ['Linux', 'Brendan', 'Rasmus']; Arr::path($people, '2.colors.1'); // Green
DataObject
Use these to encapsulate concepts and typehint them in your app.
class Car extends DataObject { protected $object = [ 'make' => null, 'model' => null, 'year' => null, 'totaled' => false, ]; } $car = new Car(['make' => 'Toyota']); $car->setModel('Corolla'); $car->getMake(); // Toyota $car->getModel(); // Corolla $car->getYear(); // null $car->getTotaled(); // false // These are helpful for typehinting purposes to make your code more robust function purchaseCar(Car $car) { // Do stuff }
Test Helpers
Various abstract PHPUnit test cases are available for controllers, mappers, etc., to make it easier to test them.