/**
  * Writes the generated code to a .php file
  *
  * @return void
  */
 public function write()
 {
     if (!file_exists("code/{$this->key}.php")) {
         $fh = fopen("code/{$this->key}.php", "w");
         $path = SilverSmith::get_script_dir() . "/code/lib/structures/PageType.bedrock";
         if (file_exists($path)) {
             $template = new BedrockTemplate($path);
             $template->bind($this);
             fwrite($fh, $template->render());
             fclose($fh);
         }
     }
 }
 /**
  * Creates a PHP file on the disk for the class that will define this node
  *
  */
 public function createFile()
 {
     $path = $this->getFilePath();
     $stock = $this->getDecorator() ? "Decorator" : $this->getContentType();
     $content = file_get_contents(SilverSmith::get_script_dir() . "/code/lib/structures/{$stock}.bedrock");
     $template = new BedrockTemplate($content);
     $template->bind($this);
     $new_content = str_replace("[?php", "<?php", $template->render());
     $fh = fopen($path, "w");
     fwrite($fh, $new_content);
     fclose($fh);
 }
Beispiel #3
0
 /**
  * Creates any templates that do not exist yet, unless the "force" parameter is specified
  *
  * @see "silversmith help"	 
  * @param The parameters, e.g. from the command line
  */
 public static function build_templates($params = array())
 {
     $theme_dir = isset($params['theme']) ? "themes/" . $params['theme'] : false;
     $force = isset($params['force']);
     $specificTemplates = isset($params['list']) ? explode(',', $params['list']) : false;
     if (!$theme_dir) {
         if (self::$project_dir == project()) {
             if (SSViewer::current_theme()) {
                 $theme_dir = "themes/" . SSViewer::current_theme();
             } else {
                 $theme_dir = project();
             }
         } else {
             $theme_dir = self::$project_dir;
         }
     }
     say("Using theme directory {$theme_dir}");
     if (!file_exists($theme_dir)) {
         fail("The theme directory {$theme_dir} does not exist");
     }
     if (!is_dir($theme_dir . "/templates")) {
         mkdir($theme_dir . "/templates");
     }
     $layout_dir = "{$theme_dir}/templates/Layout";
     if (!is_dir($layout_dir)) {
         mkdir($layout_dir);
     }
     $source = "Page.ss";
     if (isset($params['source'])) {
         $source = $params['source'];
     }
     if (!file_exists("{$layout_dir}/{$source}")) {
         fail("Source template {$layout_dir}/{$source} does not exist.");
     }
     if (!file_exists(self::$yaml_path)) {
         fail("File " . self::$yaml_path . " does not exist.");
     }
     SilverSmithProject::load(self::$yaml_path);
     $created = 0;
     say(cell("Status", 11, true, "grey", "on_white") . cell("File", 30, true, "grey", "on_white") . cell("Result", 40, true, "grey", "on_white"));
     line();
     foreach (SilverSmithProject::get_page_types() as $node) {
         if ($node->getKey() == "SiteConfig") {
             continue;
         }
         if ($specificTemplates && !in_array($node->getKey(), $specificTemplates)) {
             continue;
         }
         if (!file_exists("{$layout_dir}/{$node->getKey()}.ss") || $force) {
             $stock = file_get_contents("{$layout_dir}/{$source}");
             $fh = fopen("{$layout_dir}/{$node->getKey()}.ss", "w");
             $created++;
             $notes = "Copied from {$source}";
             if (isset($params['autofill'])) {
                 if (!preg_match('/\\$Content[^A-Za-z0-9_]/', $stock)) {
                     say(cell("Skipped", 2, "white", "on_red") . cell($node->getKey() . ".ss", 30) . cell("Varible \$Content is not in the template.", 40));
                     continue;
                 }
                 $notes .= " [Auto-filled]";
                 $template = new BedrockTemplate(file_get_contents(self::$script_dir . "/code/lib/structures/AutoFill.bedrock"));
                 $template->bind($node);
                 $autofill = $template->render();
                 $filled = preg_replace('/\\$Content([^A-Za-z0-9_])/', "\$Content\n\n{$autofill}\n\n\$1", $stock);
                 fwrite($fh, $filled);
             } else {
                 fwrite($fh, $stock);
             }
             fclose($fh);
             say(cell("Created", 10, true, "white", "on_green") . cell($node->getKey() . ".ss", 30) . cell($notes, 40));
         } else {
             say(cell("Bypassed", 11, true, "grey", "on_yellow") . cell($node->getKey() . ".ss", 30) . cell("File exists. Use --force to override", 40));
         }
     }
     line();
     say("{$created} templates created.");
 }
 /**
  * Get the "update" code for the form field, i.e. configuring the FormField object
  * after instantiation
  *
  * @return string
  */
 public function getUpdate()
 {
     if ($yml = $this->getConfig()) {
         if ($content = $yml->getUpdate()) {
             $template = new BedrockTemplate(SilverSmithUtil::tabify($content));
             $template->bind($this);
             return $template->render();
         }
     }
 }