public static function process($directory = "", $defaults, $config = array()) { PipelineHooks::beforeProcessingFilesIn($directory, $defaults, $config); // Hook for before this directory is processed $configPath = $directory . "config.json"; if (file_exists($configPath)) { CL::printDebug("Config File at: " . $configPath, 1); } $config = Secretary::getJSON($configPath, $config); if (array_key_exists("Content", $config)) { CL::println("WARNING: You've declared field \"Content\" in JSON file: " . $directory . "config.json", 0, Colour::Red); CL::println("The \"Content\" keyword is reserved for storing the file contents in.", 0, Colour::Red); CL::println("The value for \"Content\" stored in the JSON file will be ignored.", 0, Colour::Red); } $files = array(); $markdowns = glob($directory . "*.md"); foreach ($markdowns as $markdown) { CL::printDebug("Processing: " . $markdown); $file = new File($markdown); // Set up our @data $data = array_merge($config, $file->data); // Renaming to make more semantic sense as we're now working with the "data" $data["Content"] = $file->contents; // Pass in our file contents $raw = $data; // Store raw data before processing // Process our data through data extensions if (array_key_exists("DataExtensions", $defaults)) { $data = DataProcessor::process($data, $defaults["DataExtensions"]); // Process our data to be filled in CL::printDebug("Processed data", 1, Colour::Green); } else { CL::printDebug("No data extensions declared", 1); } $data["Content"] = Regex::process($data["Content"]); // Now regex it all CL::printDebug("Processed Regex", 1, Colour::Green); $templateFile = Templater::process($data["Template"]); // Generate our template CL::printDebug("Processed template: " . $data["Template"], 1, Colour::Green); $processedFile = LTM::process($templateFile, $data, $raw); // Fill in any conditions and optionals CL::printDebug("Processed LTM", 1, Colour::Green); $file->contents = $processedFile; // Store the complete processed file back into the file object array_push($files, $file); // Add it to the array ready to be exported! } PipelineHooks::afterProcessing($files, $directory, $defaults, $config); // Hook for after this directory is processed - and lets pass some files! $directories = glob($directory . "*", GLOB_ONLYDIR); foreach ($directories as $directory) { $newFiles = self::process($directory . "/", $defaults, $config); foreach ($newFiles as $newFile) { array_push($files, $newFile); } } return $files; }
/** * Process source code for any data requests and optional requests and fill them * @param string $text Text to process * @param array $data Data used to fill * @return string Processed text */ public static function process($text, $data, $raw) { $lines = []; foreach (preg_split("/((\r?\n)|(\r\n?))/", $text) as $line) { array_push($lines, $line); } $renderableLines = self::parse($lines, $data, $raw); // We know using $data which conditionals are able to be used $text = ""; foreach ($renderableLines as $line) { // Go through line by line $text .= $line . "\n"; } /* * Optional values such as tags we'll omit - RAW */ $text = preg_replace_callback("/@data\\?\\((\\w+)\\)::RAW/", function ($matches) use($raw) { if (array_key_exists($matches[1], $raw)) { return $raw[$matches[1]]; } return ""; }, $text); /* * Optional values such as tags we'll omit */ $text = preg_replace_callback("/@data\\?\\((\\w+)\\)/", function ($matches) use($data) { if (array_key_exists($matches[1], $data)) { return $data[$matches[1]]; } return ""; }, $text); /* * Optional values such as tags we'll omit - RAW */ $text = preg_replace_callback("/@data\\((\\w+)\\)::RAW/", function ($matches) use($raw) { if (array_key_exists($matches[1], $raw)) { return $raw[$matches[1]]; } CL::println("ERROR: Template requested " . $matches[1] . "::RAW but was not found. Please review file.", 0, Colour::Red); return $matches[0]; }, $text); /* * Non-Optionals we'll need to alert to the CLI to let the user know there's something up */ $text = preg_replace_callback("/@data\\((\\w+)\\)/", function ($matches) use($data) { if (array_key_exists($matches[1], $data)) { return $data[$matches[1]]; } CL::println("ERROR: Template requested " . $matches[1] . " but was not found. Please review file.", 0, Colour::Red); return $matches[0]; }, $text); return $text; }
/** * Request a template from the templates directory * @param String $templateName The name of the template to request * @return String The processed template file */ static function process($templateName) { $ref = new ReflectionClass('Templater'); $filePath = dirname(dirname($ref->getFileName())) . "/templates/" . $templateName . ".html"; if (!file_exists($filePath)) { // Does the requested file actuallye exist? CL::println("Template at " . $filePath . " does not exist.", 0, Colour::Red); CL::println("Warehouse cannot continue. Please fix. Exiting program.", 0, Colour::Red); exit; } $rawFile = file_get_contents($filePath); // If it does get it's contents $regex = "/@template\\(([\\w|.]+)\\)/"; // Check to see if there are any @template calls $processedFile = preg_replace_callback($regex, function ($matches) { return Templater::process($matches[1]); // recursion recursion recursion recursion.. }, $rawFile); return $processedFile; }
// We then remove all configs as well - we definitely don't want them going across CL::printDebug("Copying " . count($remaining) . " misc files to the upload folder."); CL::printDebug("Copying: "); foreach ($remaining as $filepath) { CL::printDebug($filepath, 1); $fileContents = file_get_contents($filepath); // This is a really dirty way to do it, and creates a nasty memory foot print - alternatives? chdir("../upload"); // Change to our upload if (!file_exists(dirname($filepath))) { // If the directory path for the file doesn't already exist, recursively make it mkdir(dirname($filepath), 0777, TRUE); } file_put_contents(dirname($filepath) . "/" . basename($filepath), $fileContents); // We then put the file contents there as a clone chdir("../source"); // Move back to the source for the next possible file } chdir("../upload"); $remaining = Secretary::getExportFiles(); CL::printDebug("Creating " . count($remaining) . " hook generated files in the upload folder."); foreach ($remaining as $filePath => $fileContents) { if (!file_exists(dirname($filePath))) { // If the directory path for the file doesn't already exist, recursively make it mkdir(dirname($filePath), 0777, TRUE); } file_put_contents($filePath, $fileContents); // We then put the file contents there as a clone } CL::println("Done!", 0, Colour::Green); // That wasn't so bad, right?
/** * Quickway to print text if we're in debug * @param string $text Text to print to console */ public static function printDebug($text, $indent = 0, $colour = Colour::White) { if (DEBUG) { CL::println($text, $indent, $colour); } }