View
4.574
Download
6
Category
Preview:
DESCRIPTION
Apresentação realizada no TDC 2013 SP na Trilha PHP onde apresento o Framework Phalcon, Benchmarks e a arquitetura Peregrinus que estamos criando na ingresse.com
Citation preview
Criando Webservices ultra rápidoscom PHP Phalcon e MongoDB
terça-feira, 16 de julho de 13
Luiz Gustavo Gavinho‣ Manaus - Amazonas
‣ +14 anos de experiência
‣ Gerente de Projetos (PMP)
‣ Desenvolvedor de Software.
‣ Bacharel em Informática (ULBRA) e MBA em Administração de Empresas e Negócios (FGV).
CTO na Ingresse, ingressos sociais
BDM na FPF Tech, Centro de Inovação
Tecnológica
terça-feira, 16 de julho de 13
terça-feira, 16 de julho de 13
terça-feira, 16 de julho de 13
Por quê?
1. Performance e escala (picos de acesso)
2. Multi-telas (Web, Mobile, TV, Redes Sociais)
3. Parceiros (publicidade, vendas, guias, blogs)
terça-feira, 16 de julho de 13
Solução
Web JS App
Webservice
Mobile App
MongoDB MySQL
Repositório de Dados
CDN
terça-feira, 16 de julho de 13
PHP Phalcon
terça-feira, 16 de julho de 13
• Phalcon é um framework PHP MVC completo open-source.
• Implementado em C como uma extensão no PHP.
PHP Phalcon
http://phalconphp.com/terça-feira, 16 de julho de 13
• Full MVC Applications (Single-Module, Multi-Module e Micro Applications)
• ORM (Transactions, Relations, Validations, Sharding, Hydration, etc)
• ODM para Mongo
• Template Engine (Volt)
• DI
• Events Managements
• Encryption
• HTTP Request/Response, Cookie
• Escaping/Filtering
• Forms Builder/Validation
• Flash Messagens
• Cache
• Pagination
• Annotations
• Security
• Translations
• Assets Managements
• Universal Auto-loader
• Logging
• CLI
PHP Phalcon
Componentes
terça-feira, 16 de julho de 13
• Muito bem documentadohttp://docs.phalconphp.com/en/latest/index.html
• Forum ativo: http://forum.phalconphp.com/
• Github: https://github.com/phalcon/cphalcon
• Blog: http://blog.phalconphp.com/
• Twitter: @phalconphp
• Webcasts: http://vimeo.com/phalconphp
PHP Phalcon
Será?
terça-feira, 16 de julho de 13
PHP Phalcon
Github Pulse
terça-feira, 16 de julho de 13
Benchmark
terça-feira, 16 de julho de 13
Benchmark 1
http://docs.phalconphp.com/en/latest/reference/benchmark/hello-world.html
2.5xmais requisições
que o 2o colocadoCodeIgniter
PHP Phalcon
terça-feira, 16 de julho de 13
Benchmark 2
http://docs.phalconphp.com/en/latest/reference/benchmark/micro.html
2.5xmais requisições
que o 2o colocadoSlim
PHP Phalcon
terça-feira, 16 de julho de 13
Benchmark 3
http://systemsarchitect.net/performance-benchmark-of-popular-php-frameworks/
2xmais requisições
que o 2o colocadoSlim
PHP Phalcon
terça-feira, 16 de julho de 13
Outro Benchmark
• Base de dados: mais 5.000 eventos
• Apache Benchmark (ab)
• Cenários:
1. Yii + MongoDB
2. Phalcon + MongoDB
3. Zend 1 + Doctrine 1 + MySQL
terça-feira, 16 de julho de 13
Benchmark 4
~2xmais requisições
que o 2o colocado
PHP Phalcon
Phalcon
Yii
Zend*
0 1 2 3 4
Requisições por segundo
* Zend com MySQL
Yii
terça-feira, 16 de julho de 13
Peregrinusalfa
terça-feira, 16 de julho de 13
‣Arquitetura
‣Framework de Webservice REST/JSON integrado para Phalcon.
Bootstrap
Controller
Service
Dao
Model
MongoDB MySQL
Peregrinusalfa
terça-feira, 16 de julho de 13
Bootstrap (index.php)
• Inicia Micro Application
• Definição das Rotas
• Conexões com banco de dados
• Mapeamento de pastas dos Namespaces
• Demais inicializações
Pere
grin
usal
fa
terça-feira, 16 de julho de 13
$loader = new \Phalcon\Loader(); $loader->registerNamespaces(array( 'Peregrinus' => 'library/framework/', 'Peregrinus\Error' => 'library/framework/', 'Ingresse\Model' => 'app/model/', 'Ingresse\Controller' => 'app/controller/', 'Ingresse\Library' => 'app/library/', 'Ingresse\Service' => 'app/service/', 'Ingresse\Dao' => 'app/dao/'));$loader->register();
terça-feira, 16 de julho de 13
$di = new \Phalcon\DI\FactoryDefault();$di->set(SQLCONNECTION,function(){ $conn = new \Phalcon\Db\Adapter\Pdo\Mysql(array( "host" => "localhost", "username" => "root", "password" => "root", "dbname" => DBNAME_SQL, )); return $conn;});$di->set(NOSQLCONNECTION, function() { $mongo = new Mongo(); return $mongo->selectDb(DBNAME_NOSQL);}, true); $di->set('collectionManager', function(){ return new Phalcon\Mvc\Collection\Manager();}, true);
terça-feira, 16 de julho de 13
$app->get('/event', function () use ($app) { $eventController = new \Ingresse\Controller\EventController($app); echo $eventController->run(); });
terça-feira, 16 de julho de 13
Controller
• Coleta dos parâmetros do comando REST
• Chamada do Service(Dao)
Pere
grin
usal
fa
terça-feira, 16 de julho de 13
class EventController extends \Peregrinus\BaseController { public function run($action = '') { parent::run($action); $this->searchEvents(); return $this->getJSON(); }
terça-feira, 16 de julho de 13
public function searchEvents() { //process params $page = $this->getPage(); $pageSize = $this->getPageSize(); $state = $this->getState(); $fieldset = $this->getFields(); $location = $this->getLocation(); $dates = $this->getDates(); $eventDao = new \Ingresse\Dao\EventDao(); $eventService = new \Ingresse\Service\EventService(); $eventService->setEventDao($eventDao); $pack = $eventService->search($page,$pageSize,$fieldset,$location,$state,$dates); $this->_pack = array( 'status' => true, 'data' => $pack['data'], 'paginationInfo' => $pack['info'] ); }
terça-feira, 16 de julho de 13
Service
• Validação dos parâmetros
• Processamento dos dados
• Processamento de regras de negócio
Pere
grin
usal
fa
terça-feira, 16 de julho de 13
public function search($page,$pageSize,$fieldset,$location,$state,$dates) { $eventDao = $this->_eventDao; $pack = array(); if (isset($eventDao)) { //validate params $state = $this->validateStateParam($state); $location = $this->validateLocationParam($location); $dates = $this->validateDatesParam($dates); $fieldset = $this->validateFieldsParam($fieldset); //set params $eventDao->setFieldset($fieldset); $eventDao->setPageSize($pageSize); $eventDao->addLocationFilter($location, $state); $eventDao->addDatesFilter($dates); //get data $pack = $eventDao->getPage($page); } return $pack; }
terça-feira, 16 de julho de 13
private function validateStateParam($plainParam) { if ($plainParam != '') { $pattern = '/^[a-z]{2}$/'; if (!preg_match($pattern, $plainParam)) { throw new Peregrinus_Exception('Invalid [state] parameter format. The parameter must be with two lowercase letters. Examples: am, sp, rj.'); } return strtoupper($plainParam); } else { return null; } }
terça-feira, 16 de julho de 13
DAO
• Comunicação com o banco de dados
• Definição de parâmetros
• Construção das queries e comandos, seja Mongo ou SQL.
• Cache*
Pere
grin
usal
fa
terça-feira, 16 de julho de 13
public function getPage($page) { $match = $this->_conditions; $project = array(); $group = array();...
if (count($match) > 0) { $_options[] = array('$match' => $match); } $_options[] = array('$project' => $project); $_options[] = array('$unwind' => $unw); $_options[] = array('$group' => $group); $_options[] = array('$sort' => $sort);...
$agg = $this->_model->aggregate($_options);
$rows = $this->transformAggregationResultToSimpleArray($agg['result']); $fieldset = $this->_fieldset;
$data = $this->processDataToSend($rows,$fieldset); ...
terça-feira, 16 de julho de 13
Model• Mapeamento do banco de dados
• Múltiplas conexões (MySQL e MongoDB)
• Uso de Namespaces
• Phalcon ODM:
• Mapeamento das coleções
• Regras de integridade e validação de dados
• Phalcon ORM:
• Mapeamento das tabelas e relacionamentos
Pere
grin
usal
fa
terça-feira, 16 de julho de 13
namespace Ingresse\Model;
class Event extends \Phalcon\Mvc\Collection { public function getSource() { return 'event'; //collection name } public function initialize() { $this->setConnectionService(NOSQLCONNECTION); }}
terça-feira, 16 de julho de 13
Padrão de Documentação
event:{ id, title, description, type, status, saleEnabled,
link,date{list of event dates},
planner{default set Planner}, addedBy{default set User}, category{list of Tags}, venue{full set Venue}, rsvp{list of, at maximum 5 Users in Ingresse list}, aiddp, formalName, cnpj, cpf, cityNumber,
private, totalTicketsSold, totalTickets, totalRevenue, totalIngresseTax, totalMoipTax, totalProfit, tax, taxToCostumer, creationDate, modificationDate}
Public informationPrivate - can only be requested by event owner and admins
Entidades
Pere
grin
usal
fa
terça-feira, 16 de julho de 13
Padrão de DocumentaçãoConjunto de conteúdos
Set Properties
Defaultid, title, description (200 chars), date, type, link, venue
Pere
grin
usal
fa
terça-feira, 16 de julho de 13
Padrão de Documentação
Action
Particular Params
List all events ordered by date from past to future
lat (none): requires long (higher priority than the state*)long(none): requires lat (higher priority than the state*)from(present date)to(none)state(none): valid state code. Ex.: am for "Amazonas"term(none): text to be used to search for tags, titles, description, link encoded in URI format - see RFC-3986.venues names
Exemplo
/event/?term=Migration%20Party&state=spBrings all events who has term words in any context and are in São Paulo.
Comandos
Pere
grin
usal
fa
terça-feira, 16 de julho de 13
PeregrinusPhalcon
API2
terça-feira, 16 de julho de 13
@lgavinho
lg@gavinho.eti.br
crazydeploy.tumblr.com
linkedin.com/in/lgavinho
falcão peregrino: o animal mais rápido do mundo. A ave chega a atingir incríveis 360 quilômetros por hora durante um voo em queda livre.
www.gavinho.eti.br
terça-feira, 16 de julho de 13
Recommended