Example #1
0
 /**
  * @param string $file_template
  * @param string $template_type
  */
 function generate_file($file_template, $template_type)
 {
     $filepath = $file_template;
     if (!is_file($filepath)) {
         if (!is_file($template_file = $this->get_template_dir("{$template_type}.php"))) {
             Util::log_error("Template file {$template_file} does not exist.");
         }
         $generator_slug = static::generator_slug();
         $object_name = Util::underscorify($generator_slug);
         extract(array($object_name => $this->object, 'generator' => $this), EXTR_SKIP);
         unset($file_template, $generator_slug);
         ob_start();
         require $template_file;
         $source = ob_get_clean();
         // @todo which of '#ims' do we need?
         if (preg_match_all("#(.*?)(\n+(\\s+))?\\[\\s*@include\\s*\\(\\s*(.+?)\\s*\\)\\s*\\]#ims", "{$source}[@include(~~~)]", $matches, PREG_SET_ORDER)) {
             if (1 < count($matches[0])) {
                 $include = array();
                 foreach ($matches as $match) {
                     /**
                      * Capture what comes before the [@include({filename})]
                      */
                     $include[] = $match[1];
                     if ('~~~' !== $match[4]) {
                         ob_start();
                         /**
                          * Capture what is in /includes/{filename}.php
                          */
                         require dirname($template_file) . "/includes/{$match[4]}.php";
                         $include[] = $match[2] . ltrim(implode(array_map(function ($line) use($match) {
                             return "{$match[3]}{$line}";
                         }, explode("\n", ob_get_clean()))));
                     }
                 }
                 $source = implode($include);
             }
         }
         Util::ensure_dir(dirname($filepath));
         file_put_contents($filepath, $source);
     }
 }