Previous Up Next
Filter architecture Filter architecture PHP_Beautifier_Batch documentation

Create Filters

Facts and strategies to create new Filters

Claudio Bustos cdx@users.sourceforge.com Jens Bierkandt scortch@users.sourceforge.com

Table of Contents

General

Define the name of your filter. PHP_Beautifier try to include the file and the class automatically, so don't use chars that aren't allowed for filenames or class names. Use the direction included in Filter.pkg

The class have to extends PHP_Beautifier_Filter or a subclass. Please, don't use PHP_Beautifier_Filter_Default as your superclass, because this class handle all the tokens and all the others Filters in the stacks will be omitted. BTW, don't overload handleToken and __call, unless you know what you're doing!

All the Filters have as property a reference to the object PHP_Beautifier in PHP_Beautifier_Filter::$oBeaut

Tokenizer sequence

The process starts in PHP_Beautifier::process(). The script (in PHP_Beautifier::$text) is tokenized. Every token is sended to PHP_Beautifier::controlToken(), then to the method PHP_Beautifier_Filter::handleToken() for each Filter until one returns true and, last, to PHP_Beautifier::controlTokenPost(). Last, the method PHP_Beautifier_Filter::postProcess() is called for every Filter

Now, the question is, what are the rules for PHP_Beautifier_Filter::handleToken()?

So, if you want to define your own handlers, create your methods and overload the array PHP_Beautifier_Filter::$aFilterTokenFunctions with the references.

For example, if you want to define a handler to create a commentary before a switch statement, you first have to create the public or protected method t_switch (try use the same name of the constant)

  1. public function method t_switch($sText
  2. {
  3.     $this->oBeaut->add ('/* this is a switch control structure */'.$sText);
  4. }

And create the reference in the array PHP_Beautifier_Filter::$aFilterTokenFunctions

  1. protected $aFilterTokenFunctions=array('t_switch'=>'t_switch');

Now, imagine that you need only count how many switch the script contains. You don't need to alter the output, so the best is bypass the control of the token to the next filter. Your method look like this

  1. public function method t_switch($sText
  2. {
  3.     $this->iNumberOfSwitchs++;
  4.     return PHP_Beautifier_Filter::BYPASS;
  5. }

Methods of PHP_Beautifier to use by the Filters

You should use the reference to PHP_Beautifier stored in PHP_Beautifier_Filter::$oBeaut. So, your code look like

  1. $this->oBeaut->add('something');
  2. $this->oBeaut->addNewLine();
  3. $this->oBeaut->addIndent();

By default, your method must be responsible of the new line and the indent of the next line, if is pertinent.

Previous Up Next
Filter architecture Filter architecture PHP_Beautifier_Batch documentation

Documentation generated on Mon, 31 May 2010 16:16:02 -0400 by phpDocumentor 1.4.3