Skip to content

Instantly share code, notes, and snippets.

@alanpich
Created March 4, 2013 14:06
Show Gist options
  • Save alanpich/5082433 to your computer and use it in GitHub Desktop.
Save alanpich/5082433 to your computer and use it in GitHub Desktop.
Wrapper class for loading a MODx instance from a specific path on disk and interacting with it
<?php
namespace AlanPich\Tools;
class ModxWrapper
{
/** @var \modX */
protected $modx;
/** @var string */
public $path;
public function __construct($path)
{
$this->path = $path;
define('MODX_API_MODE', 1);
/** @var \modX $modx */
require rtrim($path, '/') . '/index.php';
$this->modx = $modx;
}
/**
* Unset $modx property before serialization
*
* @return array
*/
public function __sleep()
{
return array('path');
}
/**
* Re-establish $modx property on unserialization
*/
public function __wakeup()
{
define('MODX_API_MODE', 1);
/** @var \modX $modx */
require rtrim($this->path, '/') . '/index.php';
$this->modx = $modx;
}
/**
* Passes method calls through to modX instance
*
* @param $name
* @param $args
* @return mixed
*/
public function __call($name, $args)
{
return call_user_func_array(array($this->modx, $name), $args);
}
/**
* Passes parameter gets through to modX instance
*
* @param $key
* @return mixed
*/
public function __get($key)
{
return $this->modx->$key;
}
/**
* Passes setter requests through to modx
*
* @param $key
* @param $val
* @return mixed
*/
public function __set($key, $val)
{
return $this->modx->$key = $val;
}
/**
* Passthru isset request to modX instance
*
* @param $key
* @return bool
*/
public function __isset($key)
{
return isset($this->modx->$key);
}
/**
* Passthru unset requests to modX instance
* @param $key
*/
public function __unset($key)
{
unset($this->modx->$key);
}
}
<?php
$modx = new \AlanPich\Tools\ModxWrapper('/path/to/modx/root');
$siteName = $modx->getOption('site_name');
$res = $modx->newObject('modResource');
$res->fromArray(array(
'pagetitle' => 'Hello World',
'introtext' => 'This was created on the command line!'
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment