Created
March 4, 2015 11:57
-
-
Save Mark-H/af009f895ffbabd3dc58 to your computer and use it in GitHub Desktop.
Object Oriented Snippets for MODX :D There are a couple of files here. The first one (`1_interface_and_base_class.php`) contains an interface and the base abstract class for creating snippets. The second file (`2_example_snippet.php`) is a real basic example of a snippet. The third file (`3_modSnippet.php`) is the code that is in the actual modS…
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 | |
namespace mhwd\Donations; | |
/** | |
* Interface ObjectOrientedSnippet | |
* | |
* @see Snippet class | |
* | |
* @package mhwd\Donations | |
*/ | |
interface ObjectOrientedSnippet { | |
public function __construct(\modX $modx, array $properties = array()); | |
public function execute(); | |
public function getDefaultProperties(); | |
public function getProperties(); | |
public function getProperty($key, $default = null); | |
} | |
/** | |
* Class Snippet | |
* | |
* Base implementation of the ObjectOrientedSnippet interface. Implement the execute() method. | |
* | |
* @package mhwd\Donations | |
*/ | |
abstract class Snippet implements ObjectOrientedSnippet { | |
protected $modx; | |
protected $properties = array(); | |
/** | |
* Creates a new ObjectOrientedSnippet instance, setting $modx to the internal $this->modx and setting | |
* properties to $this->properties. | |
* | |
* @param \modX $modx | |
* @param array $properties | |
*/ | |
public function __construct(\modX $modx, array $properties = array()) | |
{ | |
$this->modx =& $modx; | |
$this->properties = array_merge($this->getDefaultProperties(), $properties); | |
} | |
/** | |
* Returns an array of each property passed to the snippet. | |
* | |
* @return array | |
*/ | |
public function getProperties() | |
{ | |
return $this->properties; | |
} | |
/** | |
* Grabs a specific property and returns its value, or the default value. | |
* | |
* @param $key | |
* @param null $default | |
* @return mixed | |
*/ | |
public function getProperty($key, $default = null) | |
{ | |
if (isset($this->properties[$key])) { | |
return $this->properties[$key]; | |
} | |
return $default; | |
} | |
} |
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 | |
namespace mhwd\Donations; | |
class DonationsSnippet extends Snippet { | |
/** | |
* Executes the snippet and returns the result (string). | |
* | |
* @return string | |
*/ | |
public function execute() | |
{ | |
return 'Whoohee! It works! ' . print_r($this->getProperties(), true); | |
} | |
/** | |
* Returns an array of default properties for the snippet. | |
* | |
* @return array | |
*/ | |
public function getDefaultProperties() | |
{ | |
return array( | |
'foo' => 'bar' | |
); | |
} | |
} |
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 | |
/** | |
* @var modX $modx | |
* @var Donations $donations | |
* @var array $scriptProperties | |
*/ | |
$modelPath = $modx->getOption('donations.core_path', null, MODX_CORE_PATH . 'components/donations/') . 'model/donations/'; | |
$donations = $modx->getService('donations', 'Donations', $modelPath); | |
if (!$donations || !($donations instanceof Donations)) { | |
$modx->log(modX::LOG_LEVEL_ERROR, '[Donations] Could not load Donations service class from ' . $modelPath); | |
return '<p class="error">Sorry, donations are currently unavailable.</p>'; | |
} | |
return $donations->runSnippet('Donations', $scriptProperties); |
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 Donations | |
{ | |
/** @var modX $modx */ | |
public $modx; | |
public $config = array(); | |
public $debug = 0; | |
/** | |
* Creates a new Donations instance. | |
* | |
* @param \modX $modx | |
* @param array $config | |
*/ | |
function __construct(\modX &$modx, array $config = array()) | |
{ | |
$this->modx =& $modx; | |
$corePath = $this->modx->getOption( | |
'donations.core_path', | |
$config, | |
$this->modx->getOption('core_path') . 'components/donations/' | |
); | |
$assetsUrl = $this->modx->getOption( | |
'donations.assets_url', | |
$config, | |
$this->modx->getOption('assets_url') . 'components/donations/' | |
); | |
$assetsPath = $this->modx->getOption( | |
'donations.assets_path', | |
$config, | |
$this->modx->getOption('assets_path') . 'components/donations/' | |
); | |
$this->config = array_merge( | |
array( | |
'basePath' => $corePath, | |
'corePath' => $corePath, | |
'modelPath' => $corePath . 'model/', | |
'controllersPath' => $corePath . 'controllers/', | |
'processorsPath' => $corePath . 'processors/', | |
'elementsPath' => $corePath . 'elements/', | |
'templatesPath' => $corePath . 'templates/', | |
'assetsPath' => $assetsPath, | |
'jsUrl' => $assetsUrl . 'js/', | |
'cssUrl' => $assetsUrl . 'css/', | |
'assetsUrl' => $assetsUrl, | |
'connectorUrl' => $assetsUrl . 'connector.php', | |
), | |
$config | |
); | |
$this->modx->lexicon->load('donations:default'); | |
} | |
public function runSnippet($snippet, array $properties = array()) | |
{ | |
if (!class_exists('Snippet')) { | |
require_once dirname(__FILE__) . '/Snippet.php'; | |
} | |
$fqn = '\mhwd\Donations\\' . ucfirst($snippet) . 'Snippet'; | |
$file = $this->config['modelPath'] . 'donations/' . ucfirst($snippet) . 'Snippet.php'; | |
if (file_exists($file)) { | |
require_once $file; | |
} | |
if (class_exists($fqn)) { | |
$snippet = new $fqn($this->modx, $properties); | |
if ($snippet instanceof \mhwd\Donations\ObjectOrientedSnippet) { | |
return $snippet->execute(); | |
} | |
} | |
return 'Snippet ' . $snippet . ' not found :('; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment