Skip to content

Instantly share code, notes, and snippets.

@bastianccm
Created July 2, 2014 12:29
Show Gist options
  • Save bastianccm/1a60c22e214b6840be72 to your computer and use it in GitHub Desktop.
Save bastianccm/1a60c22e214b6840be72 to your computer and use it in GitHub Desktop.
Persistent Magento
<?php
// put in magento root folder
// run via
// rm /tmp/magento; php persistent.php
// output is in /tmp/magento.log
function plog($msg) {
file_put_contents('/tmp/magento.log', date('H:i:s') . ': ' . $msg . PHP_EOL, FILE_APPEND);
}
error_reporting(E_ALL | E_STRICT);
define('MAGENTO_ROOT', getcwd());
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
umask(0);
/* Store or website code */
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';
/* Run store or run website */
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';
#Mage::run($mageRunCode, $mageRunType);
#/** @var Mage_Core_Model_App $mageApp */
Mage::init($mageRunCode, $mageRunType);
$mageApp = Mage::app();
$mageApp->getFrontController();
class Persistent_Response extends Mage_Core_Controller_Response_Http
{
public function sendHeaders()
{
plog(__METHOD__);
return $this;
}
public function sendResponse()
{
plog(__METHOD__);
$this->outputBody();
return $this;
}
public function setRedirect($url, $code = 302)
{
mageDebugBacktrace(false, false);
plog(__METHOD__ . '::' . $url);
return $this;
}
}
$mageApp->setResponse(new Persistent_Response());
$socket = stream_socket_server('unix:///tmp/magento');
plog('Server running');
while ($client = stream_socket_accept($socket, 3600)) {
$len = unpack('I', fread($client, 4));
$len = $len[1];
$url = fread($client, $len);
plog("Got msg $len: $url");
if (pcntl_fork() == 0) {
Mage::getSingleton('core/resource')->getConnection('core_write')->closeConnection();
$request = new Mage_Core_Controller_Request_Http($url);
$request->setPathInfo();
$mageApp->setRequest($request);
ob_start();
$mageApp->getFrontController()->dispatch();
$result = ob_get_contents();
ob_end_clean();
fwrite($client, pack('I', strlen($result)));
fwrite($client, $result);
fclose($client);
}
}
<?php
// put in magento root folder
// access this file via browser instead of magento index.php
$client = stream_socket_client('unix:///tmp/magento');
$url = 'http://mage.local/magento-mirror/';
fwrite($client, pack('I', strlen($url))); // sending strlen
fwrite($client, $url); // sending url
$len = unpack('I', fread($client, 4));
$len = $len[1];
echo fread($client, $len);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment