JDay Deutschland 2015 - PHP Design Patterns in Joomla!

41
PHP Design Patterns in Joomla! @yveshoppe

Transcript of JDay Deutschland 2015 - PHP Design Patterns in Joomla!

PHP Design Patternsin Joomla!

@yveshoppe

Was sind Design Patterns?

Allgemein anerkannte Lösungen für

wiederkehrende Probleme

Anti Patterns

$_REQUEST anstelle von JInput

Reinventing the square wheel

Mambo / Joomla 1.0

Ball of Mud

Als Stackoverflow einen Tag nicht erreichbar war sank die Produktivität um 80 %!

Copy & Paste

Perfekte Lösungen für eine nicht perfekte Welt!

Overengineering

Ein Objekt welche alle Aufgaben und Daten in sich vereint.

God Object

Entwurfsmuster, Antimuster, abstrakte Fabrik, Schablonenmethode,

Fliegengewicht, Gasfabrik, Wunderwaffe

Translating Design Patterns into German!

Quelle: Wikipedia.de

Geschichte der Design Patterns

Gang of Four (1994)

Erich Gamma

Ralph Johnson

Richard Helm

John Vlissides

23 Design Patterns

in Kategorien

Einteilung von Design Patterns

$bla = JFilterInput::getInstance()->clean($bla);$db = JFactory::getDbo();

Creational Patterns

Bridge Pattern (Ouptut raw, html, component, feed)

Structual Patterns

JTableObserverContenthistory::createObserver($this, array('typeAlias' => 'com_tags.tag'));

Behavioral Patterns

Revisions / Versions in Joomla 3.x!

Observer Pattern

Tag History

Observer Pattern

Speichern

interface JObserverInterface{

public static function createObserver ( JObservableInterface $observableObject, $params = array());

}

libraries/joomla/observer/interface.php

abstract class JTableObserver implements JObserverInterface

{public function __construct(JTableInterface $table){ $table->attachObserver($this);..}

public function onAfterStore(&$result){}

}

libraries/joomla/table/observer.php

class JTableObserverContenthistory extends JTableObserver{

public static function createObserver(JObservableInterface $observableObject, $params = array())

{$typeAlias = $params['typeAlias'];$observer = new self($observableObject);$observer->contenthistoryHelper = ..;

return $observer;}

public function onAfterStore(&$result){

..$this->contenthistoryHelper->store($this->table);

}

libraries/joomla/table/observer/contenthistory.php

class TagsTableTag extends JTableNested{

public function __construct($db){

parent::__construct('#__tags', 'id', $db);

JTableObserverContenthistory::createObserver($this, array('typeAlias' => 'com_tags.tag'));

}

libraries/joomla/observer/interface.php

Model View Controller Pattern

Observer, Composite und Strategy Pattern vereint.

MVC Schema

Verarbeitung von Aufgaben (tasks) und Events

Controller

class ContentControllerArticle extends JControllerForm{

public function save($key = null, $urlVar = null){

$model = $this->getModel();$table = $model->getTable();

.. $model->save($validData)

..$viewType = 'html';$view = $this->getView($viewName, $viewType, '', array('base_path'

=> $this->basePath, 'layout' => $viewLayout));

$view->display();

administrator/components/com_content/controllers/article.php

Stark vereinfacht!

Die Applikations-Logik (CRUD etc)

Model

class ContentModelArticle extends JModelAdmin{

public function save($data){

// Filtern, Aufräumen, Plugin Events und Co.

...$table = $this->getTable();

if (!$table->bind($data)){

$this->setError($table->getError());}

..// Speichern in der Datenbank.. ..

return true;

administrator/components/com_content/models/article.php

Die Ausgabeschicht (HTML, RAW, COMPONENT etc.)

View

class ContentViewArticle extends JViewLegacy{

public function display($tpl = null){

echo “Erfolgreich gespeichert..”;

administrator/components/com_content/views/article/view.html.php

JLoader::register('ContentHelper', JPATH_ADMINISTRATOR . '/components/com_content/helpers/content.php'

);

Auto-Loading (PSR)

(statt require(_once))

Warum JLoader?

Performance&

Einfachheit!

abstract class JLoader{

public static function setup($enablePsr = true, $enablePrefixes = true, $enableClasses = true){

..spl_autoload_register(array('JLoader', 'load'))spl_autoload_register(array('JLoader', 'loadByPsr0'));spl_autoload_register(array('JLoader', 'loadByAlias'));

libraries/loader.php

abstract class JLoader{

protected static $classes = array();

public static function register($class, $path, $force = true)

{..self::$classes[$class] = $path;

}

public static function discover($classPrefix, $parentPath, $force = true, $recurse = false)

{.. foreach ($iterator as $file)

self::register($class, $file->getPath() . '/' . $fileName);

libraries/loader.php

abstract class JLoader{

public static function load($class){

..include_once self::$classes[$class];

libraries/loader.php

JUpdater -> update($type)

Fragen?

Vielen Dank!