/** * Creates a new Capsule context with some basic properties set. * (Capsule is a simple PHP encapsulation system -- aka a php "template" class.) * @return Capsule */ protected function createContext() { $context = new Capsule(); // Make sure the output directory exists, if it doesn't // then create it. $outputDir = new PhingFile($this->outputDirectory); if (!$outputDir->exists()) { $this->log("Output directory does not exist, creating: " . $outputDir->getAbsolutePath()); $outputDir->mkdirs(); } // Place our set of data models into the context along // with the names of the databases as a convenience for now. $context->put("targetDatabase", $this->targetDatabase); $context->put("targetPackage", $this->targetPackage); $context->put("now", strftime("%c")); $this->log("Target database type: " . $this->targetDatabase); $this->log("Target package: " . $this->targetPackage); $this->log("Using template path: " . $this->templatePath); $this->log("Output directory: " . $this->outputDirectory); $context->setTemplatePath($this->templatePath); $context->setOutputDirectory($this->outputDirectory); $this->populateContextProperties($context); return $context; }
/** * Execute the input script with Velocity * * @throws BuildException * BuildExceptions are thrown when required attributes are missing. * Exceptions thrown by Velocity are rethrown as BuildExceptions. */ public function main() { // Make sure the template path is set. if (empty($this->templatePath)) { throw new BuildException("The template path needs to be defined!"); } // Make sure the control template is set. if ($this->controlTemplate === null) { throw new BuildException("The control template needs to be defined!"); } // Make sure the output directory is set. if ($this->outputDirectory === null) { throw new BuildException("The output directory needs to be defined!"); } // Make sure there is an output file. if ($this->outputFile === null) { throw new BuildException("The output file needs to be defined!"); } // Setup Smarty runtime. // Smarty uses one object to store properties and to store // the context for the template (unlike Velocity). We setup this object, calling it // $this->context, and then initControlContext simply zeros out // any assigned variables. $this->context = new Capsule(); if ($this->templatePath !== null) { $this->log("Using templatePath: " . $this->templatePath); $this->context->setTemplatePath($this->templatePath); } // Make sure the output directory exists, if it doesn't // then create it. $outputDir = new PhingFile($this->outputDirectory); if (!$outputDir->exists()) { $this->log("Output directory does not exist, creating: " . $outputDir->getAbsolutePath()); $outputDir->mkdirs(); } $this->context->setOutputDirectory($outputDir->getAbsolutePath()); $path = $this->outputDirectory . DIRECTORY_SEPARATOR . $this->outputFile; $this->log("Generating to file " . $path); //$writer = new FileWriter($path); // The generator and the output path should // be placed in the init context here and // not in the generator class itself. $c = $this->initControlContext(); // Set any variables that need to always // be loaded $this->populateInitialContext($c); // Feed all the options into the initial // control context so they are available // in the control/worker templates. if ($this->contextProperties !== null) { foreach ($this->contextProperties->keys() as $property) { $value = $this->contextProperties->getProperty($property); // Special exception (from Texen) // for properties ending in file.contents: // in that case we dump the contents of the file // as the "value" for the Property. if (preg_match('/file\\.contents$/', $property)) { // pull in contents of file specified $property = substr($property, 0, strpos($property, "file.contents") - 1); // reset value, and then // read in the contents of the file into that var $value = ""; $f = new PhingFile($this->project->resolveFile($value)->getCanonicalPath()); if ($f->exists()) { $fr = new FileReader($f); $fr->readInto($value); } } // if ends with file.contents if (StringHelper::isBoolean($value)) { $value = StringHelper::booleanValue($value); } $c->put($property, $value); } // foreach property } // if contextProperties !== null try { $this->log("Parsing control template: " . $this->controlTemplate); $c->parse($this->controlTemplate, $path); } catch (Exception $ioe) { throw new BuildException("Cannot write parsed template: " . $ioe->getMessage()); } $this->cleanup(); }