Skip to content

Instantly share code, notes, and snippets.

@stojg
Created August 23, 2011 11:17
Show Gist options
  • Save stojg/1164878 to your computer and use it in GitHub Desktop.
Save stojg/1164878 to your computer and use it in GitHub Desktop.
Silverstripe visitor pattern
<?php
Object::add_extension('SiteTree', 'ChangeDecorator');
<?php
class ChangeDecorator extends DataObjectDecorator {
/**
*
*/
public function onAfterWrite() {
$publisher = PublisherQueueVisitor::instance();
$this->getOwner()->accept($publisher);
}
/**
*
* @param $visitor
*/
public function accept( $visitor ) {
$owner = $this->getOwner();
$methodName = "visit".get_class($owner);
if( method_exists($visitor, $methodName)) {
$visitor->$methodName($this->getOwner());
}
}
}
<?php
class PublisherQueueVisitor {
/**
*
* @var array
*/
protected $urls = array();
/**
*
* @var PublisherQueueVisitor
*/
static $instance = null;
/**
*
* @return Dispatcher
*/
public static function instance() {
if( !self::$instance ) {
self::$instance = new PublisherQueueVisitor();
}
return self::$instance;
}
public function visitPage(DataObject $object) {
$this->urls[] = $object->Link();
}
public function visitBlogHolder(DataObject $object) {
$this->urls[] = $object->Link();
if( !$object->Children() ) {
return;
}
foreach( $object->Children() as $children ) {
$this->urls[] = $children->Link();
}
}
public function visitIndexPage(DataObject $object) {
$this->urls[] = $object->Link();
}
public function __destruct() {
file_put_contents("/tmp/urls", var_export(array_unique($this->urls), true));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment