5.7. OPT API

OPT API is a simplified version of the main OPT parser. It contains only the most important methods and fields necessary to run the compiler. OPT API is the best solution, if you want to use OPT as a parser in your e-mailer class or similar. The optApi class contains the following methods and fields from the main parser:

  1. assign()
  2. assignGroup()
  3. assignRef()
  4. error()
  5. fetch()
  6. registerInstruction()
  7. registerInstructionFile()
  8. setDefaultI18n()
  9. setObjectI18n()
  10. parse()
  11. All the configuration directives.
  12. All the fields with the registered items.
  13. All the template data fields.

In order to use OPT API, load the opt.api.php file into your script. You can keep both the main parser and the API loaded - just remember to include the opt.api.php after you load the opt.class.php file.

By default, OPT API is null - there are no instructions or functions registered. It is you, who decides, what the parser can. The API provides a method only for instruction adding, because it is a bit complicated task. To add other features, you fill the following class fields manually:

  1. optApi::$functions - an assotiative array of registered functions. It contains the pairs: "OPT function name" => "PHP function name (without prefix!)"
  2. optApi::$phpFunctions - an assotiative array of PHP functions available in the templates. It contains the pairs: "OPT function name" => "PHP function name"
  3. optApi::$namespaces - an array of registered namespaces. "opt" is available by default.
  4. optApi::$components - an assotiative array of registered components. The component class name is an index, the value must be set to 1.
  5. optApi::$delimiters - a list of Perl regular expressions that define the available delimiters.
  6. optApi::$filters - the field contains three arrays: "pre", "preMaster", "post" and "output" - the buffers for the available filter types. The arrays are pairs of "filter identifier" => "PHP function name (without prefix!)"

Note that OPT API does not have custom resource support.

Although OPT API provides some simple template parsing methods, you can freely write new ones, if you need it. To load and (optionally) compile a template, the parsing method must contain the following code:

Example 5.12. Writing custom parsing methods

public function customParse($tpl)
{
	$compiled = $this -> needCompile($tpl); // 1
				
	$oldErrorReporting = error_reporting(E_ALL ^ E_NOTICE); // 2
	include($this -> compile.$compiled);
	error_reporting($oldErrorReporting);
} // end customParse();

  1. The needCompile() method generates a compiled template name, which is later returned, and tries to compile the template, if it has not been done yet.
  2. To parse a template, we simply include the compiled version. Before that, we must change the error reporting level in order not to report the null blocks as notices. After that, we restore the original level.

To capture the generated output, OPT uses ob_start() and ob_get_clean() methods.

To explain, how to control the compiler manually, we are going to describe the needCompile() method:

Example 5.13. Controlling the compiler

protected function needCompile($filename, $noException = false)
{
	$compiled = optCompileFilename($filename); // 1
	
	$compiledTime = @filemtime($this -> compile.$compiled); // 2
	$result = false;
	$rootTime = @filemtime($this -> root.$filename);
	if($rootTime === false)
	{
		// 3
		if($noException)
		{
			return NULL;
		}
		$this -> error(E_USER_ERROR, '"'.$filename.'" not found in '.$this->root.' directory.', OPT_E_FILE_NOT_FOUND);
	}
	if($compiledTime === false || $compiledTime < $rootTime || $this -> alwaysRebuild)
	{
		// 4
		$result = file_get_contents($this -> root.$filename);
	}
		
	if($result === false)
	{
		// 5
		return $compiled;
	}

	if(!is_object($this -> compiler))
	{
		// 6
		require_once(OPT_DIR.'opt.compiler.php');
		$this -> compiler = new optCompiler($this);
	}
	$this -> compiler -> parse($this -> compile.$compiled, $result); // 7
	return $compiled; // 8
} // end needCompile();

  1. First, we generate the compiled template filename.
  2. We check the modification time. Note that we also check whether the files exist, using filemtime() function, to minimize the number of disk operations.
  3. There is no such template - we have an error!
  4. Here we have a situation, where the template is not compiled - we load its source into memory.
  5. If there is no need to recompile the template, the $result will be empty - the script can return the compiled filename here.
  6. The template must be compiled. If the compiler is not loaded, we do it. The API passes itself as a parameter to the compiler constructor. The compiler may be also created from the main OPT parser or from another compiler - it is needed to import the list of registered features etc.
  7. We request the template compilation. As a first parameter, we pass the path to the compiled file, as a second - the template source. The optional third parameter, which is not used here, tells whether we parse a normal template (false, default) or a master template (true).
  8. Once we have done the compilation, we return the compiled filename.

As you see in the listing, OPT API does not support the performance directive.