Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove partial, add browse and read from array. Not from DB #7

Merged
merged 1 commit into from May 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
/vendor/
composer.lock
24 changes: 24 additions & 0 deletions composer.json
@@ -0,0 +1,24 @@
{
"name": "pmjones/mvc-refinement",
"description": "An attempt to refine MVC for a web-specific environment.",
"homepage": "https://github.com/pmjones/mvc-refinement",
"require": {
"aura/view": "dev-reduced",
"aura/web": "2.0.*@dev",
"aura/html": "2.0.*@dev"
},
"license": "BSD-2-Clause",
"authors": [
{
"name": "Paul M Jones",
"email": "pmjones88@gmail.com"
}
],
"autoload": {
"psr-4": {
"Blog\\": "example-code/Blog",
"": "example-code"
}
},
"minimum-stability": "dev"
}
4 changes: 2 additions & 2 deletions example-code/AbstractResponder.php
Expand Up @@ -4,7 +4,7 @@

abstract class AbstractResponder
{
protected $data;
public $data;

protected $response;

Expand All @@ -19,7 +19,7 @@ public function __construct(Response $response, View $view)

public function __get($key)
{
return $this->data->$key;
return isset($this->data->$key) ? $this->data->{$key} : '';
}

public function __set($key, $val)
Expand Down
6 changes: 3 additions & 3 deletions example-code/Blog/Action/BlogBrowseAction.php
Expand Up @@ -10,17 +10,17 @@ class BlogBrowseAction extends AbstractBlogAction
public function __construct(
Request $request,
BlogService $domain,
BlogReadResponder $responder
BlogBrowseResponder $responder
) {
$this->request = $request;
$this->domain = $domain;
$this->responder = $responder;
}

public function __invoke($id)
public function __invoke()
{
$page = $this->request->query->get('page', 1);
$this->responder->collection = $this->domain->fetchAllByPage($page);
$this->responder->data->collection = $this->domain->fetchAllByPage($page);
return $this->response();
}
}
2 changes: 1 addition & 1 deletion example-code/Blog/Action/BlogReadAction.php
Expand Up @@ -19,7 +19,7 @@ public function __construct(

public function __invoke($id)
{
$this->responder->blog = $this->domain->fetchOneById($id);
$this->responder->data->blog = $this->domain->fetchOneById($id);
return $this->response();
}
}
20 changes: 20 additions & 0 deletions example-code/Blog/Domain/BlogEntity.php
Expand Up @@ -11,6 +11,11 @@ class BlogEntity

protected $messages;

public function __construct($data = array())
{
$this->setData($data);
}

public function setMessages($messages)
{
$this->messages = $messages;
Expand All @@ -20,4 +25,19 @@ public function getMessages($messages)
{
return $this->messages;
}

public function setData($data = array())
{
foreach ($data as $key => $value) {
if (property_exists($this, $key)) {
$this->{$key} = $value;
}
}
}

public function getData()
{
$properties = get_object_vars($this);
return $properties;
}
}
29 changes: 29 additions & 0 deletions example-code/Blog/Domain/BlogService.php
Expand Up @@ -6,11 +6,40 @@ class BlogService
public function fetchAllByPage($page)
{
// returns a collection of BlogEntity objects
$data = array(
array(
'id' => 1,
'author' => 'Paul M Jones',
'title' => 'The creator of aura',
'intro' => 'Paul M. Jones is an internationally recognized PHP expert who has worked as everything from junior developer to VP of Engineering in all kinds of organizations',
'body' => 'Paul M. Jones is an internationally recognized PHP expert who has worked as everything from junior developer to VP of Engineering in all kinds of organizations (corporate, military, non-profit, educational, medical, and others). Paul\'s latest open-source project is Aura for PHP. Among his other accomplishments, Paul is the lead developer of the Solar Framework, and the creator of the Savant template system. He has authored a series of authoritative benchmarks on dynamic framework performance, and was a founding contributor to the Zend Framework (the DB, DB_Table, and View components). Paul is a voting member of the PHP Framework Interoperability Group, where he shepherded the PSR-1 and PSR-2 recommendations, and was the primary author on the PSR-4 autoloader recommendation. He was also a member of the Zend PHP 5.3 Certification education advisory board. He blogs at paul-m-jones.com. In a previous career, Paul was an operations intelligence specialist for the US Air Force, and enjoys putting .308 holes in targets at 400 yards.'
),
array(
'id' => 2,
'author' => 'Hari KT',
'title' => 'Something',
'intro' => 'Some introduction',
'body' => 'Some long content'
)
);
$collection = array();
foreach ($data as $val) {
$collection[] = new BlogEntity($val);
}
return $collection;
}

public function fetchOneById($id)
{
// returns a single BlogEntity
$data = array(
'id' => 1,
'author' => 'Hari KT',
'title' => 'Something',
'intro' => 'Some introduction',
'body' => 'Some long content'
);
return new BlogEntity($data);
}

public function create(array $data)
Expand Down
4 changes: 3 additions & 1 deletion example-code/Blog/Responder/AbstractBlogResponder.php
@@ -1,7 +1,9 @@
<?php
namespace Blog\Responder;

abstract class AbstractBlogResponder
use AbstractResponder;

abstract class AbstractBlogResponder extends AbstractResponder
{
protected function notFound($key)
{
Expand Down
23 changes: 23 additions & 0 deletions example-code/Blog/Responder/BlogBrowseResponder.php
Expand Up @@ -5,6 +5,29 @@ class BlogBrowseResponder extends AbstractBlogResponder
{
public function __invoke()
{
$view_registry = $this->view->getViewRegistry();
$view_registry->set('browse', __DIR__ . '/views/browse.php');
$view_registry->set('_intro', __DIR__ . '/views/_intro.php');
/*
$this->data->items = array(
array(
'name' => 'Something',
'cost' => 23
),
array(
'name' => 'Many',
'cost' => 23
)
);
$view_registry->set('item_rows', function () {
foreach ($this->items as $item) {
echo $this->render('item_row', array('item' => $item));
}
});
$view_registry->set('item_row', function () {
echo $item['name'] . ' costs ' . $item['price'] . PHP_EOL;
});
*/
return $this->notFound('collection')
|| $this->responseView('browse');
}
Expand Down
4 changes: 3 additions & 1 deletion example-code/Blog/Responder/BlogReadResponder.php
Expand Up @@ -5,7 +5,9 @@ class BlogReadResponder extends AbstractBlogResponder
{
public function __invoke()
{
return $this->notFound('collection')
$view_registry = $this->view->getViewRegistry();
$view_registry->set('read', __DIR__ . '/views/read.php');
return $this->notFound('blog')
|| $this->responseView('read');
}
}
9 changes: 8 additions & 1 deletion example-code/Blog/Responder/views/browse.php
@@ -1,4 +1,11 @@
<?php
foreach ($this->collection as $blog) {
$this->render('_intro', array('blog' => $blog));
?>
<div class="blog-intro">
<h2><?= $blog->title; ?></h2>
<p class="byline"><?= $blog->author; ?></p>
<?= $blog->intro; ?>
<p><?= $this->anchor("/blog/read/{$blog->id}", 'Read More ...'); ?></p>
</div>
<?php
}
4 changes: 2 additions & 2 deletions example-code/Blog/Responder/views/read.php
@@ -1,7 +1,7 @@
<?php use Aura\Html\Functions; ?>

<h2><?= h($this->blog->title); ?></h2>
<p class="byline"><?= h($this->blog->author); ?>;
<h2><?= $this->blog->title; ?></h2>
<p class="byline"><?= $this->blog->author; ?></p>

<div class="blog-body">
<?= $this->blog->body; ?>
Expand Down
17 changes: 17 additions & 0 deletions example-code/blog-browse.php
@@ -0,0 +1,17 @@
<?php
require __DIR__ . "/bootstrap.php";

$blog_service = new Blog\Domain\BlogService();
$blog_browse_responder = new Blog\Responder\BlogBrowseResponder(
$response,
$view
);

$blog_browse_action = new Blog\Action\BlogBrowseAction(
$request,
$blog_service,
$blog_browse_responder
);
$id = 1;
$blog_browse_action();
echo $response->content->get();
16 changes: 16 additions & 0 deletions example-code/blog-read.php
@@ -0,0 +1,16 @@
<?php
require __DIR__ . '/bootstrap.php';
$blog_service = new Blog\Domain\BlogService();
$blog_read_responder = new Blog\Responder\BlogReadResponder(
$response,
$view
);

$blog_read_action = new Blog\Action\BlogReadAction(
$request,
$blog_service,
$blog_read_responder
);
$id = 1;
$blog_read_action($id);
echo $response->content->get();
12 changes: 12 additions & 0 deletions example-code/bootstrap.php
@@ -0,0 +1,12 @@
<?php
require dirname(__DIR__) . "/vendor/autoload.php";
use Aura\Web\WebFactory;

$web_factory = new WebFactory($GLOBALS);
$request = $web_factory->newRequest();
$response = $web_factory->newResponse();

$helper = require dirname(__DIR__) . "/vendor/aura/html/scripts/instance.php";

$view_factory = new \Aura\View\ViewFactory;
$view = $view_factory->newInstance($helper);