Example #1
0
 /**
  * Execute compile command
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     //get project name & set source/output dirs
     $project = $input->getArgument('project');
     $this->config = new \Implico\Email\Config($project, $input->getOption('dir'));
     if ($error = $this->config->getErrors()) {
         switch ($error) {
             case 'projectNotFound':
                 $output->writeln('<fg=red>ERROR: project directory not found</fg=red>');
                 exit(1);
                 break;
         }
     }
     SmartyUtils::init($this->config);
     //get script name(s)
     $scripts = $input->getOption('script');
     //if script name(s) not passed, set all scripts (exclude dirs)
     if (!$scripts) {
         $scripts = array_filter(array_diff(scandir($this->config['scriptsDir']), array('.', '..')), function ($script) {
             return !is_dir($this->config['scriptsDir'] . $script);
         });
     }
     //add ".tpl" extension when applicable
     foreach ($scripts as $i => $script) {
         if (strpos($script, '.') === false) {
             $scripts[$i] = $script . '.tpl';
         }
     }
     //get watch option
     $watch = $input->getOption('watch');
     //get output option
     $outputMode = $input->getOption('output');
     //create & configure Smarty object
     $smarty = new \Smarty();
     $smarty->setCompileDir(IE_SMARTY_COMPILE_DIR);
     $smarty->addPluginsDir(IE_SMARTY_PLUGINS_DIR);
     $smarty->addPluginsDir(IE_SMARTY_CUSTOM_PLUGINS_DIR);
     $smarty->compile_check = false;
     $smarty->force_compile = true;
     $smarty->error_reporting = E_ALL;
     $smarty->registerClass('SmartyUtils', 'Implico\\Email\\Utils\\Smarty');
     //set directories
     $smarty->setTemplateDir(array(0 => $this->config['dir'], 'core' => IE_CORE_DIR, 'layouts' => $this->config['layoutsDir'], 'scripts' => $this->config['scriptsDir'], 'styles' => $this->config['stylesDir']));
     //master config file
     $smarty->configLoad(IE_CORE_DIR . 'config.conf');
     //optional master custom config file
     $customConf = IE_CUSTOM_DIR . 'config.conf';
     if (file_exists($customConf)) {
         $smarty->configLoad($customConf);
     }
     //console message for watching
     if ($watch) {
         $output->writeln('Watching for changes...');
     }
     //main loop - watch for changes (or execute once if not watching)
     $compileNo = 1;
     $compileDirStamp = '';
     //dirs to inspect file change
     $checkDirs = array($this->config['configsDir'], $this->config['configsScriptsDir'], $this->config['layoutsDir'], $this->config['scriptsDir'], $this->config['stylesDir']);
     //set output mode variables
     $outputMinified = in_array('m', $outputMode);
     $outputFormatted = in_array('f', $outputMode);
     //formatter object
     $formatter = null;
     //css inliner object
     $cssToInlineStyles = new CssToInlineStyles();
     while (true) {
         //compile only if not watching or the dirs filestamp changes
         if (!$watch || $compileDirStamp != $this->getDirStamp($checkDirs)) {
             //clear compiled templates
             $smarty->clearCompiledTemplate();
             //Smarty assign project-specific config file path
             $configFile = $this->config['configsDir'] . 'config.conf';
             $loadConfigFile = file_exists($configFile);
             //set random complile_id (forces Smarty to compile)
             $smarty->compile_id = uniqid();
             //list of compiled scripts
             $compiledScripts = $scripts;
             //fetch & save templates
             foreach ($scripts as $i => $script) {
                 //script name without extension
                 $scriptName = substr($script, 0, strrpos($script, '.'));
                 $smarty->clearConfig();
                 if ($loadConfigFile) {
                     $smarty->configLoad($configFile);
                 }
                 //set script-specific config file path if exists
                 $configFileScript = $this->config['configsScriptsDir'] . $scriptName . '.conf';
                 if (file_exists($configFileScript)) {
                     $smarty->configLoad($configFileScript);
                 }
                 //lazy create indenter
                 if ($outputFormatted && !$formatter) {
                     $formatter = new \Gajus\Dindent\Indenter(array('indentation_character' => $smarty->getConfigVars('indentChar')));
                 }
                 //set encoding
                 $outputEncoding = $smarty->getConfigVars('encoding');
                 if (!$outputEncoding) {
                     $outputEncoding = 'utf-8';
                 }
                 $outputEncodingUtf8 = strtoupper($outputEncoding) == 'UTF-8';
                 try {
                     //get the html
                     $html = $smarty->fetch($this->config['scriptsDir'] . $script);
                     //get inline styles
                     $inlineCss = $smarty->fetch($this->config['stylesDir'] . 'inline.tpl');
                     if (trim($inlineCss)) {
                         $cssToInlineStyles->setHTML($html);
                         $cssToInlineStyles->setCSS($inlineCss);
                         $html = $cssToInlineStyles->convert();
                     }
                     //save minified
                     if ($outputMinified) {
                         $htmlSave = $html;
                         if (!$outputEncodingUtf8) {
                             $htmlSave = mb_convert_encoding($htmlSave, $outputEncoding, 'utf-8');
                         }
                         //max line width = 900 chars
                         $maxPerLine = 750;
                         $endLine = false;
                         $newHtml = '';
                         for ($i = 0; $i < mb_strlen($htmlSave, $outputEncoding); $i++) {
                             if ($i % $maxPerLine == 0 && $i > 0) {
                                 $endLine = true;
                             }
                             $curChar = mb_substr($htmlSave, $i, 1, $outputEncoding);
                             $newHtml .= $curChar;
                             if ($endLine) {
                                 if ($curChar == '>') {
                                     $newHtml .= PHP_EOL;
                                     $endLine = false;
                                 }
                             }
                         }
                         $htmlSave = $newHtml;
                         $this->saveOutput($this->config['outputsDir'] . $scriptName . '.min.html', $htmlSave);
                     }
                     //save formatted
                     if ($outputFormatted) {
                         $htmlSave = $formatter->indent($html);
                         if (!$outputEncodingUtf8) {
                             $htmlSave = mb_convert_encoding($htmlSave, $outputEncoding, 'utf-8');
                         }
                         $this->saveOutput($this->config['outputsDir'] . $scriptName . '.html', $htmlSave, true);
                     }
                 } catch (\Exception $e) {
                     $output->writeln('<fg=red>' . $e->getMessage() . '</fg=red>');
                     $compiledScripts[$i] .= ' <fg=red>(ERROR)</fg=red>';
                 }
             }
             //console info message
             $output->writeln(($watch ? '#' . $compileNo++ . ' ' : '') . 'Compiled ' . date('d-m-Y H:i:s') . ' ' . implode(', ', $compiledScripts));
         }
         //break if not watching
         if (!$watch) {
             break;
         }
         //calculate dirs filestamp to compare
         $compileDirStamp = $this->getDirStamp($checkDirs);
         //pause
         usleep(500000);
     }
 }
Example #2
0
 /**
  * Register a class for access to static methods / constants in a template. Especially
  * useful for classes outside the standard namespace.
  *
  * From the Smarty developers:
  *
  * > We have decided not to integrate namespace support into the template syntax.
  * > The goal of Smarty is to speparate the design as much as possible from the
  * > business logic. With namespace syntax we would more and more of business
  * > logic into the templates.
  * >
  * > Instead ou can register a class with optional namespace for the use in the template like:
  * >
  * > `$smarty->registerClass("FOO","\Fully\Qualified\Name\Foo");`
  *
  * @see http://www.smarty.net/forums/viewtopic.php?p=65279
  *
  * @param string $n The variable name for the class within the template
  * @param string $c The fully qualified class name
  */
 public function registerClass($n, $c)
 {
     $this->_smarty->registerClass($n, $c);
 }
Example #3
0
<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);
chdir('../');
$path = getcwd();
require 'vendor/autoload.php';
$cs = \MadLab\Cornerstone\App::getInstance($path);
//Detect Environments
/*
$cs->detectEnvironment(array(
    'local'=>'example.local',
    'production'=>'example.com'
));
*/
$smarty = new Smarty();
$smarty->setTemplateDir('pages');
$smarty->setCompileDir('storage/templates');
$smarty->registerClass('App', '\\MadLab\\Cornerstone\\App');
$template = new \MadLab\Cornerstone\Components\TemplateBridges\SmartyTemplateBridge($smarty);
$cs->setTemplateHandler($template);
include 'bootstrap.php';
$cs->run();