Getting Started With Aura

47
Getting Started with Aura Chris Tankersley NomadPHP EU June 2015 NomadPHP EU, June 2015 1

Transcript of Getting Started With Aura

Page 1: Getting Started With Aura

NomadPHP EU, June 2015 1

Getting Started with AuraChris TankersleyNomadPHP EU June 2015

Page 2: Getting Started With Aura

NomadPHP EU, June 2015 2

Who Am I

• PHP Programmer for over 10 years• Work/know a lot of different

languages, even COBOL• Primarily do Zend Framework 2• https://github.com/dragonmantank

Page 3: Getting Started With Aura

NomadPHP EU, June 2015 3

Professional Tools for Professional Applications

Page 4: Getting Started With Aura

NomadPHP EU, June 2015 4

About Aura

• Started by Paul M. Jones• Maintained by him and the community• Originally a rewrite of Solar (not Solr)• There are three versions, 1.x, 2.x, and a 3.x!• Works well by themselves, or in supplied Bundles/Kernels/Projects

Page 5: Getting Started With Aura

NomadPHP EU, June 2015 5

Requirements

• PHP 5.3 or PHP 5.4, depending on the package

Page 6: Getting Started With Aura

NomadPHP EU, June 2015 6

Each Package is Self Contained

http://looselycoupled.info/

Page 7: Getting Started With Aura

NomadPHP EU, June 2015 7

Getting Started

Page 8: Getting Started With Aura

NomadPHP EU, June 2015 8

2 Steps

• Find a package you want on https://github.com/auraphp• Install via Composer

Page 9: Getting Started With Aura

NomadPHP EU, June 2015 9

Winning was never so easy!

Page 10: Getting Started With Aura

NomadPHP EU, June 2015 10

Aura is built to use whatever, whenever

• Aura.Filter• Aura.Router• Aura.Sql• Aura.SqlQuery• Aura.Di• Aura.Http• Aura.Html• Aura.Session

• Aura.Cli• Aura.View• Aura.Intl• Aura.Input• Aura.Includer• Aura.Dispatcher• Aura.Auth

Page 11: Getting Started With Aura

NomadPHP EU, June 2015 11

Or go all in

• Aura.SqlMapper_Bundle• Aura.Framework_Project• Aura.Web_Project• Aura.Web_Kernel• Aura.Cli_Project• Aura.Cli_Kernel

Page 12: Getting Started With Aura

NomadPHP EU, June 2015 12

Most Common Packages

Page 13: Getting Started With Aura

NomadPHP EU, June 2015 13

Aura.Di

Page 14: Getting Started With Aura

NomadPHP EU, June 2015 14

Dependency Injection, Not Service Location• Provides a way to lazily create services• Interface for providing Constructor and Setter Injection• Can provide auto resolution to injection, you should probably turn it

off though

Page 15: Getting Started With Aura

NomadPHP EU, June 2015 15

use Aura\Di\Container as DiContainer;use Aura\Di\Factory as DiFactory;

$di = new DiContainer(new DiFactory());

$di->set('database.handler', function() use ($di) { $config = $di->get('config'); return new \Aura\Sql\ExtendedPdo( 'mysql:host='.$config['db']['host'].';dbname='.$config['db']['dbname'], $config['db']['username'], $config['db']['password'] );});

$di->params['Aura\SqlQuery\QueryFactory'] = array( 'db' => 'mysql');

$di->set('database.query_handler', $di->lazyNew('Aura\SqlQuery\QueryFactory'));

Page 16: Getting Started With Aura

NomadPHP EU, June 2015 16

Aura.Router

Page 17: Getting Started With Aura

NomadPHP EU, June 2015 17

Almost like a micro-framework

• Provides a way to match a route to a chunk of code• Can handle different HTTP verbs• Can filter parameters based on regex• Can set default parameters• Generate URLs from the attached routes

Page 18: Getting Started With Aura

NomadPHP EU, June 2015 18

Setting up a Route

$router_factory = new Aura\Router\RouterFactory();$router = $router_factory->newInstance();$router ->addPost('bitbucket_webhook', '/api/v0/bitbucket/webhook/{token}') ->addTokens([ 'token' => '\w+', ]) ->addValues(array( 'controller' => 'bitbucket', 'action' => 'webhook', ));

Page 19: Getting Started With Aura

NomadPHP EU, June 2015 19

Dispatching

$app = function ($request, $response) use($di, $router) { $server['REQUEST_METHOD'] = $request->getMethod(); $route = $router->match($request->getPath(), $server); if(!$route) { echo sprintf('[%s] %s 404', date('Y-m-d H:i:s'), $request->getPath()).PHP_EOL; $response->writeHead(404, array('Content-Type' => 'text/plain')); $response->end('404'); return; } else { $controllerName = ‘My\\App\\Controller\\'.ucfirst($route->params['controller']).'Controller'; $actionName = $route->params['action'].'Action'; $controller = new $controllerName($request, $response, $di); $controller->$actionName(); }};

Page 20: Getting Started With Aura

NomadPHP EU, June 2015 20

Aura.Dispatcher

Page 21: Getting Started With Aura

NomadPHP EU, June 2015 21

It’s an Object Mapper

• Maps names to objects, and then invokes them in some way• Will continue to dispatch until there is nothing left to do

Page 22: Getting Started With Aura

NomadPHP EU, June 2015 22

$dispatcher = new Aura\Dispatcher\Dispatcher();

$dispatcher->setObjectParam('controller');$dispatcher->setMethodParam('action');$dispatcher->setObject('projects', $di->lazyNew('Primus\App\Command\ProjectsCommand'));

$argv = $context->argv->get();if(count($argv) < 2) { $stdio = $di->get('cli.stdio'); $stdio->outln('Please enter a commmand to run'); die();}$controller = $argv[1];if(isset($argv[2])) { $args = $context->argv->get(); $action = $args[2].'Action';} else { $action = 'indexAction';}$dispatcher->__invoke(compact('controller', 'action'));

Page 23: Getting Started With Aura

NomadPHP EU, June 2015 23

Aura.SqlQuery

Page 24: Getting Started With Aura

NomadPHP EU, June 2015 24

Fluent Query Builder

• Generates SQL for MySQL, Postgres, SQLite, and SQL Server• Does not require a DB connection• Select, Insert, Update, and Delete builders are available

Page 25: Getting Started With Aura

NomadPHP EU, June 2015 25

protected function buildSelectQuery($table, $criteria = array(), $cols = array('*'), $order = 'id ASC') { $select = $this->queryHandler->newSelect(); $select ->cols($cols) ->from($table) ->orderBy(array($order)) ; foreach($criteria as $column => $value) { $select->where($column.' = :'.$column); } if(!empty($criteria)) { $select->bindValues($criteria); } return $select;}

public function save($data, $table, $identifierColumn = 'id') { $data = $this->convertToArray($data); if(!empty($data[$identifierColumn])) { // Removed for brevity } else { $insert = $this->queryHandler->newInsert(); $insert ->into($table) ->cols(array_keys($data)) ->bindValues($data) ; $this->db->perform($insert->__toString(), $insert->getBindValues()); $name = $insert->getLastInsertIdName($identifierColumn); return $this->db->lastInsertId($name); }}

Page 26: Getting Started With Aura

NomadPHP EU, June 2015 26

/** * Returns a single result based on the criteria * Criteria must be an array, with the DB column the key and the DB value the value * * @param array $criteria Search criteria * @param string $table Table to search against * @return array */public function find($criteria, $table){ $select = $this->buildSelectQuery($table, $criteria); return $this->db->fetchOne($select->__toString(), $select->getBindValues());}

Page 27: Getting Started With Aura

NomadPHP EU, June 2015 27

Aura.Sql

Page 28: Getting Started With Aura

NomadPHP EU, June 2015 28

A Better Wrapper around PDO

• Lazy connections to your DB• Allows you to quote arrays of strings• Prepare, bind and fetch in one step• SQL Profiler to see what went on during a query

Page 29: Getting Started With Aura

NomadPHP EU, June 2015 29

use Aura\Di\Container as DiContainer;use Aura\Di\Factory as DiFactory;

$di = new DiContainer(new DiFactory());

$di->set('database.handler', function() use ($di) { $config = $di->get('config'); return new \Aura\Sql\ExtendedPdo( 'mysql:host='.$config['db']['host'].';dbname='.$config['db']['dbname'], $config['db']['username'], $config['db']['password'] );});

$di->params['Aura\SqlQuery\QueryFactory'] = array( 'db' => 'mysql');

$di->set('database.query_handler', $di->lazyNew('Aura\SqlQuery\QueryFactory'));

Page 30: Getting Started With Aura

NomadPHP EU, June 2015 30

public function delete($criteria, $table){ $delete = $this->queryHandler->newDelete(); $delete->from($table); foreach($criteria as $col => $value) { $delete->where($col.' = :'.$col); } $delete->bindValues($criteria);

return $this->db->perform($delete->__toString(), $delete->getBindValues());}

public function fetchAllBy($criteria, $table, $order = 'id ASC'){ $select = $this->buildSelectQuery($table, $criteria, array('*'), $order); return $this->db->fetchAll($select->__toString(), $select->getBindValues());}

Page 31: Getting Started With Aura

NomadPHP EU, June 2015 31

Aura.Cli

Page 32: Getting Started With Aura

NomadPHP EU, June 2015 32

Very nice wrapper around the CLI

• Wraps the request and response for CLI interfaces• Supports options and help

Page 33: Getting Started With Aura

NomadPHP EU, June 2015 33

Setup

use Aura\Cli\CliFactory;

$cli_factory = new CliFactory();$context = $cli_factory->newContext($GLOBALS);$cli_stdio = $cli_factory->newStdio();

Page 34: Getting Started With Aura

NomadPHP EU, June 2015 34

public function createAction(){ $opts = $this->context->getopt(array('name:', 'repo:', 'branch:', 'deploy-path:')); // ... $name = $opts->get('--name'); $repo = $opts->get('--repo'); while (empty($name)) { $stdio->out('Please enter the name of this project: '); $name = $stdio->in(); } // ...}

public function deployAction(){ $args = $this->context->argv->get(); $project = $this->projectService->fetchProject($args[3]); if($project && $project->active) { $deploymentService = $this->di->get('service.deployment'); $deploymentService->deploy($project); $this->stdio->outln('Deployed'); } else { $this->stdio->outln('There is no project by that name'); }}

Page 35: Getting Started With Aura

NomadPHP EU, June 2015 35

Aura.Filter

Page 36: Getting Started With Aura

NomadPHP EU, June 2015 36

Better Filtering and Sanitization

• Provides a better interface for filtering than the Filter module• Ships with a ton of filters and validators• Allows chains of commands for working with data• Provides soft, hard, and full stop validation chains

Page 37: Getting Started With Aura

NomadPHP EU, June 2015 37

Page 38: Getting Started With Aura

NomadPHP EU, June 2015 38

Page 39: Getting Started With Aura

NomadPHP EU, June 2015 39

Bundles, Kernels, and Projects

Page 40: Getting Started With Aura

NomadPHP EU, June 2015 40

Using modules as building blocks

• Bundles are larger-weight versions of the individual packages• Kernels are ‘core’ logic for projects• Projects are more like using ZF2 or Symfony 2 as full stacks

Page 41: Getting Started With Aura

NomadPHP EU, June 2015 41

Aura.SqlMapper_Bundle

• Use at your own risk• Links together Aura.Sql and Aura.SqlQuery into a mapper pattern

Page 42: Getting Started With Aura

NomadPHP EU, June 2015 42

Project Kernel, Web Kernel, CLI Kernel• Provides the core setup for the Web and CLI projects• Mostly config and classes. Still needs wired up to work fully• You can create your own custom kernels by basing it off Project Kernel

Page 43: Getting Started With Aura

NomadPHP EU, June 2015 43

Web Project, CLI Project

• Full stack project skeletons for those project types• They pull in a specific Kernel, and bootstrap from there• Make heavy use of Aura.Di, so make sure you know how to use it

Page 44: Getting Started With Aura

NomadPHP EU, June 2015 44

Web Project

• Web Project follows Action Domain Responder instead of the normal MVC• Can be used as a Micro Framework or as a Full Stack

Page 45: Getting Started With Aura

NomadPHP EU, June 2015 45

Questions?

Page 46: Getting Started With Aura

NomadPHP EU, June 2015 46

Thank You!

http://[email protected]

@dragonmantank

https://joind.in/14632

Page 47: Getting Started With Aura

NomadPHP EU, June 2015 47

Notes

• Code is taken from:• https://github.com/dragonmantank/primus• https://github.com/dragonmantank/casket

• All code in this presentation is available under the MIT license