Más contenido relacionado La actualidad más candente (20) Similar a Adianti Framework PHPConf 2013 (20) Adianti Framework PHPConf 20132. Roteiro
●
Introdução
–
–
Por que o framework? Qual o seu foco real?
–
Arquitetura, estrutura;
–
●
Contextualizar o caminho até o framework;
Templates.
Aspectos
–
Persistência de objetos;
–
Apresentação;
–
Organização.
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#2
4. O caminho
●
Clipper (1994-1998): comercial, bibliotecas, funções;
●
Delphi (1998-1999): automação, componentes;
●
PHP (2000): SAGU (php+html+sql);
●
PHP-GTK(2001): PHP só com classes;
●
Gnuteca (2002): PHP Web com classes;
●
Design Patterns (2004): Aprendizado na Unisinos;
●
Core (2006): Primeiro Framework básico MVC;
●
Mestrado (2008, 2009): Projeto em background;
●
Retomada (2011, 2012): Lançamento em Dez/12;
●
Comunidade crescente;
●
Projetos grandes provando sua eficácia.
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#4
5. Por quê?
●
Explicação bom mocinho:
–
●
Porque eu quero mudar o mundo;
O mais próximo da realidade:
–
Por que eu preciso disto;
–
Porque eu sei como fazer;
–
Porque eu quero muito fazer;
–
Porque vai ficar do jeito que eu quiser;
–
Porque ninguém pode me impedir;
–
Porque eu faria de qualquer jeito mesmo;
–
Porque mais alguém pode gostar disso (you ;-).
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#5
6. What's the main point
●
Softwares de negócio possuem necessidades pontuais;
●
Foco na camada de negócios, com regras complexas;
●
Maior uniformidade (padronização) na apresentação;
●
Maior padronização de estrutura entre projetos;
●
Necessidade de produtividade, mesmo para iniciantes;
●
Várias formas de fazer a mesma coisa gera fragmentação;
●
Quanto mais tecnologias diferentes, menos uniforme;
●
Uniformização facilita um entendimento comum;
●
Foco no negócio, não na tecnologia;
●
Tudo para tornar a MANUTENÇÃO mais simples;
●
MANUTENÇÃO é a grande causadora de falhas em SW.
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#6
10. Instalação
# cd /var/www
# unzip adianti-framework-<versão>.zip
http://localhost/framework
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#10
11. Página de entrada
●
Pelo navegador:
http://localhost/framework/index.php?class=ClienteForm
●
Links internos:
<a href="index.php?class=ClienteForm" generator="adianti"> ...</a>
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#11
12. Template
●
Armazenado em app/templates.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
{LIBRARIES}
...
<script type="text/javascript">
$('[generator="adianti"]').live('click', function() {
__adianti_load_page($(this).attr('href'));
return false;
});
</script>
</head>
<body>
<div class="container">
<div class="body">
<div id="adianti_div_content" class="content"></div>
<div id="adianti_online_content"></div>
</div>
</div>
<script type="text/javascript">
//#javascript_placeholder#
</script>
</body>
</html>
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#12
13. Um controlador
●
Toda página é filha de TPage ou TWindow;
●
É construída por meio de composição de objetos.
<?php
class WellcomeView extends TPage
{
function __construct()
{
parent::__construct();
$image = new TImage('app/images/frontpage.png');
// adiciona a imagem à página.
parent::add($image);
}
}
?>
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#13
17. Active Record
●
Um objeto que representa uma linha de uma tabela e
encapsula ao mesmo tempo o acesso aos dados e a lógica
de negócios;
<?php
/**
* Active Record para Customer (clientes)
*/
class Customer extends TRecord
{
const TABLENAME = 'customer';
const PRIMARYKEY= 'id';
const IDPOLICY = 'max'; // {max, serial}
}
?>
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#17
18. Salvar um novo objeto
●
Armazenar um novo objeto
<?php
//...
TTransaction::open('samples'); // abre uma transação
// cria novo objeto
$maria = new Customer;
INSERT INTO customer (
$maria->name
= 'Maria da Silva';
name,
$maria->address
= 'Rua da Conceicao';
address,
$maria->phone
= '(51) 8111-2222';
phone,
... )
$maria->birthdate
= '2013-02-15';
values
$maria->status
= 'S';
(
$maria->email
= 'maria@email.com';
'Maria da Silva',
'Rua da Conceicao',
$maria->gender
= 'M';
'(51) 8111-2222',
$maria->category_id = '1';
...);
$maria->city_id
= '1';
$maria->store(); // armazena o objeto
TTransaction::close(); // fecha a transação.
//...
?>
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#18
19. Alterar um objeto
●
Carregar um cliente;
●
Alterar uma de suas propriedades.
<?php
//...
TTransaction::open('samples'); // abre uma transação
$objeto = new Customer; // instancia o cliente
$customer= $objeto->load(31); // carrega o cliente 31
if ($customer) // se existe
{
$customer->phone = '51 8111-3333'; // muda o fone
$customer->store(); // armazena o objeto
}
new TMessage('info', 'Objeto atualizado');
TTransaction::close(); // fecha a transação.
//...
?>
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#19
20. Manipulando coleções
●
Carrega um conjunto de objetos que atendem ao critério.
<?php
//...
TTransaction::open('samples'); // abre uma transação
$criteria = new TCriteria;
$criteria->add(new TFilter('address', 'like', 'Rua Porto%'));
$criteria->add(new TFilter('gender', '=', 'M'));
$repository = new TRepository('Customer');
$customers = $repository->load($criteria);
foreach ($customers as $customer)
{
$customer->phone = '84 '.substr($customer->phone, 3);
$customer->store();
}
TTransaction::close(); // fecha a transação.
//...
?>
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#20
21. Manipulando coleções
●
Versão simplificada
<?php
TTransaction::open('samples');
$repository = new TRepository('Customer');
$customers = $repository->where('gender', '=', 'M')
->where('name', 'like', 'A%')
->load();
foreach ($customers as $customer)
{
echo $customer->id . ' - ' . $customer->name . '<br>';
}
TTransaction::close();
?>
●
Versão super simplificada (PHP >= 5.4):
(new TRepository('Contact'))->where('customer_id', '=', $id)->delete();
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#21
23. Studio Pro
XMI
SQL
PHP
<?php
/**
* Customer Active Record
* @author <your-name-here>
*/
class Customer extends TRecord
{
public function get_city()
public function addSkill(Skill $skill)
public function getSkills()
public function load($id)
public function store()
public function delete($id = NULL)
}
?>
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#23
24. Studio Pro
●
Se o modelo for feito antes, facilita tudo;
●
Relações podem ser definidas por wizards também.
<?php
// load customer
$obj = new Customer(5);
print $obj->city->name;
print $obj->category->name;
// percorre contatos
foreach ($obj->getContacts() as $contact)
{
$contact->value = '9'.$contact->value;
$contact->store();
print $contact->type . '-' . $contact->value;
}
foreach ($obj->getSkills() as $skill)
{
print $skill->name;
}
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#24
26. Containers
<?php
class TestView extends TPage
{
function __construct()
{
parent::__construct();
$table = new TTable;
$table->width = '400px';
$table->border = '1';
$title = new TLabel('Texto1');
$title->setFontSize(18);
$title->setFontFace('Arial');
$title->setFontColor('red');
$row=$table->addRow();
$row->addCell($title);
$row->addCell(new TLabel('Texto2'));
parent::add($table);
}
}
?>
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#26
27. Painéis
<?php
class TestView extends TPage
{
function __construct()
{
parent::__construct();
$panel = new TPanel(480, 260);
$title = new TLabel('Texto1');
$title->setFontSize(18);
$title->setFontFace('Arial');
$title->setFontColor('red');
$image = new TImage('app/images/mouse.png');
$name = new TEntry('name');
$name->setSize(140);
$panel->put($title, 10, 40);
$panel->put($image, 100, 80);
parent::add($panel);
}
}
?>
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#27
28. Notebook
<?php
class TestView extends TPage
{
function __construct()
{
parent::__construct();
$notebook = new TNotebook(400,200);
$page1 = new TTable;
$page2 = new TPanel(370,180);
// adds two pages in the notebook
$notebook->appendPage('Basic data', $page1);
$notebook->appendPage('Other data', $page2);
$page1->addRow()->addCell(new TLabel('conteudo page1'));
$page2->put(new TEntry('name'), 50,50);
parent::add($notebook);
}
}
?>
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#28
29. Informação
<?php
class TestView extends TPage
{
public function __construct()
{
parent::__construct();
new TMessage('info', 'Information message');
}
}
?>
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#29
30. Questão
<?php
class TestView extends TPage
{
public function __construct()
{
parent::__construct();
$action1 = new TAction(array($this, 'onAction1'));
$action2 = new TAction(array($this, 'onAction2'));
$action1->setParameter('parameter', 1);
$action2->setParameter('parameter', 2);
new TQuestion('Deseja realizar essa operação ?', $action1, $action2);
}
function onAction1($param)
{
new TMessage('info', "Primeira opção. Parâmetro {$param['parameter']}");
}
}
?>
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#30
31. Quick Forms
<?php
class TestView extends TPage
{
private $form;
function __construct()
{
parent::__construct();
$this->form
$id
$description
$date
$text
=
=
=
=
=
new
new
new
new
new
TQuickForm;
TEntry('id');
TEntry('description');
TDate('date');
TText('text');
$this->form->addQuickField('Id',
$id,
40);
$this->form->addQuickField('Description', $description, 200);
$this->form->addQuickField('Date', $date, 100);
$this->form->addQuickField('Text', $text, 120);
$action = new TAction(array($this, 'onSave'));
$this->form->addQuickAction('Save', $action, 'ico_save.png');
parent::add($this->form);
}
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#31
32. Quick Forms
public function onSave($param)
{
$data = $this->form->getData();
$this->form->setData($data);
$message =
$message.=
$message.=
$message.=
'Id: '
. $data->id . '<br>';
'Description : ' . $data->description . '<br>';
'Date : '
. $data->date . '<br>';
'Text : '
. $data->text . '<br>';
new TMessage('info', $message);
}
}
?>
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#32
33. Automatic Selections
<?php
class TestView extends TPage
{
private $form;
function __construct()
{
parent::__construct();
$notebook = new TNotebook(530, 160);
$this->form = new TForm;
$table = new TTable;
$this->form->add($table);
$notebook->appendPage('Automatic DBselection elements', $this->form);
$radio = new TDBRadioGroup('radio', 'samples', 'Category', 'id', 'name');
$check = new TDBCheckGroup('check', 'samples', 'Category', 'id', 'name');
$combo = new TDBCombo('combo', 'samples', 'Category', 'id', 'name');
$radio->setLayout('horizontal');
$check->setLayout('horizontal');
$combo->setSize(160);
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#33
34. Datagrids
<?php
class TestView extends TPage
{
private $datagrid;
public function __construct()
{
parent::__construct();
$this->datagrid = new TDataGrid;
$code = new TDataGridColumn('code', 'Code', 'right', 70);
$name = new TDataGridColumn('name', 'Name', 'left', 180);
...
$this->datagrid->addColumn($code);
$this->datagrid->addColumn($name);
...
$action1 = new TDataGridAction(array($this, 'onView'));
$action1->setLabel('View');
$action1->setImage('ico_find.png');
$action1->setField('name');
$this->datagrid->addAction($action1);
$this->datagrid->createModel();
parent::add($this->datagrid);
}
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#34
35. Datagrids
function onReload()
{
$this->datagrid->clear();
$item = new StdClass;
$item->code
= '1';
$item->name
= 'Fábio Locatelli';
$item->address = 'Rua Expedicionario';
$item->fone
= '1111-1111';
$this->datagrid->addItem($item);
...
}
function onView($param)
{
$key=$param['key'];
new TMessage('info', "The name is : $key");
}
function show()
{
$this->onReload();
parent::show();
}
}
?>
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#35
37. Formulário
<?php
class StandardFormView extends TPage
{
protected $form; // form
function __construct()
{
parent::__construct();
$this->form = new TQuickForm('form_City');
$id
= new TEntry('id');
$name
= new TEntry('name');
$id->setEditable(FALSE);
$this->form->addQuickField('id', $id, 100);
$this->form->addQuickField('name', $name, 100);
$action = new TAction(array($this, 'onSave'));
$this->form->addQuickAction('Save', $action, 'ico_save.png');
parent::add($this->form);
}
}
?>
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#37
38. Formulário
<?php
class StandardFormView extends TPage
{
// ...
function onSave()
{
try
{
TTransaction::open('samples');
$object = $this->form->getData('City');
$object->store();
$this->form->setData($object);
TTransaction::close();
new TMessage('info', 'Record saved');
}
catch (Exception $e) // in case of exception
{
new TMessage('error', '<b>Error</b> ' . $e->getMessage());
TTransaction::rollback();
}
}
}
?>
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#38
41. Templates
<?php
class FormDecoratedView extends TPage
{
private $form;
function __construct()
{
parent::__construct();
TPage::include_css('app/resources/formdecorator.css');
$html = new THtmlRenderer('app/resources/formdecorator.html');
$this->form = new TQuickForm;
$id
= new TEntry('id');
$description = new TEntry('description');
$this->form->addQuickField('Id',
$id,
// ...
40);
$this->form->addQuickAction('Save', new TAction(..., 'ico_save.png');
// substitui variáveis
$replace = array('form'=> $this->form);
$html->enableSection('main', $replace);
parent::add($html);
}
}
?>
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#41
43. Form Elements
<?php
class TestView extends TPage
{
private $form;
function __construct()
{
parent::__construct();
$this->form = new TForm;
try {
$ui = new TUIBuilder(500,300);
$ui->setController($this);
$ui->setForm($this->form);
$ui->parseFile('app/forms/sample.form.xml'); // lê o xml
$this->form->add($ui);
$this->form->setFields($ui->getFields());
}
catch (Exception $e)
{
new TMessage('error', $e->getMessage());
}
parent::add($this->form);
}
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#43
44. Form Elements
function onView()
{
$data = $this->form->getData();
// put the data back to the form
$this->form->setData($data);
// creates
$message =
$message.=
$message.=
$message.=
$message.=
$message.=
a string with the form element's values
'ID : ' . $data->id . '<br>';
'Name : ' . $data->name . '<br>';
'Birthdate : ' . $data->birthdate . '<br>';
'Gender : ' . $data->gender . '<br>';
'Pets : ' . $data->pets . '<br>';
'Fashion : ' . implode(',', $data->fashion) . '<br>';
// show the message
new TMessage('info', $message);
}
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#44
45. Próximas novidades
●
Novos componentes:
TSortList;
Melhorias:
–
●
Nova TFile com progresso;
– Novos dialogs (TMessage, TQuestion com bootstrap);
– Novos eventos (trocas em radios, checks);
– Melhorias nas exceções das validações;
Novos exemplos:
–
●
–
●
Transições de páginas (wizards);
Novas apps disponíveis:
–
Aplicação completa com controle de permissões.
Adianti Solutions Ltda © Pablo Dall'Oglio
Adianti Framework
#45