Skip to content

Instantly share code, notes, and snippets.

Created May 22, 2010 12:37
Show Gist options
  • Select an option

  • Save anonymous/410044 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/410044 to your computer and use it in GitHub Desktop.
<?php
/**
* Zfast Library
*
* @category Zfast
* @package Zfast_Compiler
* @subpackage Zfast_Compiler_Detector
* @copyright Copyright (c) 2009 Tobias Petry
* @version $Id$
*/
/**
* @see Zfast_Compiler_Detector_Abstract
*/
require_once 'Zfast/Compiler/Detector/Abstract.php';
/**
* @category Zfast
* @package Zfast_Compiler
* @subpackage Zfast_Compiler_Detector
* @copyright Copyright (c) 2009 Tobias Petry
* @version $Id$
*/
class Zfast_Compiler_Detector_Oop extends Zfast_Compiler_Detector_Abstract {
/**
* detects class dependencies
*
* @param Zfast_Compiler_Class_Source $source
* @return array class- or interfacenames
*/
public function detect(Zfast_Compiler_Class_Source $source) {
// start tokens (class definition)
$starttokens = array(
T_CLASS,
T_INTERFACE
);
// end token (class body)
$endtoken = new Zfast_Compiler_Tokenizer_Token(Zfast_Compiler_Tokenizer_Token::T_OTHER, '{');
// find matches, but include start and end token for next operations
$matches = $this->_findBetweenTokens($source, $starttokens, $endtoken, 1, true);
// find dependencies
$classes = $this->_findDependencies($matches, T_EXTENDS, T_IMPLEMENTS);
$interfaces = $this->_findDependencies($matches, T_IMPLEMENTS, T_EXTENDS);
// merge dependencies
$dependencies = array();
foreach(array_merge($classes, $interfaces) as $token) {
$dependencies[] = $token->getValue();
}
return $dependencies;
}
/**
* finds classnames
*
* @param array $tokens
* @param int $start
* @param int $end
* @return array
*/
protected function _findDependencies($tokens, $start, $end) {
// end tokens
$end = array($end);
$end[] = new Zfast_Compiler_Tokenizer_Token(Zfast_Compiler_Tokenizer_Token::T_OTHER, '{');
// find declaration
$declaration = $this->_findBetweenTokens($tokens, $start, $end, 1);
// stripping codes for getting class names
$stripping = array(
T_WHITESPACE,
new Zfast_Compiler_Tokenizer_Token(Zfast_Compiler_Tokenizer_Token::T_OTHER, ',')
);
// get classnames
$names = $this->_stripTokens($declaration, $stripping);
return $names;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment