Created
November 17, 2010 12:14
-
-
Save paulchubatyy/703325 to your computer and use it in GitHub Desktop.
Examples for translation of Scaling Web Applications by samsoir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// .... bootstap.php | |
Route::set('messages', 'messages/<action>/<user>(<format>)', array('format' => '\.\w+')) | |
->defaults(array( | |
'format' => '.json', | |
'controller' => 'messages', | |
)); | |
// .... bootstrap.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// .......... | |
//-- Environment setup -------------------------------------------------------- | |
Kohana::$profiling = TRUE; | |
// .......... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Controller_Default extends Controller { | |
public function action_index() | |
{ | |
// Приклад внутрішнього запиту | |
$internal_request = Request::factory('controller/action/param') | |
->execute(); | |
// Приклад зовнішнього запиту | |
$external_request = Request::factory('http://www.ibuildings.com/controller/action/param') | |
->execute(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Контроллер що обслуговує перший запит | |
class Controller_Default extends Controller | |
{ | |
public function action_index() | |
{ | |
// Якщо був GET запит | |
if ($this->request->method === 'GET') | |
{ | |
// Виводимо POST, в результаті array (0) { empty } | |
var_dump($_POST); | |
// Створюємо запит до іншого ресурсу | |
$log = Request::factory('/log/access/'.$page_id); | |
// Встановлюємо метод POST | |
$log->method = 'POST'; | |
// Додаємо інформацію до запиту | |
$log->post = array( | |
'uid' => $this->user->id, | |
'ua' => Request::user_agent('browser'), | |
'protocol' => Request::$protocol, | |
); | |
// Виконуємо запит | |
$log->execute(); | |
// виводимо POST, знову виведе array (0) { empty } | |
var_dump($_POST); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Обробляє запит до http://gazouillement.com/samsoir/ | |
class Controller_Index extends Controller { | |
public function action_index() | |
{ | |
// Загружає кориситуваа (samsoir) в модель | |
$user = new Model_User($this->request->param('user')); | |
// Якщо немає користувача виводимо 404 вмкобчення. | |
if ( ! $user->loaded) | |
throw new Controller_Exception_404('Unable to load user :user', array(':user' => $this->request->param('user'))); | |
// Загружаємо повідомлення користувача в форматі .xhtml | |
$messages = Request::factory('messages/find/'.$user->name.'.xhtml') | |
->execute() | |
->response; | |
// Загружаємо зв’язки користувача в форматі .xhtml | |
$relations = Request::factory($user->name.'/following.xhtml') | |
->execute() | |
->response; | |
// Приміняється до вигляду дані про користувача | |
// повідомлення і зв’язки і віддається в вивід. | |
$this->request->response = View::factory('user/home', array( | |
'user' => $user, | |
'messages' => $messages, | |
'relations' => $relations, | |
)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Обробляє запит до to http://gazouillement.com/samsoir/ | |
class Controller_Index extends Controller { | |
protected $_messages_uri = 'http://messages.gazouillement.com'; | |
public function action_index() | |
{ | |
// Загружає користувача в модель (samsoir) | |
$user = new Model_User($this->request->param('user')); | |
// Якщо користувач не завантажений викидаємо Exception | |
if ( ! $user->loaded) | |
throw new Controller_Exception_404('Unable to load user :user', array(':user' => $this->request->param('user'))); | |
// -- Починається змінений код тут -- | |
// Нове URI до повідомлень на іншому сервері | |
$messages_uri = $this->_messages_uri.'/find/'.$user->name'.xhtml'; | |
// Загрузка повідмолень в xhtml форматі | |
$messages = Request::factory($messages_uri) | |
->execute() | |
->response; | |
// -- Закінчився змінений код -- | |
// Завантаження зв’язків в xhtml форматі | |
$relations = Request::factory($user->name.'/following.xhtml') | |
->execute() | |
->response; | |
// Приміняється до вигляду дані про користувача | |
// повідомлення і зв’язки і віддається в вивід. | |
$this->request->response = View::factory('user/home', array( | |
'user' => $user, | |
'messages' => $messages, | |
'relations' => $relations, | |
)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Контроллер другого запиту | |
class Controller_Log extends Controller | |
{ | |
public function action_access($page_id) | |
{ | |
// Коли запит йде з дії index вище | |
// ми виводимо інформацію | |
// array (3) {string (3) 'uid' => int (1) 1, string (2) 'ua' => string(10) 'Mozilla ... ... | |
var_dump($_POST); | |
// Create a new log model | |
$log = new Log_Model; | |
// Set the values and save | |
$log->set_values($_POST) | |
->save(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Controller_Messages extends Controller { | |
// Формати виводу, доступні для контроллера | |
protected $supported_formats = array( | |
'.xhtml', | |
'.json', | |
'.xml', | |
'.rss', | |
); | |
// Контекстний користувач запиту | |
protected $user; | |
// цей код виконається перед дією | |
// перевіримо на валідний формат і користувача | |
public function before() | |
{ | |
// Перевіряємо формат | |
if ( ! in_array($this->request->param('format'), $this->supported_formats)) | |
throw new Controller_Exception_404('File not found'); | |
// Перевіряємо користувача | |
$this->user = new Model_User($this->request->param('user'); | |
if ( ! $this->user->loaded()) | |
throw new Controller_Exception_404('File not found'); | |
return parent::before(); | |
} | |
// Знаходить повідомлення користувача | |
public function find() | |
{ | |
// завантажує повідомлення 1:M зв’язку | |
$messages = $this->user->messages; | |
// визначає відповідь згідно з формату | |
$this->request->response = $this->_prepare_response($messages); | |
} | |
// Готує до форматування | |
protected function _prepare_response(Model_Iterator $messages) | |
{ | |
// Повертає повідомлення згідно формату | |
switch ($this->request->param('format') { | |
case '.json' : { | |
$this->request->headers['Content-Type'] = 'application/json'; | |
$messages = $messages->as_array(); | |
return json_encode($messages); | |
} | |
case '.xhtml' : { | |
return View::factory('messages/xhtml', $messages); | |
} | |
default : { | |
throw new Controller_Exception_404('File not found!'); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Controller_Somecontroller { | |
// ......................... | |
// ......................... | |
// ......................... | |
// ......................... | |
public function after() | |
{ | |
// Add the profiler output to the controller | |
$this->request->response .= View::factory('profiler/stats'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment