JDay Deutschland 2015 - PHP Design Patterns in Joomla!
-
Upload
yves-hoppe -
Category
Software
-
view
367 -
download
4
Transcript of JDay Deutschland 2015 - PHP Design Patterns in Joomla!
Entwurfsmuster, Antimuster, abstrakte Fabrik, Schablonenmethode,
Fliegengewicht, Gasfabrik, Wunderwaffe
Translating Design Patterns into German!
JTableObserverContenthistory::createObserver($this, array('typeAlias' => 'com_tags.tag'));
Behavioral Patterns
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
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!
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
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)
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