Skip to content

Commit

Permalink
The chef server now bakes/processes files on-demand.
Browse files Browse the repository at this point in the history
  • Loading branch information
ludovicchabant committed Jan 17, 2012
1 parent 15215a6 commit 6f294c5
Showing 1 changed file with 89 additions and 6 deletions.
95 changes: 89 additions & 6 deletions _piecrust/src/PieCrust/Server/PieCrustServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PieCrust\PieCrust;
use PieCrust\PieCrustException;
use PieCrust\PieCrustErrorHandler;
use PieCrust\Baker\DirectoryBaker;
use PieCrust\Page\PageRepository;


Expand All @@ -24,7 +25,11 @@ class PieCrustServer
{
protected $server;
protected $rootDir;
protected $logger;
protected $debugMode;

protected $bakeCacheDir;
protected $bakeCacheFiles;

/**
* Creates a new chef server.
Expand All @@ -33,6 +38,15 @@ public function __construct($appDir, array $options = array(), $logger = null)
{
$this->rootDir = rtrim($appDir, '/\\');

$pieCrust = new PieCrust(array(
'root' => $this->rootDir,
'cache' => true
)
);
$this->bakeCacheDir = $pieCrust->getCacheDir() . 'server_cache';
if (!is_dir($this->bakeCacheDir))
mkdir($this->bakeCacheDir);

$options = array_merge(
array(
'port' => 8080,
Expand All @@ -49,10 +63,11 @@ public function __construct($appDir, array $options = array(), $logger = null)
require_once 'Log.php';
$logger = \Log::singleton('null', '', '');
}
$this->logger = $logger;

// Set-up the stupid web server.
$port = $options['port'];
$this->server = new StupidHttp_WebServer($this->rootDir, $port);
$this->server = new StupidHttp_WebServer($this->bakeCacheDir, $port);
if ($logger != null)
{
$this->server->addLog(new StupidHttp_PearLog($logger));
Expand All @@ -77,15 +92,47 @@ public function __construct($appDir, array $options = array(), $logger = null)
{
$self->_runPieCrustRequest($context);
});
$this->server->setPreprocess(function($req) use ($self) { $self->_preprocessRequest($req); });
}

/**
* Runs the chef server.
*/
public function run(array $options = array())
{
$bakedFiles = $this->prebake();

// Build a reverse-index of what file creates each output file.
$this->bakeCacheFiles = array();
foreach ($bakedFiles as $f => $info)
{
foreach ($info['outputs'] as $out)
{
$this->bakeCacheFiles[$out] = $f;
}
}

$this->server->run($options);
}

/**
* For internal use only.
*/
public function _preprocessRequest(\StupidHttp_WebRequest $request)
{
$documentPath = $this->bakeCacheDir . $request->getUri();
if (is_file($documentPath))
{
if (isset($this->bakeCacheFiles[$documentPath]))
{
$this->prebake(
$request->getServerVariables(),
$this->bakeCacheFiles[$documentPath],
true
);
}
}
}

/**
* For internal use only.
Expand All @@ -104,11 +151,11 @@ public function _runPieCrustRequest(\StupidHttp_HandlerContext $context)
PageRepository::clearPages();

$pieCrust = new PieCrust(array(
'root' => $this->rootDir,
'cache' => true
),
$context->getRequest()->getServerVariables()
);
'root' => $this->rootDir,
'cache' => true
),
$context->getRequest()->getServerVariables()
);
$pieCrust->getConfig()->setValue('server/is_hosting', true);
$pieCrust->getConfig()->setValue('site/cache_time', false);
$pieCrust->getConfig()->setValue('site/pretty_urls', true);
Expand Down Expand Up @@ -181,4 +228,40 @@ public function _runPieCrustRequest(\StupidHttp_HandlerContext $context)
$context->getLog()->logError(" PieCrust error: " . $pieCrustException->getMessage());
}
}

protected function prebake($server = null, $path = null, $smart = false)
{
$pieCrust = new PieCrust(array(
'root' => $this->rootDir,
'cache' => true
),
$server
);

$parameters = $pieCrust->getConfig()->getValue('baker');
if ($parameters == null)
$parameters = array();
$parameters = array_merge(array(
'smart' => true,
'processors' => '*',
'skip_patterns' => array(),
'force_patterns' => array()
),
$parameters
);

$dirBaker = new DirectoryBaker($pieCrust,
$this->bakeCacheDir,
array(
'smart' => $smart,
'skip_patterns' => $parameters['skip_patterns'],
'force_patterns' => $parameters['force_patterns'],
'processors' => $parameters['processors']
),
$this->logger
);
$dirBaker->bake($path);

return $dirBaker->getBakedFiles();
}
}

0 comments on commit 6f294c5

Please sign in to comment.