The past few weeks I've been trying to grasp on nearly any Design Pattern ever invented, but the repository pattern is one I could understand and implement it myself in my project. My application uses the MVC pattern where I've got the "V" and the "C" part covered in a very basic way, and I want to get rid of a single model "M" doing everything the view needs. I would like to know if I'm somewhat in the right direction. Along with that I ask some questions on parts of the approach.
Let's get started with some code, the whole operation is to get the latest log events from an external data source (database):
Controller
My controller creates a new Repository object, which takes the Database wrapper and the Data Mapper as its arguments. The Data Mapper takes a Domain Entity.
How would I take care of these 4 require_once statements in my Controller functions?
public function index() {
// need a way to not have to require these classes all the time
require_once '../app/domain/repositories/EventRepository.php';
require_once '../app/domain/interfaces/EventRepositoryInterface.php';
require_once '../app/domain/mappers/EventMapper.php';
require_once '../app/domain/entities/Event.php';
$events = new EventRepository(new Database('db'), new EventMapper(new Event()));
try {
$return = $events->fetchAll();
} catch (DatabaseException $e) {
$return = $e->getErrorMessage();
}
$this->view('/home/dashboard', ['return' => $return]);
}
Repository
class EventRepository implements EventRepositoryInterface {
protected $dbh;
protected $eventMapper;
public function __construct(Database $dbh, EventMapper $eventMapper) {
$this->dbh = $dbh;
$this->eventMapper = $eventMapper;
}
public function fetchAll() {
$data = $this->dbh->sql_select('SELECT datetime, message FROM eventlog');
return $this->eventMapper->toEvent($data);
}
}
Repository Interface
interface EventRepositoryInterface {
public function fetchAll();
}
Data Mapper
I'm using a Data Mapper in my Repository Pattern approach. Not sure if this is a right way to do it, but I've seen examples of implementing Data Mappers with repositories.
class EventMapper {
protected $collection = [];
protected $event;
public function __construct(Event $event) {
$this->event = $event;
}
public function toEvent($data) {
foreach($data as $value) {
$this->event->setDateTime($value['datetime']);
$this->event->setMessage($value['message']);
$this->collection[] = $this->event;
}
return $this->collection;
}
}
Domain Entity
class Event {
protected $id;
protected $dateTime;
protected $message;
protected $type;
public function setDateTime($dateTime) {
$this->dateTime = $dateTime;
}
public function getDateTime() {
return $this->dateTime;
}
public function setMessage($message) {
$this->message = $message;
}
public function getMessage() {
return $this->message;
}
}
This code leaves me with an array of objects which I can do other stuff with it. Eventually the data from this array needs to be converted to an HTML table. Where would I put such a class (like TablePrinter.php) in my application?
require_once
statements! getcomposer.org/doc/01-basic-usage.md#autoloading \$\endgroup\$