Agel Nash created very useful plugin that allows to manage chunks and snippets with IDE only. The idea is to store all chunks and snippets in files. And MODX will load them automatically on start. If you create new file in chunks folder you can work with it immediately, there's no need to switch to manager and create new DB record there.
Such way of development allows to save time and to use version control systems for chunks and snippets.
<?php
if (!defined('MODX_BASE_PATH')) { die('HACK???'); }
/**
* LoadElement
*
* Loading chunks and snippets from files
*
* @license GNU General Public License (GPL), http://www.gnu.org/copyleft/gpl.html
* @author Agel_Nash <[email protected]>
* @version 0.1
* @internal @events OnWebPageInit, OnManagerPageInit
* @internal @properties &extChunk=Chunks extensions (<i>comma separated</i>);input;txt,html &extSnippet=Snippets extensions (<i>comma separated</i>);input;php &pathElement=Elemets folder (<i>relative to site root</i>);input;assets/element/
*/
class LoadElement{
public static $pathElement = 'assets/element/';
/**
* Elements type validation
*
* @param string $element elements type
* @return bool
*/
protected static function validate($element){
return is_dir(static::getPath($element));
}
/**
* Elements folder path
*
* @param string $element elements type
* @return string
*/
protected static function getPath($element){
return MODX_BASE_PATH . static::$pathElement . $element.'/';
}
/**
* Get name of the method containing rules to load elements
*
* @param string $element тип элементов
* @return string
*/
protected static function getMethodName($element){
return 'get'.ucfirst($element);
}
/**
* Rules to load snippets
*
* @param DocumentParser $modx
* @param SplFileInfo $item detected element
* @return bool status of elements loading
*/
protected static function getSnippet(DocumentParser $modx, SplFileInfo $item){
$snippetName = $item->getBasename('.'.$item->getExtension());
$modx->snippetCache[$snippetName] = "return require '".$item->getRealPath()."';";
$modx->snippetCache[$snippetName . "Props"] = array();
return true;
}
protected static function getChunk(DocumentParser $modx, SplFileInfo $item){
$chunkName = $item->getBasename('.'.$item->getExtension());
$modx->chunkCache[$chunkName] = file_get_contents($item->getRealPath());
return true;
}
/**
* Run elements creation
*
* @param DocumentParser $modx
* @param string $element
* @return bool
*/
public static function run(DocumentParser $modx, $element, array $ext = array()){
if( ! static::validate($element) ) return false;
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
static::getPath($element),
RecursiveDirectoryIterator::SKIP_DOTS
), RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $item) {
/**
* @var SplFileInfo $item
*/
if($item->isFile() && $item->isReadable() && (empty($ext) || in_array($item->getExtension(), $ext))){
$name = static::getMethodName($element);
static::$name($modx, $item);
}
}
return true;
}
}
LoadElement::$pathElement = (!empty($pathElement) && is_scalar($pathElement)) ? $pathElement : LoadElement::$pathElement;
$extSnippet = (!empty($extSnippet) && is_scalar($extSnippet)) ? $extSnippet : 'txt,html';
$extSnippet = array_map('trim', explode(",", $extSnippet));
LoadElement::run($modx, 'snippet', $extSnippet);
$extChunk = (!empty($extChunk) && is_scalar($extChunk)) ? $extChunk : 'txt,html';
$extChunk = array_map('trim', explode(",", $extChunk));
LoadElement::run($modx, 'chunk', $extChunk);
Create folder for elements (default is assets/element/) and 2 subfolders there:
- snippet - for snippets;
- chunk - for chunks.
After that try to create any file with .php extension in /assets/element/snippet/. The name of this file will be the name of the new snippet. The same with chunks (but file extension should be .txt or .html). It's worth to mention that elements are not created in manager - they are virtual, so you can disable them changing file extension or sort creating additional subfolders.
Look at demo:
http://www.youtube.com/watch?v=q_p2TDT0RKA&feature=player_embedded
Original article in Russian:
http://blog.agel-nash.ru/2014/2/ide-and-modx-evolution.html