123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530 |
- <?php
- require_once 'phing/Task.php';
- class PhpDependTask extends Task
- {
-
- protected $_file = null;
-
- protected $_filesets = array();
-
- protected $_allowedFileExtensions = array('php', 'php5');
-
- protected $_excludeDirectories = array('.git', '.svn', 'CVS');
-
- protected $_excludePackages = array();
-
- protected $_withoutAnnotations = false;
-
- protected $_supportBadDocumentation = false;
-
- protected $_debug = false;
-
- protected $_configFile = null;
-
- protected $_loggers = array();
-
- protected $_analyzers = array();
-
- protected $_runner = null;
-
- protected $_optimization = '';
-
- private $_optimizations = array();
-
- protected $_haltonerror = false;
-
- public function init()
- {
-
- @include_once 'PHP/Depend/TextUI/Runner.php';
- if (! class_exists('PHP_Depend_TextUI_Runner')) {
- throw new BuildException(
- 'PhpDependTask depends on PHP_Depend being installed '
- . 'and on include_path',
- $this->getLocation()
- );
- }
- $this->_optimizations[] = PHP_Depend_TextUI_Runner::OPTIMZATION_BEST;
- $this->_optimizations[] = PHP_Depend_TextUI_Runner::OPTIMZATION_NONE;
-
- require_once 'phing/tasks/ext/pdepend/PhpDependLoggerElement.php';
- require_once 'phing/tasks/ext/pdepend/PhpDependAnalyzerElement.php';
- require_once 'PHP/Depend/TextUI/ResultPrinter.php';
- require_once 'PHP/Depend/Util/Configuration.php';
- require_once 'PHP/Depend/Util/ConfigurationInstance.php';
- }
-
- public function setFile(PhingFile $file)
- {
- $this->_file = $file;
- }
-
- public function createFileSet()
- {
- $num = array_push($this->_filesets, new FileSet());
- return $this->_filesets[$num-1];
- }
-
- public function setAllowedFileExtensions($fileExtensions)
- {
- $this->_allowedFileExtensions = array();
- $token = ' ,;';
- $ext = strtok($fileExtensions, $token);
- while ($ext !== false) {
- $this->_allowedFileExtensions[] = $ext;
- $ext = strtok($token);
- }
- }
-
- public function setExcludeDirectories($excludeDirectories)
- {
- $this->_excludeDirectories = array();
- $token = ' ,;';
- $pattern = strtok($excludeDirectories, $token);
- while ($pattern !== false) {
- $this->_excludeDirectories[] = $pattern;
- $pattern = strtok($token);
- }
- }
-
- public function setExcludePackages($excludePackages)
- {
- $this->_excludePackages = array();
- $token = ' ,;';
- $pattern = strtok($excludePackages, $token);
- while ($pattern !== false) {
- $this->_excludePackages[] = $pattern;
- $pattern = strtok($token);
- }
- }
-
- public function setWithoutAnnotations($withoutAnnotations)
- {
- $this->_withoutAnnotations = StringHelper::booleanValue(
- $withoutAnnotations
- );
- }
-
- public function setSupportBadDocumentation($supportBadDocumentation)
- {
- $this->_supportBadDocumentation = StringHelper::booleanValue(
- $supportBadDocumentation
- );
- }
-
- public function setDebug($debug)
- {
- $this->_debug = StringHelper::booleanValue($debug);
- }
-
- public function setHaltonerror($haltonerror)
- {
- $this->_haltonerror = StringHelper::booleanValue($haltonerror);
- }
-
- public function setConfigFile(PhingFile $configFile)
- {
- $this->_configFile = $configFile;
- }
-
- public function createLogger()
- {
- $num = array_push($this->_loggers, new PhpDependLoggerElement());
- return $this->_loggers[$num-1];
- }
-
- public function createAnalyzer()
- {
- $num = array_push($this->_analyzers, new PhpDependAnalyzerElement());
- return $this->_analyzers[$num-1];
- }
-
- public function main()
- {
- if (!isset($this->_file) and count($this->_filesets) == 0) {
- throw new BuildException(
- "Missing either a nested fileset or attribute 'file' set"
- );
- }
- if (count($this->_loggers) == 0) {
- throw new BuildException("Missing nested 'logger' element");
- }
- $this->validateLoggers();
- $this->validateAnalyzers();
- $filesToParse = array();
- if ($this->_file instanceof PhingFile) {
- $filesToParse[] = $this->_file->__toString();
- } else {
-
- foreach ($this->_filesets as $fs) {
- $files = $fs->getDirectoryScanner($this->project)
- ->getIncludedFiles();
- foreach ($files as $filename) {
- $f = new PhingFile($fs->getDir($this->project), $filename);
- $filesToParse[] = $f->getAbsolutePath();
- }
- }
- }
- $this->_runner = new PHP_Depend_TextUI_Runner();
- $this->_runner->addProcessListener(new PHP_Depend_TextUI_ResultPrinter());
- $this->_runner->setSourceArguments($filesToParse);
- foreach ($this->_loggers as $logger) {
-
- $this->_runner->addLogger(
- $logger->getType(),
- $logger->getOutfile()->__toString()
- );
- }
- foreach ($this->_analyzers as $analyzer) {
-
- $this->_runner->addOption(
- $analyzer->getType(),
- $analyzer->getValue()
- );
- }
-
- if ($this->_withoutAnnotations) {
- $this->_runner->setWithoutAnnotations();
- }
-
- if ($this->_supportBadDocumentation) {
- $this->_runner->setSupportBadDocumentation();
- }
-
- if (count($this->_allowedFileExtensions) > 0) {
- $this->_runner->setFileExtensions($this->_allowedFileExtensions);
- }
-
- if (count($this->_excludeDirectories) > 0) {
- $this->_runner->setExcludeDirectories($this->_excludeDirectories);
- }
-
- if (count($this->_excludePackages) > 0) {
- $this->_runner->setExcludePackages($this->_excludePackages);
- }
-
- if ($this->_optimization !== '') {
- if (in_array($this->_optimization, $this->_optimizations)) {
-
- $this->_runner->setOptimization($this->_optimization);
- } else {
- throw new BuildException(
- 'Invalid optimization "' . $this->_optimization . '" given.'
- );
- }
- }
-
- if ($this->_configFile instanceof PhingFile) {
- if (file_exists($this->_configFile->__toString()) === false) {
- throw new BuildException(
- 'The configuration file "'
- . $this->_configFile->__toString() . '" doesn\'t exist.'
- );
- }
-
- $config = new PHP_Depend_Util_Configuration(
- $this->_configFile->__toString(),
- null,
- true
- );
-
- PHP_Depend_Util_ConfigurationInstance::set($config);
- }
- if ($this->_debug) {
- require_once 'PHP/Depend/Util/Log.php';
-
- PHP_Depend_Util_Log::setSeverity(PHP_Depend_Util_Log::DEBUG);
- }
- $this->_runner->run();
- if ($this->_runner->hasParseErrors() === true) {
- $this->log('Following errors occurred:');
- foreach ($this->_runner->getParseErrors() as $error) {
- $this->log($error);
- }
- if ($this->_haltonerror === true) {
- throw new BuildException('Errors occurred during parse process');
- }
- }
- }
-
- protected function validateLoggers()
- {
- foreach ($this->_loggers as $logger) {
- if ($logger->getType() === '') {
- throw new BuildException(
- "Logger missing required 'type' attribute"
- );
- }
- if ($logger->getOutfile() === null) {
- throw new BuildException(
- "Logger requires 'outfile' attribute"
- );
- }
- }
- }
-
- protected function validateAnalyzers()
- {
- foreach ($this->_analyzers as $analyzer) {
- if ($analyzer->getType() === '') {
- throw new BuildException(
- "Analyzer missing required 'type' attribute"
- );
- }
- if (count($analyzer->getValue()) === 0) {
- throw new BuildException(
- "Analyzer missing required 'value' attribute"
- );
- }
- }
- }
- }
|