Aura for PHP at Fossmeet 2014
Upcoming SlideShare
Loading in...5
×
 

Aura for PHP at Fossmeet 2014

on

  • 2,376 views

Introduce aura project a collection of standalone library package for PHP

Introduce aura project a collection of standalone library package for PHP

Statistics

Views

Total Views
2,376
Views on SlideShare
2,360
Embed Views
16

Actions

Likes
0
Downloads
2
Comments
2

1 Embed 16

https://twitter.com 16

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Aura for PHP at Fossmeet 2014 Aura for PHP at Fossmeet 2014 Presentation Transcript

  • Aura for PHP Hari KT @harikt http://harikt.com FOSSMeet 2014 @ NIT Calicut
  • About me ● Freelance LAMP developer ● Open-source lover and contributor ● Writes for sitepoint.com ● Technical reviewer of Packt FOSSMeet 2014 @ NIT Calicut
  • Aura ● 2nd major version of SolarPHP ( started in 2004 by Paul M Jones) ● Independent decoupled library packages ● V1 started around late 2010 ( PSR-0 standard ) ● V2 started around late 2013 ( PSR-4 standard ) FOSSMeet 2014 @ NIT Calicut
  • PSR-0 vs PSR-4 namespace AuraRouter; class RouterFactory { } ● PSR-0 : http://www.php-fig.org/psr/psr-0/ AuraRouterRouteFactory => /path/to/src/Aura/Router/RouteFactory.php Zend_Mail_Message => /path/to/lib/Zend/Mail/Message.php ● PSR-4 : http://www.php-fig.org/psr/psr-4/ AuraRouterRouteFactory => /path/to/src/RouteFactory.php FOSSMeet 2014 @ NIT Calicut
  • Driving Principles ● ● Libraries first, framework 2nd No use of globals within packages ( $_SERVER, $_GET etc ) ● No dependency on any other package ● Tests and Assets inside the package FOSSMeet 2014 @ NIT Calicut
  • Library / Package Organization FOSSMeet 2014 @ NIT Calicut
  • Aura.Router v2 FOSSMeet 2014 @ NIT Calicut
  • Aura.Router ● ● Provides a web router implementation Given a URL path and a copy of $_SERVER, it will extract path-info parameters and $_SERVER values for a specific route FOSSMeet 2014 @ NIT Calicut
  • Instantiation require path/to/Aura.Router/autoload.php; use AuraRouterRouterFactory; $router_factory = new RouterFactory; $router = $router_factory->newInstance(); or use AuraRouterRouter; use AuraRouterRouteCollection; use AuraRouterRouteFactory; $router = new Router(new RouteCollection(new RouteFactory)); FOSSMeet 2014 @ NIT Calicut
  • Adding Route ● // add a simple unnamed route with params $router->add(null, '/{controller}/{action}/{id}'); ● // add a route with optional params $router->add('archive', '/archive{/year,month,day}') ● // add a named route with an extended specification $router->add('read', '/blog/read/{id}{format}') ->addTokens(array('id' => 'd+', 'format' => '(.[^/]+)?',)) ->addValues(array('controller' => 'BlogPage', 'action' => 'read', 'format' => '.html',)); ● // REST resource route $router->attachResource('blog', '/blog'); FOSSMeet 2014 @ NIT Calicut
  • Matching A Route // get the incoming request URL path $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); // get the route based on the path and server $route = $router->match($path, $_SERVER); FOSSMeet 2014 @ NIT Calicut
  • Example $path = '/blog/read/42.json'; $route = $router->match($path, $_SERVER); var_export($route->params); [ 'controller' => 'BlogPage', 'action' => 'read', 'id' => 42, 'format' => 'json' ] FOSSMeet 2014 @ NIT Calicut
  • Dispatching a Route if ($route) { echo "No application route was found for that URL path."; exit(); } $class = $route->params['controller']; // BlogPage $method = $route->params['action']; // read // instantiate the controller class $object = new $class(); echo $object->$method($route->params); // $object->read($route->params); FOSSMeet 2014 @ NIT Calicut
  • Microframework Route $router->add('read', '/blog/read/{id}') ->addTokens(array( 'id' => 'd+', )) ->addValues(array( 'controller' => function ($params) { $id = (int) $params['id']; header('Content-Type: application/json'); echo json_encode(['id' => $id]); }, )); FOSSMeet 2014 @ NIT Calicut
  • Microframework Dispatcher $controller = $route->params['controller']; echo $controller($route->params); FOSSMeet 2014 @ NIT Calicut
  • Generating a route path // $path => "/blog/read/42.atom" $path = $router->generate('read', array( 'id' => 42, 'format' => '.atom', )); $href = htmlspecialchars($path, ENT_QUOTES, 'UTF-8'); echo '<a href="$href">Atom feed for this blog entry</a>'; FOSSMeet 2014 @ NIT Calicut
  • Aura.Filter v1 FOSSMeet 2014 @ NIT Calicut
  • Instantiation $filter = require "/path/to/Aura.Filter/scripts/instance.php"; or use AuraFilterRuleCollection as Filter; use AuraFilterRuleLocator; use AuraFilterTranslator; $filter = new Filter( new RuleLocator, new Translator(require 'path/to/Aura.Filter/intl/en_US.php') ); FOSSMeet 2014 @ NIT Calicut
  • Rule Types ● ● ● Soft : if the rule fails, the filter will keep applying all remaining rules to that field and all other fields Hard : if the rule fails, the filter will not apply any more rules to that field, but it will keep filtering other fields. Stop : if the rule fails, the filter will not apply any more filters to any more fields; this stops all filtering on the data object. FOSSMeet 2014 @ NIT Calicut
  • Aura.Filter $filter->addSoftRule('username', $filter::IS, 'alnum'); $filter->addSoftRule('username', $filter::IS, 'strlenBetween', 6, 12); $filter->addSoftRule('username', $filter::FIX, 'string'); $filter->addSoftRule('password', $filter::IS, 'strlenMin', 6); $filter->addSoftRule('password_confirm', $filter::IS, 'equalToField', 'password'); $data = [ 'username' => 'bolivar', 'password' => 'p@55w0rd', 'password_confirm' => 'p@55word', // not the same!]; $success = $filter->values($data); if (! $success) { $messages = $filter->getMessages(); } FOSSMeet 2014 @ NIT Calicut
  • Validating and Sanitizing ● ● ● ● ● RuleCollection::IS means the field value must match the rule. RuleCollection::IS_NOT means the field value must not match the rule. RuleCollection::IS_BLANK_OR means the field value must either be blank, or match the rule. RuleCollection::FIX to force the field value to comply with the rule; this may forcibly transform the value. RuleCollection::FIX_BLANK_OR will convert blank values to null; non-blank fields will be forced to comply with the rule. FOSSMeet 2014 @ NIT Calicut
  • Available Rules alnum alpha between blank creditCard dateTime email equalToField equalToValue float inKeys inValues int ipv4 locale max min regex strictEqualToF strictEqualToV ield alue string strlen strlenBetween strlenMax strlenMin trim upload url isbn any all word FOSSMeet 2014 @ NIT Calicut
  • Custom Rules ● Write a rule class ● Set that class as a service in the RuleLocator ● Use the new rule in our filter chain FOSSMeet 2014 @ NIT Calicut
  • Rule Class ● ● ● ● ● Extend AuraFilterAbstractRule with two methods: validate() and sanitize() Add params as needed to each method. Each method should return a boolean: true on success, or false on failure. Use getValue() to get the value being validated, and setValue() to change the value being sanitized. Add a property $message to indicate a string that should be translated as a message when validation or sanitizing fails. FOSSMeet 2014 @ NIT Calicut
  • Example use AuraFilterAbstractRule; class Hex extends AbstractRule { protected $message; public function validate($max = null) { $value = $this->getValue(); } public function sanitize($max = null) { $this->setValue('Hello'); // some code } } $locator = $filter->getRuleLocator(); $locator->set('hex', function () { return new Hex; }); FOSSMeet 2014 @ NIT Calicut
  • Questions ? ● Hire me to coach you PHP, Aura Hari KT harikt.com https://github.com/harikt FOSSMeet 2014 @ NIT Calicut
  • more library / framework Aura.Cli Aura.Di Aura.Includer Aura.Input Aura.Web Aura.Di Aura.Dispatcher Aura. Sql Aura.View Aura.Html Aura.Http Aura.Intl Aura.Router Aura.Signal Aura.Filter Aura.Session Aura.Sql_Query Aura.Sql_Schema Aura.Marshal Aura.Autoload Aura.Web_Project Aura.Framework Aura.Framework_Project Aura.Project_Kernel Aura.Web_Kernel Aura.Web_Project Aura.Cli_Kernel Aura.Cli_Project ..... FOSSMeet 2014 @ NIT Calicut
  • PHP User Group in Calicut ● ● ● Plan to organize a user group meetup once in a month Looking for space and audience Join https://groups.google.com/forum/#! forum/php-clt FOSSMeet 2014 @ NIT Calicut
  • Thanks ● Website : http://auraphp.com/ ● Source Code : http://github.com/auraphp ● ● ● ● Groups : https://groups.google.com/d/forum/auraphp Paul M Jones : http://paul-m-jones.com/ Chris Tankersley http://www.slideshare.net/ctankersley/dtyu SolarPHP : http://solarphp.com/ FOSSMeet 2014 @ NIT Calicut