Skip to content

Commit

Permalink
Added Asset provider. Added simple Logger.
Browse files Browse the repository at this point in the history
  • Loading branch information
mermshaus committed Mar 27, 2012
1 parent 4d34b5b commit a213134
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 37 deletions.
18 changes: 7 additions & 11 deletions library/Phpsog/Application.php
Expand Up @@ -11,6 +11,7 @@
use SplFileInfo;

use Phpsog\Provider\Html\Provider as HtmlProvider;
use Phpsog\Provider\Asset\Provider as AssetProvider;

use Phpsog\Exporter;

Expand All @@ -30,6 +31,8 @@ function e($s)
*/
class Application
{
const VERSION = '0.1.0';

/**
* @var array
*/
Expand Down Expand Up @@ -152,30 +155,23 @@ public function processResources()
$dirIter = new RecursiveDirectoryIterator($config['project.dir']
. '/' . $config['resources.dir']);
} catch (UnexpectedValueException $e) {
echo ' Found no resource directory.' . PHP_EOL;
echo ' Found no resource directory.' . "\n";
return;
}

$recursiveIterator = new RecursiveIteratorIterator($dirIter,
RecursiveIteratorIterator::SELF_FIRST,
RecursiveIteratorIterator::CATCH_GET_CHILD);

$provider = new AssetProvider($this->config, $this->exporter, $this->pathHelper);

foreach ($recursiveIterator as $file => $unused) {

if (!is_file($file)) {
continue;
}

$relativePath = substr($file, strlen($config['project.dir']
. '/' . $config['resources.dir'] . '/'));

$exportPath = $config['project.dir'] . '/' . $config['export.dir']
. '/' . pathinfo($relativePath, PATHINFO_DIRNAME)
. '/' . basename($relativePath);

$content = file_get_contents($file);

$this->exporter->export($exportPath, $content);
$provider->compile(new SplFileInfo($file));
}
}

Expand Down
32 changes: 25 additions & 7 deletions library/Phpsog/Exporter.php
Expand Up @@ -3,6 +3,7 @@
namespace Phpsog;

use Kaloa\Filesystem\PathHelper;
use Phpsog\Logger;

class Exporter
{
Expand All @@ -12,13 +13,33 @@ class Exporter
*/
protected $pathHelper;

/**
*
* @var Logger
*/
protected $logger;

/**
*
* @param PathHelper $pathHelper
*/
public function __construct(PathHelper $pathHelper)
public function __construct(PathHelper $pathHelper, Logger $logger)
{
$this->pathHelper = $pathHelper;
$this->logger = $logger;
}

public function mkdir($path, $rights = 0777, $recursive = true)
{
$normPath = $this->pathHelper->normalize($path);

if (strpos($normPath, getcwd()) === 0) {
$normPath = substr($normPath, strlen(getcwd()));
$normPath = ltrim($normPath, '/');
}

mkdir($path, $rights, $recursive);
$this->logger->log(' [mkdir] ' . $normPath);
}

/**
Expand All @@ -28,21 +49,18 @@ public function __construct(PathHelper $pathHelper)
*/
public function export($exportPath, $content)
{
$ph = $this->pathHelper;

if (!file_exists(dirname($exportPath))) {
mkdir(dirname($exportPath), 0777, true);
echo ' [mkdir] ' . $ph->normalize(dirname($exportPath)) . PHP_EOL;
$this->mkdir(dirname($exportPath));
}

$normPath = $ph->normalize($exportPath);
$normPath = $this->pathHelper->normalize($exportPath);

if (strpos($normPath, getcwd()) === 0) {
$normPath = substr($normPath, strlen(getcwd()));
$normPath = ltrim($normPath, '/');
}

echo ' [export] ' . $normPath . PHP_EOL;
$this->logger->log(' [export] ' . $normPath);

file_put_contents($exportPath, $content);
}
Expand Down
15 changes: 15 additions & 0 deletions library/Phpsog/Logger.php
@@ -0,0 +1,15 @@
<?php

namespace Phpsog;

/**
*
* @author Marc Ermshaus <marc@ermshaus.org>
*/
class Logger
{
public function log($text)
{
echo $text . "\n";
}
}
67 changes: 67 additions & 0 deletions library/Phpsog/Provider/Asset/Provider.php
@@ -0,0 +1,67 @@
<?php

namespace Phpsog\Provider\Asset;

use Phpsog\Exporter;
use SplFileInfo;
use Kaloa\Filesystem\PathHelper;

/**
*
* @author Marc Ermshaus <marc@ermshaus.org>
*/
class Provider
{
/**
*
* @var array
*/
protected $config;

/**
*
* @var Exporter
*/
protected $exporter;

/**
*
* @var PathHelper
*/
protected $pathHelper;

/**
*
* @param array $config
* @param Exporter $exporter
* @param PathHelper $pathHelper
*/
public function __construct(array $config, Exporter $exporter,
PathHelper $pathHelper)
{
$this->config = $config;
$this->exporter = $exporter;
$this->pathHelper = $pathHelper;
}

/**
*
* @param SplFileInfo $file
*/
public function compile(SplFileInfo $file)
{
$file = $file->getPathname();
$config = $this->config;

$relativePath = substr($file, strlen($config['project.dir']
. '/' . $config['resources.dir'] . '/'));

$exportPath = $config['project.dir'] . '/' . $config['export.dir']
. '/' . pathinfo($relativePath, PATHINFO_DIRNAME)
. '/' . basename($relativePath);

$content = file_get_contents($file);

$this->exporter->export($exportPath, $content);
}
}
9 changes: 6 additions & 3 deletions library/Phpsog/Provider/Html/Provider.php
Expand Up @@ -6,10 +6,11 @@
use SplFileInfo;
use Kaloa\Filesystem\PathHelper;

use Phpsog\Provider\Html\View;
#use Phpsog\Provider\Html\View;

/**
*
* @author Marc Ermshaus <marc@ermshaus.org>
*/
class Provider
{
Expand Down Expand Up @@ -49,7 +50,8 @@ class Provider
* @param Exporter $exporter
* @param PathHelper $pathHelper
*/
public function __construct(array $config, Exporter $exporter, PathHelper $pathHelper)
public function __construct(array $config, Exporter $exporter,
PathHelper $pathHelper)
{
$this->config = $config;
$this->exporter = $exporter;
Expand Down Expand Up @@ -139,7 +141,8 @@ public function compile(SplFileInfo $file)
);

$contentx = $this->fillLayout($config['project.dir']
. '/' . $config['layouts.dir'] . '/' . $this->layout, $vars);
. '/' . $config['layouts.dir'] . '/' . $this->layout,
$vars);

$relativePath = substr($file, strlen($config['project.dir']
. '/' . $config['pages.dir'] . '/'));
Expand Down
47 changes: 31 additions & 16 deletions phpsog
@@ -1,42 +1,57 @@
#!/usr/bin/env php
<?php

if ((!@include __DIR__.'/../../.composer/autoload.php') && (!@include __DIR__.'/vendor/.composer/autoload.php')) {
die('You must set up the project dependencies, run the following commands:'.PHP_EOL.
'curl -s http://getcomposer.org/installer | php'.PHP_EOL.
'php composer.phar install'.PHP_EOL);
if (
(!@include __DIR__.'/../../.composer/autoload.php')
&& (!@include __DIR__.'/vendor/.composer/autoload.php')
) {
die('You must set up the project dependencies, run the following commands:'
."\n".
'curl -s http://getcomposer.org/installer | php'."\n".
'php composer.phar install'."\n");
}

use Phpsog\Application;
use Phpsog\Exporter;
use Phpsog\Logger;
use Kaloa\Filesystem\PathHelper;

try {
$params = array(
'config' => ''
);

if ($argc !== 2) {
$dispatched = false;

if ($_SERVER['argc'] !== 2) {
throw new Exception('Wrong argument count');
} else {
$params['config'] = $argv[1];
if ($_SERVER['argv'][1] === '--version') {
echo 'phpsog ' . Application::VERSION . "\n";
$dispatched = true;
} else {
$params['config'] = $_SERVER['argv'][1];
}
}

if (is_dir($params['config'])) {
$params['config'] .= '/phpsog.ini';
}
if (!$dispatched) {
if (is_dir($params['config'])) {
$params['config'] .= '/phpsog.ini';
}

$ph = new PathHelper();
$ph = new PathHelper();
$logger = new Logger();

$phpsog = new Application(new Exporter($ph), $ph);
$phpsog = new Application(new Exporter($ph, $logger), $ph);

$phpsog->loadConfig($params['config']);
$phpsog->loadConfig($params['config']);

$phpsog->sanitizeEnvironment();
$phpsog->sanitizeEnvironment();

$phpsog->processHtmlProvider();
$phpsog->processResources();
$phpsog->processHtmlProvider();
$phpsog->processResources();
}

} catch (Exception $e) {
die('Error: ' . $e->getMessage() . PHP_EOL);
die('Error: ' . $e->getMessage() . "\n");
}

0 comments on commit a213134

Please sign in to comment.