/** * Processes raw text with regex patterns * @param string $text The raw string to apply the regex on * @return string The processes string */ public static function process($text) { /* * Since we have changed directories through code we need to get the file location for *this* class * We then load all regex files */ $thisClassReference = new ReflectionClass('Regex'); $filePaths = glob(dirname(dirname($thisClassReference->getFileName())) . "/Extensions/Regex/regex.*.php"); CL::printDebug("Applying Regexes", 1); foreach ($filePaths as $filePath) { // For every regex in the regex extensions include_once $filePath; // Include the file if (preg_match("/regex\\.(\\w+)\\.php/", basename($filePath), $matches)) { // We extract out the class name from the file, and load that as the class //CL::printDebug("Regex: " . $matches[1], 2); // Extensive debug option here $rules = $matches[1]::getRegexs(); // We then get the regexes array (pattern => replace) foreach ($rules as $regex => $replacement) { if (is_callable($matches[1] . "::" . $replacement)) { $text = preg_replace_callback($regex, $matches[1] . "::" . $replacement, $text); } else { $text = preg_replace($regex, $replacement, $text); } } } } return $text; }
/** * Process data that is going to be inserted into the export * @param array $data A mixed array of data to be processed * @return array An array of processed data ready to be plugged into the file */ public static function process($data, $extensions) { /* * Since we have changed directories through code we need to get the file location for *this* class * We then load the data processor extensions, find out what fields they want to edit and then apply them */ $ref = new ReflectionClass('DataProcessor'); $dataExtensionsPath = dirname(dirname($ref->getFileName())) . "/Extensions/Data/"; foreach ($extensions as $extension => $appliesTo) { // For every extension that the config file wants us to apply to $classFilePath = $dataExtensionsPath . "data." . $extension . ".php"; // Get the class filepath from the extension folder if (file_exists($classFilePath)) { include_once $classFilePath; // Include the class foreach ($appliesTo as $applies) { // For every @data it should be applied to $applies = trim($applies); // Trim any leading or trailing whitespace if (array_key_exists($applies, $data)) { // If the @data actually exists CL::printDebug("Applying " . $extension . " to: " . $applies, 1); // Notify we're applying the extension $data[$applies] = $extension::process($data[$applies], $applies, $data); } } } } return $data; }
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; }
public static function afterProcessing($files, $directory, $defaults, $config) { foreach ($defaults["Hooks"] as $appliesTo => $hookExtensions) { // Cycle through the paths that apply to if ($appliesTo == $directory) { // If the path matches the current directory foreach ($hookExtensions as $hookExtension => $arguments) { // For every hook for this directory CL::printDebug($hookExtension . " after-hook called upon: " . $directory, 0, Colour::Blue); include_once self::getHookPath($hookExtension); $hookExtension::afterProcessing($files, $directory, $config, $arguments); } } } }
/** * Set up Warehouse and load any defaults * Return configuration file by loading Classes/config.defaults.json and then config.json */ public static function initDefaults() { $_SESSION["exportFiles"] = array(); $defaults = array(); if (file_exists("Classes/config.defaults.json")) { CL::printDebug("Loading default Warehouse configuration", 0, Colour::Green); $defaults = self::getJSON("Classes/config.defaults.json"); } else { CL::printDebug("File: Classes/config.defaults.json was not found.", 0, Colour::Red); CL::printDebug("Warehouse may not function correctly without this file.", 0, Colour::Red); CL::printDebug("Please re-download from Github.", 0, Colour::Red); } if (file_exists("config.json")) { CL::printDebug("Loading config file.", 0, Colour::Green); $defaults = self::getJSON("config.json", $defaults); } else { CL::printDebug("No custom config file found. #BareBones"); } return $defaults; }
// 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?