public function addNode($node, $parent) { if (!isset($this->source[$parent])) { $this->source[$parent] = array(); } if (!isset($this->source[$parent][$node])) { $this->source[$parent][$node] = array(); } $fh = fopen($this->ymlPath, "w"); fwrite($fh, SilverSmithUtil::array_to_yml($this->source)); fclose($fh); }
/** * This is the function that does most of the legwork for creating the PHP code for * this node. Note teh replacement of [?php tags to allow the eval() function * in {@link BedrockTemplate} to work properly * * @return array A list of the differences in the file */ public function updateFile() { $path = $this->getFilePath(); $content = file_get_contents($path); $original_content = $content; $content = str_replace("<?php", "[?php", $content); $content = SilverSmithUtil::replace_tags(BedrockDataRecord::$model_open, BedrockDataRecord::$model_close, "\n\n<@= GeneratedCode @>\n\n\t", $content); $template = new BedrockTemplate($content); $template->bind($this); $new_content = str_replace("[?php", "<?php", $template->render()); // Ensure getCMSFields if ($this->getIsFinal()) { $func = $this->getDecorator() ? "updateCMSFields" : "getCMSFields"; if (!preg_match('/function[\\s]+' . $func . '\\(/', $new_content)) { $new_content = str_replace(self::$model_close, self::$model_close . "\n\n" . $this->getGetCMSFieldsCode(), $new_content); } } // Ensure parent $pattern = "/([A-Za-z0-9_]+)[\\s]+extends[\\s]+([A-Za-z0-9_]+)/"; preg_match_all($pattern, $new_content, $matches, PREG_SET_ORDER); if ($matches) { foreach ($matches as $set) { if (isset($set[2])) { if (substr($set[1], -11) == "_Controller" && $set[2] != $this->getParent() . "_Controller") { //say("Replacing ".$set[1] . " extends " . $set[2] . " with " . $set[1] . " extends {$this->getParent()}_Controller"); $new_content = preg_replace('/' . $set[1] . '[\\s]+extends[\\s]+([A-Za-z0-9_]+)/', $set[1] . " extends {$this->getParent()}_Controller", $new_content); } elseif (substr($set[1], -11) != "_Controller" && $set[2] != $this->getParent()) { // say("Replacing ".$set[1] . " extends " . $set[2] . " with " . $set[1] . " extends {$this->getParent()}"); $new_content = preg_replace('/' . $set[1] . '[\\s]+extends[\\s]+([A-Za-z0-9_]+)/', $set[1] . " extends {$this->getParent()}", $new_content); } } } } $fh = fopen($path, "w"); fwrite($fh, $new_content); fclose($fh); return SilverSmithUtil::get_text_diff($original_content, $new_content); }
/** * Gets the entity for this interface for the _t() function * * @return string */ public function getEntity() { return SilverSmithUtil::generate_i18n_entity($this->getHeading()); }
/** * Initializes a new module in the current project directory * * @see "silversmith help" * @param The parameters, e.g. from the command line */ public static function init_module($params = array()) { if (!isset($params[2]) || empty($params[2])) { fail("Please specify a module name."); } $d = SilverSmithUtil::proper_form($params[2]); if (is_dir($d)) { fail("Module {$d} already exists."); } mkdir($d); say($d); mkdir("{$d}/code"); say("{$d}/code"); mkdir("{$d}/css"); say("{$d}/css"); mkdir("{$d}/javascript"); say("{$d}/javascript"); mkdir("{$d}/templates"); say("{$d}/templates"); mkdir("{$d}/templates/Layout"); say("{$d}/templates/Layout"); mkdir("{$d}/templates/Includes"); say("{$d}/templates/Includes"); $fh = fopen("{$d}/_config.php", "w"); fwrite($fh, "<?php\n"); fclose($fh); say("{$d}/_config.php"); mkdir("{$d}/_config"); say("{$d}/_config"); $fh = fopen("{$d}/_config/_config.yml", "w"); fwrite($fh, "---\nName: {$d}\nAfter: 'framework/*','cms/*'\n"); fclose($fh); say("{$d}/_config/_config.yml"); }
require_once "{$script_dir}/code/SilverSmithNode.php"; require_once "{$script_dir}/code/BedrockDataRecord.php"; foreach (glob("{$script_dir}/code/*.php") as $class) { require_once $class; } require_once "{$script_dir}/code/lib/thirdparty/TextDiff.php"; // Bootstrap the SilverSmith class SilverSmith::set_cli(true); SilverSmith::set_script_dir($script_dir); SilverSmith::set_git_path(trim(shell_exec("which git"))); SilverSmithDefaults::load(SilverSmith::get_script_dir() . "/code/lib/_defaults.yml"); SilverSmithSpec::load(SilverSmith::get_script_dir() . "/code/lib/_spec.yml"); // Validation for the CLI commands $commands = new BedrockYAML(SilverSmith::get_script_dir() . "/code/lib/_cli.yml"); $allowed_actions = $commands->getAllowedActions(); $PARAMS = SilverSmithUtil::parse_parameters(); if (!isset($PARAMS[1])) { fail("Usage: silversmith <command> [-args]. Type 'silversmith help' for more information."); } $action = $PARAMS[1]; if (!$allowed_actions->get($action)) { say(error("'{$action}' is not an allowed command.")); say("Available commands:\n " . implode("\n", array_keys($allowed_actions->toArray()))); die; } $allowed_options = $allowed_actions->get($action)->getOptions(); foreach ($PARAMS as $k => $v) { if (!is_numeric($k) && !$allowed_options->get($k)) { say(error("Option '{$k}' is not allowed.")); say("Available options for {$action}:\n" . implode("\n", array_keys($allowed_options->toArray()))); die;
/** * Singularize a string * * Note: English biased * * @param string The word to singularize * @return string */ public static function singularize($str) { $name = SilverSmithUtil::proper_form($str); if (substr($name, -1, 3) == "ies") { return substr_replace($name, "y", -1, 3); } else { if (substr($name, -1, 1) == "s") { return substr_replace($name, "", -1, 3); } } return $str; }
/** * 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(); } } }