Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Getting Started with
Aura
Chris Tankersley
NomadPHP EU June 2015
NomadPHP EU, June 2015 1
Who Am I
• PHP Programmer for over 10 years
• Work/know a lot of different
languages, even COBOL
• Primarily do Zend Frame...
Professional Tools for Professional
Applications
NomadPHP EU, June 2015 3
About Aura
• Started by Paul M. Jones
• Maintained by him and the community
• Originally a rewrite of Solar (not Solr)
• T...
Requirements
• PHP 5.3 or PHP 5.4, depending on the package
NomadPHP EU, June 2015 5
Each Package is Self Contained
NomadPHP EU, June 2015 6
http://looselycoupled.info/
Getting Started
NomadPHP EU, June 2015 7
2 Steps
• Find a package you want on https://github.com/auraphp
• Install via Composer
NomadPHP EU, June 2015 8
Winning was never so easy!
NomadPHP EU, June 2015 9
Aura is built to use whatever, whenever
• Aura.Filter
• Aura.Router
• Aura.Sql
• Aura.SqlQuery
• Aura.Di
• Aura.Http
• Aur...
Or go all in
• Aura.SqlMapper_Bundle
• Aura.Framework_Project
• Aura.Web_Project
• Aura.Web_Kernel
• Aura.Cli_Project
• Au...
Most Common Packages
NomadPHP EU, June 2015 12
Aura.Di
NomadPHP EU, June 2015 13
Dependency Injection, Not Service
Location
• Provides a way to lazily create services
• Interface for providing Constructo...
NomadPHP EU, June 2015 15
use AuraDiContainer as DiContainer;
use AuraDiFactory as DiFactory;
$di = new DiContainer(new Di...
Aura.Router
NomadPHP EU, June 2015 16
Almost like a micro-framework
• Provides a way to match a route to a chunk of code
• Can handle different HTTP verbs
• Can...
Setting up a Route
NomadPHP EU, June 2015 18
$router_factory = new AuraRouterRouterFactory();
$router = $router_factory->n...
Dispatching
NomadPHP EU, June 2015 19
$app = function ($request, $response) use($di, $router) {
$server['REQUEST_METHOD'] ...
Aura.Dispatcher
NomadPHP EU, June 2015 20
It’s an Object Mapper
• Maps names to objects, and then invokes them in some way
• Will continue to dispatch until there i...
NomadPHP EU, June 2015 22
$dispatcher = new AuraDispatcherDispatcher();
$dispatcher->setObjectParam('controller');
$dispat...
Aura.SqlQuery
NomadPHP EU, June 2015 23
Fluent Query Builder
• Generates SQL for MySQL, Postgres, SQLite, and SQL Server
• Does not require a DB connection
• Sele...
NomadPHP EU, June 2015 25
protected function buildSelectQuery($table, $criteria = array(), $cols = array('*'), $order = 'i...
NomadPHP EU, June 2015 26
/**
* Returns a single result based on the criteria
* Criteria must be an array, with the DB col...
Aura.Sql
NomadPHP EU, June 2015 27
A Better Wrapper around PDO
• Lazy connections to your DB
• Allows you to quote arrays of strings
• Prepare, bind and fetc...
NomadPHP EU, June 2015 29
use AuraDiContainer as DiContainer;
use AuraDiFactory as DiFactory;
$di = new DiContainer(new Di...
NomadPHP EU, June 2015 30
public function delete($criteria, $table)
{
$delete = $this->queryHandler->newDelete();
$delete-...
Aura.Cli
NomadPHP EU, June 2015 31
Very nice wrapper around the CLI
• Wraps the request and response for CLI interfaces
• Supports options and help
NomadPHP ...
Setup
NomadPHP EU, June 2015 33
use AuraCliCliFactory;
$cli_factory = new CliFactory();
$context = $cli_factory->newContex...
NomadPHP EU, June 2015 34
public function createAction()
{
$opts = $this->context->getopt(array('name:', 'repo:', 'branch:...
Aura.Filter
NomadPHP EU, June 2015 35
Better Filtering and Sanitization
• Provides a better interface for filtering than the Filter module
• Ships with a ton of...
NomadPHP EU, June 2015 37
NomadPHP EU, June 2015 38
Bundles, Kernels, and Projects
NomadPHP EU, June 2015 39
Using modules as building blocks
• Bundles are larger-weight versions of the individual packages
• Kernels are ‘core’ logi...
Aura.SqlMapper_Bundle
• Use at your own risk
• Links together Aura.Sql and Aura.SqlQuery into a mapper pattern
NomadPHP EU...
Project Kernel, Web Kernel, CLI Kernel
• Provides the core setup for the Web and CLI projects
• Mostly config and classes....
Web Project, CLI Project
• Full stack project skeletons for those project types
• They pull in a specific Kernel, and boot...
Web Project
• Web Project follows Action Domain Responder instead of the normal
MVC
• Can be used as a Micro Framework or ...
Questions?
NomadPHP EU, June 2015 45
Thank You!
http://ctankersley.com
chris@ctankersley.com
@dragonmantank
https://joind.in/14632
NomadPHP EU, June 2015 46
Notes
• Code is taken from:
• https://github.com/dragonmantank/primus
• https://github.com/dragonmantank/casket
• All code...
Upcoming SlideShare
Loading in …5
×

Getting Started With Aura

719 views

Published on

PHP has come a long way since it’s inception. What started out as an ecosystem of functions passed around on the internet eventually turned into a land of frameworks. These first frameworks were saddled with the problem of size, where if you wanted to use part of Zend Framework 1, for example, in your app, you pulled in the entire thing. Those days are gone now and frameworks are increasingly decoupled, but none so much as the Aura project. We’ll dive into what Aura is and how we can use it as standalone libraries as well as for full stack development.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Getting Started With Aura

  1. 1. Getting Started with Aura Chris Tankersley NomadPHP EU June 2015 NomadPHP EU, June 2015 1
  2. 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 NomadPHP EU, June 2015 2
  3. 3. Professional Tools for Professional Applications NomadPHP EU, June 2015 3
  4. 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 NomadPHP EU, June 2015 4
  5. 5. Requirements • PHP 5.3 or PHP 5.4, depending on the package NomadPHP EU, June 2015 5
  6. 6. Each Package is Self Contained NomadPHP EU, June 2015 6 http://looselycoupled.info/
  7. 7. Getting Started NomadPHP EU, June 2015 7
  8. 8. 2 Steps • Find a package you want on https://github.com/auraphp • Install via Composer NomadPHP EU, June 2015 8
  9. 9. Winning was never so easy! NomadPHP EU, June 2015 9
  10. 10. Aura is built to use whatever, whenever • Aura.Filter • Aura.Router • Aura.Sql • Aura.SqlQuery • Aura.Di • Aura.Http • Aura.Html • Aura.Session NomadPHP EU, June 2015 10 • Aura.Cli • Aura.View • Aura.Intl • Aura.Input • Aura.Includer • Aura.Dispatcher • Aura.Auth
  11. 11. Or go all in • Aura.SqlMapper_Bundle • Aura.Framework_Project • Aura.Web_Project • Aura.Web_Kernel • Aura.Cli_Project • Aura.Cli_Kernel NomadPHP EU, June 2015 11
  12. 12. Most Common Packages NomadPHP EU, June 2015 12
  13. 13. Aura.Di NomadPHP EU, June 2015 13
  14. 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 NomadPHP EU, June 2015 14
  15. 15. NomadPHP EU, June 2015 15 use AuraDiContainer as DiContainer; use AuraDiFactory as DiFactory; $di = new DiContainer(new DiFactory()); $di->set('database.handler', function() use ($di) { $config = $di->get('config'); return new AuraSqlExtendedPdo( 'mysql:host='.$config['db']['host'].';dbname='.$config['db']['dbname'], $config['db']['username'], $config['db']['password'] ); }); $di->params['AuraSqlQueryQueryFactory'] = array( 'db' => 'mysql' ); $di->set('database.query_handler', $di->lazyNew('AuraSqlQueryQueryFactory'));
  16. 16. Aura.Router NomadPHP EU, June 2015 16
  17. 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 NomadPHP EU, June 2015 17
  18. 18. Setting up a Route NomadPHP EU, June 2015 18 $router_factory = new AuraRouterRouterFactory(); $router = $router_factory->newInstance(); $router ->addPost('bitbucket_webhook', '/api/v0/bitbucket/webhook/{token}') ->addTokens([ 'token' => 'w+', ]) ->addValues(array( 'controller' => 'bitbucket', 'action' => 'webhook', )) ;
  19. 19. Dispatching NomadPHP EU, June 2015 19 $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 = ‘MyAppController'.ucfirst($route->params['controller']).'Controller'; $actionName = $route->params['action'].'Action'; $controller = new $controllerName($request, $response, $di); $controller->$actionName(); } };
  20. 20. Aura.Dispatcher NomadPHP EU, June 2015 20
  21. 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 NomadPHP EU, June 2015 21
  22. 22. NomadPHP EU, June 2015 22 $dispatcher = new AuraDispatcherDispatcher(); $dispatcher->setObjectParam('controller'); $dispatcher->setMethodParam('action'); $dispatcher->setObject('projects', $di->lazyNew('PrimusAppCommandProjectsCommand')); $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'));
  23. 23. Aura.SqlQuery NomadPHP EU, June 2015 23
  24. 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 NomadPHP EU, June 2015 24
  25. 25. 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); } }
  26. 26. 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()); }
  27. 27. Aura.Sql NomadPHP EU, June 2015 27
  28. 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 NomadPHP EU, June 2015 28
  29. 29. NomadPHP EU, June 2015 29 use AuraDiContainer as DiContainer; use AuraDiFactory as DiFactory; $di = new DiContainer(new DiFactory()); $di->set('database.handler', function() use ($di) { $config = $di->get('config'); return new AuraSqlExtendedPdo( 'mysql:host='.$config['db']['host'].';dbname='.$config['db']['dbname'], $config['db']['username'], $config['db']['password'] ); }); $di->params['AuraSqlQueryQueryFactory'] = array( 'db' => 'mysql' ); $di->set('database.query_handler', $di->lazyNew('AuraSqlQueryQueryFactory'));
  30. 30. 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()); }
  31. 31. Aura.Cli NomadPHP EU, June 2015 31
  32. 32. Very nice wrapper around the CLI • Wraps the request and response for CLI interfaces • Supports options and help NomadPHP EU, June 2015 32
  33. 33. Setup NomadPHP EU, June 2015 33 use AuraCliCliFactory; $cli_factory = new CliFactory(); $context = $cli_factory->newContext($GLOBALS); $cli_stdio = $cli_factory->newStdio();
  34. 34. 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'); } }
  35. 35. Aura.Filter NomadPHP EU, June 2015 35
  36. 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 NomadPHP EU, June 2015 36
  37. 37. NomadPHP EU, June 2015 37
  38. 38. NomadPHP EU, June 2015 38
  39. 39. Bundles, Kernels, and Projects NomadPHP EU, June 2015 39
  40. 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 NomadPHP EU, June 2015 40
  41. 41. Aura.SqlMapper_Bundle • Use at your own risk • Links together Aura.Sql and Aura.SqlQuery into a mapper pattern NomadPHP EU, June 2015 41
  42. 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 NomadPHP EU, June 2015 42
  43. 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 NomadPHP EU, June 2015 43
  44. 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 NomadPHP EU, June 2015 44
  45. 45. Questions? NomadPHP EU, June 2015 45
  46. 46. Thank You! http://ctankersley.com chris@ctankersley.com @dragonmantank https://joind.in/14632 NomadPHP EU, June 2015 46
  47. 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 NomadPHP EU, June 2015 47

×