Created
August 23, 2011 11:17
-
-
Save stojg/1164878 to your computer and use it in GitHub Desktop.
Silverstripe visitor pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Object::add_extension('SiteTree', 'ChangeDecorator'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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