Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactor namespace - replace generic \Tools by \Autoload

  • Loading branch information...
commit abc677b68257a1c7f26b726f0bb90f32fd2710f8 1 parent ff2960c
@theseer authored
View
2  phpab.php
@@ -53,6 +53,6 @@
require __DIR__ . '/src/cli.php';
-$exec = new \TheSeer\Tools\CLI();
+$exec = new \TheSeer\Autoload\CLI();
$exec->run();
exit(0);
View
2  samples/sample.php
@@ -2,7 +2,7 @@
require '../src/classfinder.php';
- $finder = new \TheSeer\Tools\ClassFinder;
+ $finder = new \TheSeer\Autoload\ClassFinder;
$rc = $finder->parseFile('src/classfinder.php');
var_dump($rc);
View
4 samples/sample2.php
@@ -8,10 +8,10 @@
require '../../scanner/src/filesonlyfilter.php';
- $scanner = new \TheSeer\Tools\DirectoryScanner;
+ $scanner = new \TheSeer\Autoload\DirectoryScanner;
$scanner->addInclude('*.php');
- $finder = new \TheSeer\Tools\ClassFinder;
+ $finder = new \TheSeer\Autoload\ClassFinder;
$rc = $finder->parseMulti($scanner('../'));
var_dump($rc);
View
6 samples/sample3.php
@@ -8,13 +8,13 @@
require '../../scanner/src/includeexcludefilter.php';
require '../../scanner/src/filesonlyfilter.php';
- $scanner = new \TheSeer\Tools\DirectoryScanner;
+ $scanner = new \TheSeer\Autoload\DirectoryScanner;
$scanner->addInclude('*.php');
- $finder = new \TheSeer\Tools\ClassFinder;
+ $finder = new \TheSeer\Autoload\ClassFinder;
$found = $finder->parseMulti($scanner('../'));
- $ab = new \TheSeer\Tools\AutoloadBuilder($found);
+ $ab = new \TheSeer\Autoload\AutoloadBuilder($found);
echo $ab->render();
View
6 samples/sample4.php
@@ -8,14 +8,14 @@
require '../../scanner/src/includeexcludefilter.php';
require '../../scanner/src/filesonlyfilter.php';
- $scanner = new \TheSeer\Tools\DirectoryScanner;
+ $scanner = new \TheSeer\Autoload\DirectoryScanner;
$scanner->addInclude('*.php');
- $finder = new \TheSeer\Tools\ClassFinder;
+ $finder = new \TheSeer\Autoload\ClassFinder;
$found = $finder->parseMulti($scanner('../'));
- $ab = new \TheSeer\Tools\AutoloadBuilder($found);
+ $ab = new \TheSeer\Autoload\AutoloadBuilder($found);
$ab->setIndent("\t");
$ab->setLineBreak("\r\n");
echo $ab->render();
View
6 samples/sample5.php
@@ -8,14 +8,14 @@
require '../../scanner/src/includeexcludefilter.php';
require '../../scanner/src/filesonlyfilter.php';
- $scanner = new \TheSeer\Tools\DirectoryScanner;
+ $scanner = new \TheSeer\Autoload\DirectoryScanner;
$scanner->addInclude('*.php');
- $finder = new \TheSeer\Tools\ClassFinder;
+ $finder = new \TheSeer\Autoload\ClassFinder;
$found = $finder->parseMulti($scanner('../'));
- $ab = new \TheSeer\Tools\AutoloadBuilder($found);
+ $ab = new \TheSeer\Autoload\AutoloadBuilder($found);
$ab->setBaseDir(realpath(__DIR__.'/..'));
echo $ab->render();
View
4 samples/sample6.php
@@ -3,13 +3,13 @@
require '../src/classfinder.php';
require '../src/dependencysorter.php';
-$finder = new \TheSeer\Tools\ClassFinder(true);
+$finder = new \TheSeer\Autoload\ClassFinder(true);
$finder->parseFile('../tests/_data/dependency/file2.php');
$finder->parseFile('../tests/_data/dependency/file1.php');
$found = $finder->getClasses();
-$x = new \TheSeer\Tools\classDependencySorter($found, $finder->getDependencies());
+$x = new \TheSeer\Autoload\classDependencySorter($found, $finder->getDependencies());
$r = $x->process();
var_dump($found, $finder->getDependencies(), $r);
View
518 src/autoloadbuilder.php
@@ -35,273 +35,273 @@
* @license BSD License
*/
-namespace TheSeer\Tools {
-
- /**
- * Builds spl based autoload code for inclusion into projects
- *
- * @author Arne Blankerts <arne@blankerts.de>
- * @copyright Arne Blankerts <arne@blankerts.de>, All rights reserved.
- */
- class AutoloadBuilder {
-
- /**
- * Associative array of classes (key) and the files (value) they are in
- *
- * @var array
- */
- protected $classes;
-
- /**
- * An optional base dir to strip for the realpath of the filename
- *
- * @var string
- */
- protected $baseDir;
-
- /**
- * Indenting char(s)
- *
- * @var string
- */
- protected $indent;
-
- /**
- * Char(s) used as linebreak
- *
- * @var string
- */
- protected $linebreak = "\n";
-
- /**
- * PHP Template code to render autoload list
- *
- * @var string
- */
- protected $template;
-
- /**
- * Timestamp of production start
- *
- * @var integer
- */
- protected $timestamp;
-
- /**
- * Format string supplied to date() for use with ___CREATED___
- *
- * @var string
- */
- protected $dateformat = 'r';
-
- /**
- * Variables for templates
- *
- * @var array
- */
- protected $variables = array();
-
- /**
- * Flag to toggle PHP 5.2 compat mode
- *
- * @var boolean
- */
- protected $compat = false;
-
- /**
- * Constructor of AutoloadBuilder class
- *
- * @param array $classlist Array of classes
- * @param string $baseDir Base folder for class files
- * @param string $tpl Template file to use for generated code
- * @param string $indent Chars to set default indenting to
- *
- * @return void
- */
- public function __construct(array $classlist, $baseDir = '', $tpl = null, $indent = ' ') {
- $this->classes = $classlist;
- ksort($this->classes);
- $this->baseDir = $baseDir;
- $this->indent = $indent;
- if ($tpl === null) {
- $tpl = __DIR__ . '/templates/default.php.tpl';
- }
- $this->setTemplateFile($tpl);
- }
-
- /**
- * Toggle PHP 5.2 compat mode
- *
- * @param boolean $mode Mode to set compat to
- */
- public function setCompat($mode) {
- $this->compat = $mode;
- }
-
- /**
- * Setter to load a new template from a file
- *
- * @param string $fname File to load as template
- *
- * @return void
- */
- public function setTemplateFile($fname) {
- if (!file_exists($fname)) {
- throw new AutoloadBuilderException("Template '$fname' not found.", AutoloadBuilderException::TemplateNotFound);
- }
- $this->template = file_get_contents($fname);
- }
-
- /**
- * Setter for Template from string
- *
- * @param string $tpl Template code string
- *
- * @return void
- */
- public function setTemplateCode($tpl) {
- $this->template = $tpl;
- }
-
- /**
- * Setter for the Basedir
- *
- * @param string $dir Path to strip from beginning of filenames
- *
- * @return void
- */
- public function setBaseDir($dir) {
- $this->baseDir = $dir;
- }
-
- /**
- * Overwrite default or previously set indenting option
- *
- * @param string $indent Char(s) to use for indenting
- *
- * @return void
- */
- public function setIndent($indent) {
- $this->indent = $indent;
- }
-
- /**
- * Overwrite default or previously set linebreak chars
- *
- * @param string $lbs Code to set linebreak
- *
- * @return void
- */
- public function setLineBreak($lbs) {
- $this->linebreak = $lbs;
- }
-
- /**
- * Setter to use allow usage of fixed date/time for ___CREATED___
- *
- * @param integer $time unix timestamp
- */
- public function setTimestamp($time) {
- if (!is_int($time) && !is_null($time)) {
- throw new AutoloadBuilderException("'$time' is not a unix timestamp", AutoloadBuilderException::InvalidTimestamp);
- }
- $this->timestamp = $time;
- }
-
- /**
- * Setter to adjust the date/time format output of ___CREATED___
- *
- * @param string $frmt Date/Time format string
- */
- public function setDateTimeFormat($frmt) {
- $this->dateformat = $frmt;
- }
-
- /**
- * Set a variable for use with template code
- *
- * @param string $name Key name (use as ___key___ in template)
- * @param string $value Value to use
- */
- public function setVariable($name, $value) {
- $this->variables['___'.$name.'___'] = $value;
- }
-
-
- /**
- * Resolve relative location of file path to basedir if one is set and fix potential
- * broken windows pathnames when run on windows.
- *
- * @param string $fname
- *
- * @return string
- */
- protected function resolvePath($fname) {
- if (empty($this->baseDir)) {
- return str_replace('\\', '/', $fname);
- }
- $basedir = explode(DIRECTORY_SEPARATOR, $this->baseDir);
- $filedir = explode(DIRECTORY_SEPARATOR, dirname(realpath($fname)));
- $pos = 0;
- $max = count($basedir);
- while ($filedir[$pos] == $basedir[$pos]) {
- $pos++;
- if ($pos == $max) break;
- }
- if ($pos == 0) {
- return str_replace('\\', '/', $fname);
- }
- $rel = join('/', array_slice($filedir, $pos));
- if ($pos<count($basedir)) {
- $rel = str_repeat('../', count($basedir)-$pos) . $rel;
- }
- return '/' . (!empty($rel) ? $rel . '/' : '') . basename($fname);
- }
-
- /**
- * Render autoload code into a string
- *
- * @return string
- */
- public function render() {
- $entries = array();
- foreach($this->classes as $class => $file) {
- $fname = $this->resolvePath($file);
- $entries[] = "'$class' => '$fname'";
- }
-
- $baseDir = '';
- if ($this->baseDir) {
- $baseDir = $this->compat ? 'dirname(__FILE__) . ' : '__DIR__ . ';
- }
-
- $replace = array_merge($this->variables, array(
+namespace TheSeer\Autoload {
+
+ /**
+ * Builds spl based autoload code for inclusion into projects
+ *
+ * @author Arne Blankerts <arne@blankerts.de>
+ * @copyright Arne Blankerts <arne@blankerts.de>, All rights reserved.
+ */
+ class AutoloadBuilder {
+
+ /**
+ * Associative array of classes (key) and the files (value) they are in
+ *
+ * @var array
+ */
+ protected $classes;
+
+ /**
+ * An optional base dir to strip for the realpath of the filename
+ *
+ * @var string
+ */
+ protected $baseDir;
+
+ /**
+ * Indenting char(s)
+ *
+ * @var string
+ */
+ protected $indent;
+
+ /**
+ * Char(s) used as linebreak
+ *
+ * @var string
+ */
+ protected $linebreak = "\n";
+
+ /**
+ * PHP Template code to render autoload list
+ *
+ * @var string
+ */
+ protected $template;
+
+ /**
+ * Timestamp of production start
+ *
+ * @var integer
+ */
+ protected $timestamp;
+
+ /**
+ * Format string supplied to date() for use with ___CREATED___
+ *
+ * @var string
+ */
+ protected $dateformat = 'r';
+
+ /**
+ * Variables for templates
+ *
+ * @var array
+ */
+ protected $variables = array();
+
+ /**
+ * Flag to toggle PHP 5.2 compat mode
+ *
+ * @var boolean
+ */
+ protected $compat = false;
+
+ /**
+ * Constructor of AutoloadBuilder class
+ *
+ * @param array $classlist Array of classes
+ * @param string $baseDir Base folder for class files
+ * @param string $tpl Template file to use for generated code
+ * @param string $indent Chars to set default indenting to
+ *
+ * @return void
+ */
+ public function __construct(array $classlist, $baseDir = '', $tpl = null, $indent = ' ') {
+ $this->classes = $classlist;
+ ksort($this->classes);
+ $this->baseDir = $baseDir;
+ $this->indent = $indent;
+ if ($tpl === null) {
+ $tpl = __DIR__ . '/templates/default.php.tpl';
+ }
+ $this->setTemplateFile($tpl);
+ }
+
+ /**
+ * Toggle PHP 5.2 compat mode
+ *
+ * @param boolean $mode Mode to set compat to
+ */
+ public function setCompat($mode) {
+ $this->compat = $mode;
+ }
+
+ /**
+ * Setter to load a new template from a file
+ *
+ * @param string $fname File to load as template
+ *
+ * @return void
+ */
+ public function setTemplateFile($fname) {
+ if (!file_exists($fname)) {
+ throw new AutoloadBuilderException("Template '$fname' not found.", AutoloadBuilderException::TemplateNotFound);
+ }
+ $this->template = file_get_contents($fname);
+ }
+
+ /**
+ * Setter for Template from string
+ *
+ * @param string $tpl Template code string
+ *
+ * @return void
+ */
+ public function setTemplateCode($tpl) {
+ $this->template = $tpl;
+ }
+
+ /**
+ * Setter for the Basedir
+ *
+ * @param string $dir Path to strip from beginning of filenames
+ *
+ * @return void
+ */
+ public function setBaseDir($dir) {
+ $this->baseDir = $dir;
+ }
+
+ /**
+ * Overwrite default or previously set indenting option
+ *
+ * @param string $indent Char(s) to use for indenting
+ *
+ * @return void
+ */
+ public function setIndent($indent) {
+ $this->indent = $indent;
+ }
+
+ /**
+ * Overwrite default or previously set linebreak chars
+ *
+ * @param string $lbs Code to set linebreak
+ *
+ * @return void
+ */
+ public function setLineBreak($lbs) {
+ $this->linebreak = $lbs;
+ }
+
+ /**
+ * Setter to use allow usage of fixed date/time for ___CREATED___
+ *
+ * @param integer $time unix timestamp
+ */
+ public function setTimestamp($time) {
+ if (!is_int($time) && !is_null($time)) {
+ throw new AutoloadBuilderException("'$time' is not a unix timestamp", AutoloadBuilderException::InvalidTimestamp);
+ }
+ $this->timestamp = $time;
+ }
+
+ /**
+ * Setter to adjust the date/time format output of ___CREATED___
+ *
+ * @param string $frmt Date/Time format string
+ */
+ public function setDateTimeFormat($frmt) {
+ $this->dateformat = $frmt;
+ }
+
+ /**
+ * Set a variable for use with template code
+ *
+ * @param string $name Key name (use as ___key___ in template)
+ * @param string $value Value to use
+ */
+ public function setVariable($name, $value) {
+ $this->variables['___'.$name.'___'] = $value;
+ }
+
+
+ /**
+ * Resolve relative location of file path to basedir if one is set and fix potential
+ * broken windows pathnames when run on windows.
+ *
+ * @param string $fname
+ *
+ * @return string
+ */
+ protected function resolvePath($fname) {
+ if (empty($this->baseDir)) {
+ return str_replace('\\', '/', $fname);
+ }
+ $basedir = explode(DIRECTORY_SEPARATOR, $this->baseDir);
+ $filedir = explode(DIRECTORY_SEPARATOR, dirname(realpath($fname)));
+ $pos = 0;
+ $max = count($basedir);
+ while ($filedir[$pos] == $basedir[$pos]) {
+ $pos++;
+ if ($pos == $max) break;
+ }
+ if ($pos == 0) {
+ return str_replace('\\', '/', $fname);
+ }
+ $rel = join('/', array_slice($filedir, $pos));
+ if ($pos<count($basedir)) {
+ $rel = str_repeat('../', count($basedir)-$pos) . $rel;
+ }
+ return '/' . (!empty($rel) ? $rel . '/' : '') . basename($fname);
+ }
+
+ /**
+ * Render autoload code into a string
+ *
+ * @return string
+ */
+ public function render() {
+ $entries = array();
+ foreach($this->classes as $class => $file) {
+ $fname = $this->resolvePath($file);
+ $entries[] = "'$class' => '$fname'";
+ }
+
+ $baseDir = '';
+ if ($this->baseDir) {
+ $baseDir = $this->compat ? 'dirname(__FILE__) . ' : '__DIR__ . ';
+ }
+
+ $replace = array_merge($this->variables, array(
'___CREATED___' => date( $this->dateformat, $this->timestamp ? $this->timestamp : time()),
'___CLASSLIST___' => join( ',' . $this->linebreak . $this->indent, $entries),
'___BASEDIR___' => $baseDir,
'___AUTOLOAD___' => uniqid('autoload')
- ));
- return str_replace(array_keys($replace), array_values($replace), $this->template);
- }
+ ));
+ return str_replace(array_keys($replace), array_values($replace), $this->template);
+ }
- /**
- * Save autoload code to given filename
- *
- * @param string $filename Filename to store code in
- *
- * @return integer|boolean
- */
- public function save($filename) {
- return file_put_contents($filename, $this->render());
- }
- }
+ /**
+ * Save autoload code to given filename
+ *
+ * @param string $filename Filename to store code in
+ *
+ * @return integer|boolean
+ */
+ public function save($filename) {
+ return file_put_contents($filename, $this->render());
+ }
+ }
- class AutoloadBuilderException extends \Exception {
+ class AutoloadBuilderException extends \Exception {
- const TemplateNotFound = 1;
- const InvalidTimestamp = 2;
+ const TemplateNotFound = 1;
+ const InvalidTimestamp = 2;
- }
+ }
}
View
420 src/classfinder.php
@@ -35,220 +35,222 @@
* @license BSD License
*/
-namespace TheSeer\Tools {
-
- /**
- * Namespace aware parser to find and extract defined classes within php source files
- *
- * @author Arne Blankerts <arne@blankerts.de>
- * @copyright Arne Blankerts <arne@blankerts.de>, All rights reserved.
- */
- class ClassFinder {
-
- protected $withDeps;
- protected $isInTolerantMode;
-
- protected $foundClasses = array();
- protected $dependencies = array();
-
- public function __construct($doDeps = false, $tolerantMode = false) {
- $this->withDeps = $doDeps;
- $this->isInTolerantMode = $tolerantMode;
- }
-
- public function getClasses() {
- return $this->foundClasses;
- }
-
- public function getDependencies() {
- if (!$this->withDeps) {
- throw new ClassFinderException('Dependency collection disabled', ClassFinderException::NoDependencies);
- }
- return $this->dependencies;
- }
-
- /**
- * Parse a given file for defintions of classes and interfaces
- *
- * @param string $file Filename of file to process
- *
- * @return integer
- */
- public function parseFile($file) {
- $entries = 0;
- $classFound = false;
- $nsFound = false;
- $nsProc = false;
- $inNamespace = null;
- $bracketCount = 0;
- $bracketNS = false;
- $extendsFound = false;
- $implementsFound = false;
- $lastClass = '';
- $dependsClass = '';
-
- $token = token_get_all(file_get_contents($file));
- foreach($token as $pos => $tok) {
- if (!is_array($tok)) {
- switch ($tok) {
- case '{': {
- $bracketCount++;
- if ($nsProc) {
- $bracketNS = true;
- }
- if ($this->withDeps && ($dependsClass != '')) {
- if (!isset($this->dependencies[$lastClass])) {
- $this->dependencies[$lastClass] = array();
+namespace TheSeer\Autoload {
+
+ use \TheSeer\DirectoryScanner\PHPFilterIterator;
+
+ /**
+ * Namespace aware parser to find and extract defined classes within php source files
+ *
+ * @author Arne Blankerts <arne@blankerts.de>
+ * @copyright Arne Blankerts <arne@blankerts.de>, All rights reserved.
+ */
+ class ClassFinder {
+
+ protected $withDeps;
+ protected $isInTolerantMode;
+
+ protected $foundClasses = array();
+ protected $dependencies = array();
+
+ public function __construct($doDeps = false, $tolerantMode = false) {
+ $this->withDeps = $doDeps;
+ $this->isInTolerantMode = $tolerantMode;
+ }
+
+ public function getClasses() {
+ return $this->foundClasses;
+ }
+
+ public function getDependencies() {
+ if (!$this->withDeps) {
+ throw new ClassFinderException('Dependency collection disabled', ClassFinderException::NoDependencies);
+ }
+ return $this->dependencies;
+ }
+
+ /**
+ * Parse a given file for defintions of classes and interfaces
+ *
+ * @param string $file Filename of file to process
+ *
+ * @return integer
+ */
+ public function parseFile($file) {
+ $entries = 0;
+ $classFound = false;
+ $nsFound = false;
+ $nsProc = false;
+ $inNamespace = null;
+ $bracketCount = 0;
+ $bracketNS = false;
+ $extendsFound = false;
+ $implementsFound = false;
+ $lastClass = '';
+ $dependsClass = '';
+
+ $token = token_get_all(file_get_contents($file));
+ foreach($token as $pos => $tok) {
+ if (!is_array($tok)) {
+ switch ($tok) {
+ case '{': {
+ $bracketCount++;
+ if ($nsProc) {
+ $bracketNS = true;
+ }
+ if ($this->withDeps && ($dependsClass != '')) {
+ if (!isset($this->dependencies[$lastClass])) {
+ $this->dependencies[$lastClass] = array();
+ }
+ $this->dependencies[$lastClass][] = $dependsClass;
+ $dependsClass = '';
+ }
+
+ $nsProc = false;
+ $implementsFound = false;
+ $extendsFound = false;
+ break;
}
- $this->dependencies[$lastClass][] = $dependsClass;
- $dependsClass = '';
- }
-
- $nsProc = false;
- $implementsFound = false;
- $extendsFound = false;
- break;
- }
- case '}': {
- $bracketCount--;
- if ($bracketCount==0 && $inNamespace && $bracketNS) {
- $inNamespace = null;
- }
- break;
- }
- case ";": {
- if ($nsProc) {
- $nsProc = false;
- $bracketNS = false;
- }
- break;
- }
- case ',': {
- if ($this->withDeps && $implementsFound) {
- if (!isset($this->dependencies[$lastClass])) {
- $this->dependencies[$lastClass] = array();
+ case '}': {
+ $bracketCount--;
+ if ($bracketCount==0 && $inNamespace && $bracketNS) {
+ $inNamespace = null;
+ }
+ break;
}
- $this->dependencies[$lastClass][] = $dependsClass;
- $dependsClass = '';
- $classNameStart = true;
- }
- break;
- }
- }
- continue;
- }
+ case ";": {
+ if ($nsProc) {
+ $nsProc = false;
+ $bracketNS = false;
+ }
+ break;
+ }
+ case ',': {
+ if ($this->withDeps && $implementsFound) {
+ if (!isset($this->dependencies[$lastClass])) {
+ $this->dependencies[$lastClass] = array();
+ }
+ $this->dependencies[$lastClass][] = $dependsClass;
+ $dependsClass = '';
+ $classNameStart = true;
+ }
+ break;
+ }
+ }
+ continue;
+ }
- switch ($tok[0]) {
- case T_CURLY_OPEN:
- case T_DOLLAR_OPEN_CURLY_BRACES: {
- $bracketCount++;
- continue;
- }
- case T_IMPLEMENTS: {
- $implementsFound = true;
- $classNameStart = true;
- continue;
- }
- case T_EXTENDS: {
- $extendsFound = true;
- $implementsFound = false;
- $classNameStart = true;
- continue;
- }
- case T_CLASS:
- case T_INTERFACE: {
- $classFound = true;
- continue;
- }
- case T_NAMESPACE: {
- if ($token[$pos + 1][0] == T_NS_SEPARATOR) {
- // Ignore inline use of namespace keyword
- continue;
- }
- $nsFound = true;
- $nsProc = true;
- $inNamespace = null;
- continue;
- }
- case T_NS_SEPARATOR: {
- if ($nsProc) {
- $nsFound = true;
- $inNamespace .= '\\\\';
- }
- if ($extendsFound || $implementsFound) {
- if (!$classNameStart) $dependsClass .= '\\\\';
- $classNameStart = false;
- }
- continue;
- }
-
- case T_STRING: {
- if ($nsFound) {
- $inNamespace .= strtolower($tok[1]);
- $nsFound = false;
- } elseif ($classFound) {
- $lastClass = ($inNamespace ? $inNamespace .'\\\\' : '') . strtolower($tok[1]);
- if (isset($this->foundClasses[$lastClass])) {
- if ($this->canTolerateRedeclaration($lastClass, $file)) {
+ switch ($tok[0]) {
+ case T_CURLY_OPEN:
+ case T_DOLLAR_OPEN_CURLY_BRACES: {
+ $bracketCount++;
+ continue;
+ }
+ case T_IMPLEMENTS: {
+ $implementsFound = true;
+ $classNameStart = true;
+ continue;
+ }
+ case T_EXTENDS: {
+ $extendsFound = true;
+ $implementsFound = false;
+ $classNameStart = true;
+ continue;
+ }
+ case T_CLASS:
+ case T_INTERFACE: {
+ $classFound = true;
+ continue;
+ }
+ case T_NAMESPACE: {
+ if ($token[$pos + 1][0] == T_NS_SEPARATOR) {
+ // Ignore inline use of namespace keyword
continue;
}
- throw new ClassFinderException(sprintf(
+ $nsFound = true;
+ $nsProc = true;
+ $inNamespace = null;
+ continue;
+ }
+ case T_NS_SEPARATOR: {
+ if ($nsProc) {
+ $nsFound = true;
+ $inNamespace .= '\\\\';
+ }
+ if ($extendsFound || $implementsFound) {
+ if (!$classNameStart) $dependsClass .= '\\\\';
+ $classNameStart = false;
+ }
+ continue;
+ }
+
+ case T_STRING: {
+ if ($nsFound) {
+ $inNamespace .= strtolower($tok[1]);
+ $nsFound = false;
+ } elseif ($classFound) {
+ $lastClass = ($inNamespace ? $inNamespace .'\\\\' : '') . strtolower($tok[1]);
+ if (isset($this->foundClasses[$lastClass])) {
+ if ($this->canTolerateRedeclaration($lastClass, $file)) {
+ continue;
+ }
+ throw new ClassFinderException(sprintf(
"Redeclaration of class '%s' detected\n Original: %s\n Secondary: %s\n\n",
- $lastClass,
- $this->foundClasses[$lastClass],
- $file
- ), ClassFinderException::ClassRedeclaration
- );
- }
- $this->foundClasses[$lastClass] = $file;
- $entries++;
- $classFound = false;
- } elseif ($extendsFound || $implementsFound) {
- if ($classNameStart && $inNamespace) {
- $dependsClass = $inNamespace . '\\\\';
- $classNameStart = false;
- }
- $dependsClass .= strtolower($tok[1]);
- }
- continue;
- }
+ $lastClass,
+ $this->foundClasses[$lastClass],
+ $file
+ ), ClassFinderException::ClassRedeclaration
+ );
+ }
+ $this->foundClasses[$lastClass] = $file;
+ $entries++;
+ $classFound = false;
+ } elseif ($extendsFound || $implementsFound) {
+ if ($classNameStart && $inNamespace) {
+ $dependsClass = $inNamespace . '\\\\';
+ $classNameStart = false;
+ }
+ $dependsClass .= strtolower($tok[1]);
+ }
+ continue;
+ }
+ }
+ }
+
+ return $entries;
+ }
+
+ /**
+ * @return boolean
+ */
+ protected function canTolerateRedeclaration($redeclaredClassName, $redeclaredInFilePath)
+ {
+ return $this->foundClasses[$redeclaredClassName] === $redeclaredInFilePath
+ && $this->isInTolerantMode === true;
+ }
+
+ /**
+ * Process multiple files and parse them for classes and interfaces
+ *
+ * @param Iterator $sources Iterator based list of files (SplFileObject) to parse
+ *
+ * @return integer
+ */
+ public function parseMulti(\Iterator $sources) {
+ $count = 0;
+ $worker = new PHPFilterIterator($sources);
+ foreach($worker as $file) {
+ $count += $this->parseFile($file->getPathname());
}
- }
-
- return $entries;
- }
-
- /**
- * @return boolean
- */
- protected function canTolerateRedeclaration($redeclaredClassName, $redeclaredInFilePath)
- {
- return $this->foundClasses[$redeclaredClassName] === $redeclaredInFilePath
- && $this->isInTolerantMode === true;
- }
-
- /**
- * Process multiple files and parse them for classes and interfaces
- *
- * @param Iterator $sources Iterator based list of files (SplFileObject) to parse
- *
- * @return integer
- */
- public function parseMulti(\Iterator $sources) {
- $count = 0;
- $worker = new PHPFilterIterator($sources);
- foreach($worker as $file) {
- $count += $this->parseFile($file->getPathname());
- }
- return $count;
- }
-
- }
-
- class ClassFinderException extends \Exception {
-
- const NoDependencies = 1;
- const ClassRedeclaration = 2;
-
- }
+ return $count;
+ }
+
+ }
+
+ class ClassFinderException extends \Exception {
+
+ const NoDependencies = 1;
+ const ClassRedeclaration = 2;
+
+ }
}
View
602 src/cli.php
@@ -36,376 +36,376 @@
* @license BSD License
*/
-namespace TheSeer\Tools {
-
- /**
- * CLI interface to AutoloadBuilder / StaticBuilder
- *
- * @author Arne Blankerts <arne@blankerts.de>
- * @copyright Arne Blankerts <arne@blankerts.de>, All rights reserved.
- */
- class CLI {
-
- /**
- * Version identifier
- *
- * @var string
- */
- const VERSION = "%version%";
-
- /**
- * Main executor method
- *
- * @return void
- */
- public function run() {
-
- $input = new \ezcConsoleInput();
-
- $versionOption = $input->registerOption( new \ezcConsoleOption( 'v', 'version' ) );
- $versionOption->shorthelp = 'Prints the version and exits';
- $versionOption->isHelpOption = true;
-
- $helpOption = $input->registerOption( new \ezcConsoleOption( 'h', 'help' ) );
- $helpOption->isHelpOption = true;
- $helpOption->shorthelp = 'Prints this usage information';
-
- $outputOption = $input->registerOption( new \ezcConsoleOption(
+namespace TheSeer\Autoload {
+
+ /**
+ * CLI interface to AutoloadBuilder / StaticBuilder
+ *
+ * @author Arne Blankerts <arne@blankerts.de>
+ * @copyright Arne Blankerts <arne@blankerts.de>, All rights reserved.
+ */
+ class CLI {
+
+ /**
+ * Version identifier
+ *
+ * @var string
+ */
+ const VERSION = "%version%";
+
+ /**
+ * Main executor method
+ *
+ * @return void
+ */
+ public function run() {
+
+ $input = new \ezcConsoleInput();
+
+ $versionOption = $input->registerOption( new \ezcConsoleOption( 'v', 'version' ) );
+ $versionOption->shorthelp = 'Prints the version and exits';
+ $versionOption->isHelpOption = true;
+
+ $helpOption = $input->registerOption( new \ezcConsoleOption( 'h', 'help' ) );
+ $helpOption->isHelpOption = true;
+ $helpOption->shorthelp = 'Prints this usage information';
+
+ $outputOption = $input->registerOption( new \ezcConsoleOption(
'o', 'output', \ezcConsoleInput::TYPE_STRING, 'STDOUT', false,
'Output file for generated code (default: STDOUT)'
));
- $pharOption = $input->registerOption( new \ezcConsoleOption(
+ $pharOption = $input->registerOption( new \ezcConsoleOption(
'p', 'phar', \ezcConsoleInput::TYPE_NONE, null, false,
'Build a phar archive of directory contents',
null,
array( new \ezcConsoleOptionRule( $input->getOption( 'o' ) ) )
- ));
+ ));
- $input->registerOption( new \ezcConsoleOption(
+ $input->registerOption( new \ezcConsoleOption(
'i', 'include', \ezcConsoleInput::TYPE_STRING, '*.php', true,
'File pattern to include (default: *.php)'
- ));
- $input->registerOption( new \ezcConsoleOption(
+ ));
+ $input->registerOption( new \ezcConsoleOption(
'e', 'exclude', \ezcConsoleInput::TYPE_STRING, null, true,
'File pattern to exclude'
- ));
- $input->registerOption( new \ezcConsoleOption(
+ ));
+ $input->registerOption( new \ezcConsoleOption(
'b', 'basedir', \ezcConsoleInput::TYPE_STRING, null, false,
'Basedir for filepaths'
- ));
- $input->registerOption( new \ezcConsoleOption(
+ ));
+ $input->registerOption( new \ezcConsoleOption(
't', 'template', \ezcConsoleInput::TYPE_STRING, null, false,
'Path to code template to use'
- ));
- $input->registerOption( new \ezcConsoleOption(
+ ));
+ $input->registerOption( new \ezcConsoleOption(
'', 'format', \ezcConsoleInput::TYPE_STRING, null, false,
'Dateformat string for timestamp'
- ));
- $input->registerOption( new \ezcConsoleOption(
+ ));
+ $input->registerOption( new \ezcConsoleOption(
'', 'linebreak', \ezcConsoleInput::TYPE_STRING, null, false,
'Linebreak style (CR, CR/LF or LF)'
- ));
- $input->registerOption( new \ezcConsoleOption(
+ ));
+ $input->registerOption( new \ezcConsoleOption(
'', 'indent', \ezcConsoleInput::TYPE_STRING, null, false,
'String used for indenting (default: 3 spaces)'
- ));
- $lintOption = $input->registerOption( new \ezcConsoleOption(
+ ));
+ $lintOption = $input->registerOption( new \ezcConsoleOption(
'', 'lint', \ezcConsoleInput::TYPE_NONE, null, false,
'Run lint on generated code'
- ));
- $input->registerOption( new \ezcConsoleOption(
+ ));
+ $input->registerOption( new \ezcConsoleOption(
'', 'lint-php', \ezcConsoleInput::TYPE_STRING, null, false,
'PHP binary path for linting (default: /usr/bin/php or c:\\php\\php.exe)'
- ));
+ ));
- $input->registerOption( new \ezcConsoleOption(
+ $input->registerOption( new \ezcConsoleOption(
'c', 'compat', \ezcConsoleInput::TYPE_NONE, null, false,
'Generate PHP 5.2 compliant code'
- ));
+ ));
- $staticOption = $input->registerOption( new \ezcConsoleOption(
+ $staticOption = $input->registerOption( new \ezcConsoleOption(
's', 'static', \ezcConsoleInput::TYPE_NONE, null, false,
'Build a static require file'
- ));
+ ));
- $staticOption = $input->registerOption( new \ezcConsoleOption(
+ $staticOption = $input->registerOption( new \ezcConsoleOption(
'', 'tolerant', \ezcConsoleInput::TYPE_NONE, null, false,
'Ignore Class Redeclarations in the same file'
- ));
-
- $input->argumentDefinition = new \ezcConsoleArguments();
- $input->argumentDefinition[0] = new \ezcConsoleArgument( "directory" );
- $input->argumentDefinition[0]->shorthelp = "The directory to process.";
-
- try {
- $input->process();
- } catch (\ezcConsoleException $e) {
- $this->showVersion();
- echo $e->getMessage()."\n\n";
- $this->showUsage();
- exit(3);
- }
-
- if ($helpOption->value === true) {
- $this->showVersion();
- $this->showUsage();
- exit(0);
- }
-
- if ($versionOption->value === true ) {
- $this->showVersion();
- exit(0);
- }
-
- try {
- $scanner = $this->getScanner($input);
- if ($pharOption->value !== false) {
- $phar = $this->buildPhar($scanner, $input);
- $scanner->rewind();
- }
- $finder = new ClassFinder(
- $input->getOption('static')->value,
- $input->getOption('tolerant')->value
- );
- $found = $finder->parseMulti($scanner);
- // this unset is needed to "fix" a segfault on shutdown
- unset($scanner);
- if ($found==0) {
- fwrite(STDERR, "No classes were found - process aborted.\n\n");
- exit(1);
+ ));
+
+ $input->argumentDefinition = new \ezcConsoleArguments();
+ $input->argumentDefinition[0] = new \ezcConsoleArgument( "directory" );
+ $input->argumentDefinition[0]->shorthelp = "The directory to process.";
+
+ try {
+ $input->process();
+ } catch (\ezcConsoleException $e) {
+ $this->showVersion();
+ echo $e->getMessage()."\n\n";
+ $this->showUsage();
+ exit(3);
}
- $builder = $this->getBuilder($finder, $input);
+ if ($helpOption->value === true) {
+ $this->showVersion();
+ $this->showUsage();
+ exit(0);
+ }
- if ($lintOption->value === true) {
- exit( $this->lintCode($builder->render(), $input) ? 0 : 4);
+ if ($versionOption->value === true ) {
+ $this->showVersion();
+ exit(0);
}
- if ($outputOption->value == 'STDOUT') {
- echo $builder->render();
- } else {
- if ($pharOption->value !== false) {
- $builder->setVariable('PHAR', basename($outputOption->value));
- $phar->setStub($builder->render());
- $phar->stopBuffering();
- echo "phar archive '{$outputOption->value}' generated.\n\n";
- } else {
- $builder->save($outputOption->value);
- echo "Autoload file '{$outputOption->value}' generated.\n\n";
- }
+ try {
+ $scanner = $this->getScanner($input);
+ if ($pharOption->value !== false) {
+ $phar = $this->buildPhar($scanner, $input);
+ $scanner->rewind();
+ }
+ $finder = new ClassFinder(
+ $input->getOption('static')->value,
+ $input->getOption('tolerant')->value
+ );
+ $found = $finder->parseMulti($scanner);
+ // this unset is needed to "fix" a segfault on shutdown
+ unset($scanner);
+ if ($found==0) {
+ fwrite(STDERR, "No classes were found - process aborted.\n\n");
+ exit(1);
+ }
+
+ $builder = $this->getBuilder($finder, $input);
+
+ if ($lintOption->value === true) {
+ exit( $this->lintCode($builder->render(), $input) ? 0 : 4);
+ }
+
+ if ($outputOption->value == 'STDOUT') {
+ echo $builder->render();
+ } else {
+ if ($pharOption->value !== false) {
+ $builder->setVariable('PHAR', basename($outputOption->value));
+ $phar->setStub($builder->render());
+ $phar->stopBuffering();
+ echo "phar archive '{$outputOption->value}' generated.\n\n";
+ } else {
+ $builder->save($outputOption->value);
+ echo "Autoload file '{$outputOption->value}' generated.\n\n";
+ }
+ }
+ exit(0);
+
+ } catch (\Exception $e) {
+ $this->showVersion();
+ fwrite(STDERR, "Error while processing request:\n");
+ fwrite(STDERR, ' - ' . $e->getMessage()."\n");
+ exit(1);
}
- exit(0);
-
- } catch (\Exception $e) {
- $this->showVersion();
- fwrite(STDERR, "Error while processing request:\n");
- fwrite(STDERR, ' - ' . $e->getMessage()."\n");
- exit(1);
- }
- }
-
- /**
- * Helper to get instance of DirectoryScanner with cli options applied
- *
- * @param ezcConsoleInput $input CLI Options pased to app
- *
- * @return Theseer\Tools\IncludeExcludeFilterIterator
- */
- protected function getScanner(\ezcConsoleInput $input) {
- $scanner = new DirectoryScanner;
-
- $include = $input->getOption('include');
- if (is_array($include->value)) {
- $scanner->setIncludes($include->value);
- } else {
- $scanner->addInclude($include->value);
- }
-
- $exclude = $input->getOption('exclude');
- if ($exclude->value) {
- if (is_array($exclude->value)) {
- $scanner->setExcludes($exclude->value);
+ }
+
+ /**
+ * Helper to get instance of DirectoryScanner with cli options applied
+ *
+ * @param ezcConsoleInput $input CLI Options pased to app
+ *
+ * @return Theseer\Autoload\IncludeExcludeFilterIterator
+ */
+ protected function getScanner(\ezcConsoleInput $input) {
+ $scanner = new DirectoryScanner;
+
+ $include = $input->getOption('include');
+ if (is_array($include->value)) {
+ $scanner->setIncludes($include->value);
} else {
- $scanner->addExclude($exclude->value);
+ $scanner->addInclude($include->value);
}
- }
-
- $args = $input->getArguments();
- return $scanner($args[0]);
- }
-
- /**
- * Helper to get instance of AutoloadBuilder with cli options applied
- *
- * @param ClassFinder $finder Instance of ClassFinder to get classes from
- * @param \ezcConsoleInput $input CLI Options pased to app
- */
- protected function getBuilder(ClassFinder $finder, \ezcConsoleInput $input) {
- $isStatic = $input->getOption('static')->value;
- $isPhar = $input->getOption('phar')->value;
- $isCompat = $input->getOption('compat')->value;
-
- if ($isStatic === true) {
- $ab = new StaticBuilder($finder->getClasses());
- $ab->setDependencies($finder->getDependencies());
- $ab->setPharMode($isPhar);
- } else {
- $ab = new AutoloadBuilder($finder->getClasses());
- }
-
- $ab->setCompat($isCompat);
-
- $basedir = $input->getOption('basedir');
- if ($basedir->value) {
- $bdir = realpath($basedir->value);
- if (!$bdir || !is_dir($bdir)) {
- throw new \RuntimeException("Given basedir '{$basedir->value}' does not exist or is not a directory");
+
+ $exclude = $input->getOption('exclude');
+ if ($exclude->value) {
+ if (is_array($exclude->value)) {
+ $scanner->setExcludes($exclude->value);
+ } else {
+ $scanner->addExclude($exclude->value);
+ }
}
- $ab->setBaseDir($bdir);
- } else {
+
$args = $input->getArguments();
- $ab->setBaseDir(realpath($args[0]));
- }
-
- $template = $input->getOption('template');
- if ($template->value) {
- if (!file_exists($template->value)) {
- $alternative = __DIR__.'/templates/'.$template->value;
- if (file_exists($alternative)) {
- $template->value = $alternative;
- }
+ return $scanner($args[0]);
+ }
+
+ /**
+ * Helper to get instance of AutoloadBuilder with cli options applied
+ *
+ * @param ClassFinder $finder Instance of ClassFinder to get classes from
+ * @param \ezcConsoleInput $input CLI Options pased to app
+ */
+ protected function getBuilder(ClassFinder $finder, \ezcConsoleInput $input) {
+ $isStatic = $input->getOption('static')->value;
+ $isPhar = $input->getOption('phar')->value;
+ $isCompat = $input->getOption('compat')->value;
+
+ if ($isStatic === true) {
+ $ab = new StaticBuilder($finder->getClasses());
+ $ab->setDependencies($finder->getDependencies());
+ $ab->setPharMode($isPhar);
+ } else {
+ $ab = new AutoloadBuilder($finder->getClasses());
}
- $ab->setTemplateFile($template->value);
- } else {
- // determine auto template to use
- $tplFile = 'default.php.tpl';
- if ($isCompat) {
- $tplFile = 'php52.php.tpl';
- }
+ $ab->setCompat($isCompat);
- if ($isPhar) {
- if ($isStatic) {
- $tplFile = 'staticphar.php.tpl';
- } else {
- $tplFile = 'phar.php.tpl';
- }
- } elseif ($isStatic) {
- $tplFile = 'static.php.tpl';
+ $basedir = $input->getOption('basedir');
+ if ($basedir->value) {
+ $bdir = realpath($basedir->value);
+ if (!$bdir || !is_dir($bdir)) {
+ throw new \RuntimeException("Given basedir '{$basedir->value}' does not exist or is not a directory");
+ }
+ $ab->setBaseDir($bdir);
+ } else {
+ $args = $input->getArguments();
+ $ab->setBaseDir(realpath($args[0]));
}
- $ab->setTemplateFile(__DIR__.'/templates/'.$tplFile);
- }
+ $template = $input->getOption('template');
+ if ($template->value) {
+ if (!file_exists($template->value)) {
+ $alternative = __DIR__.'/templates/'.$template->value;
+ if (file_exists($alternative)) {
+ $template->value = $alternative;
+ }
+ }
+ $ab->setTemplateFile($template->value);
+ } else {
- $format = $input->getOption('format');
- if ($format->value) {
- $ab->setDateTimeFormat($format->value);
- }
+ // determine auto template to use
+ $tplFile = 'default.php.tpl';
+ if ($isCompat) {
+ $tplFile = 'php52.php.tpl';
+ }
+
+ if ($isPhar) {
+ if ($isStatic) {
+ $tplFile = 'staticphar.php.tpl';
+ } else {
+ $tplFile = 'phar.php.tpl';
+ }
+ } elseif ($isStatic) {
+ $tplFile = 'static.php.tpl';
+ }
+
+ $ab->setTemplateFile(__DIR__.'/templates/'.$tplFile);
+ }
- $indent = $input->getOption('indent');
- if ($indent->value) {
- if (is_numeric($indent->value)) {
- $ab->setIndent(str_repeat(' ', $indent->value));
- } else {
- $ab->setIndent($indent->value);
+ $format = $input->getOption('format');
+ if ($format->value) {
+ $ab->setDateTimeFormat($format->value);
+ }
+
+ $indent = $input->getOption('indent');
+ if ($indent->value) {
+ if (is_numeric($indent->value)) {
+ $ab->setIndent(str_repeat(' ', $indent->value));
+ } else {
+ $ab->setIndent($indent->value);
+ }
+ } elseif ($isStatic) {
+ $ab->setIndent('');
}
- } elseif ($isStatic) {
- $ab->setIndent('');
- }
-
- $linebreak = $input->getOption('linebreak');
- if ($linebreak->value !== false) {
- $lbr = array('LF' => "\n", 'CR' => "\r", 'CRLF' => "\r\n" );
- if (isset($lbr[$linebreak->value])) {
- $ab->setLineBreak($lbr[$linebreak->value]);
+
+ $linebreak = $input->getOption('linebreak');
+ if ($linebreak->value !== false) {
+ $lbr = array('LF' => "\n", 'CR' => "\r", 'CRLF' => "\r\n" );
+ if (isset($lbr[$linebreak->value])) {
+ $ab->setLineBreak($lbr[$linebreak->value]);
+ } else {
+ $ab->setLineBreak($linebreak->value);
+ }
} else {
- $ab->setLineBreak($linebreak->value);
+ $ab->setLineBreak("\n");
}
- } else {
- $ab->setLineBreak("\n");
- }
- return $ab;
- }
+ return $ab;
+ }
- protected function buildPhar(\Iterator $scanner, \ezcConsoleInput $input) {
- $basedir = $input->getOption('basedir')->value;
- $phar = new \Phar($input->getOption('output')->value, 0, basename($input->getOption('output')->value));
- $phar->startBuffering();
- if ($basedir) {
- $phar->buildFromIterator($scanner, $basedir);
- } else {
- $args = $input->getArguments();
- $phar->buildFromIterator($scanner, $args[0]);
- }
- return $phar;
- }
-
- /**
- * Helper to execute a lint check on generated code
- *
- * @param string $code Generated code to lint
- * @param \ezcConsoleInput $input CLI Options pased to app
- *
- * @return boolean
- */
- protected function lintCode($code, $input) {
- $dsp = array(
+ protected function buildPhar(\Iterator $scanner, \ezcConsoleInput $input) {
+ $basedir = $input->getOption('basedir')->value;
+ $phar = new \Phar($input->getOption('output')->value, 0, basename($input->getOption('output')->value));
+ $phar->startBuffering();
+ if ($basedir) {
+ $phar->buildFromIterator($scanner, $basedir);
+ } else {
+ $args = $input->getArguments();
+ $phar->buildFromIterator($scanner, $args[0]);
+ }
+ return $phar;
+ }
+
+ /**
+ * Helper to execute a lint check on generated code
+ *
+ * @param string $code Generated code to lint
+ * @param \ezcConsoleInput $input CLI Options pased to app
+ *
+ * @return boolean
+ */
+ protected function lintCode($code, $input) {
+ $dsp = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w")
- );
+ );
- $php = $input->getOption('lint-php');
- if ($php->value === false) {
- $binary = PHP_OS === 'WIN' ? 'C:\php\php.exe' : '/usr/bin/php';
- } else {
- $binary = $php->value;
- }
+ $php = $input->getOption('lint-php');
+ if ($php->value === false) {
+ $binary = PHP_OS === 'WIN' ? 'C:\php\php.exe' : '/usr/bin/php';
+ } else {
+ $binary = $php->value;
+ }
- $process = proc_open($binary . ' -l', $dsp, $pipes);
+ $process = proc_open($binary . ' -l', $dsp, $pipes);
- if (!is_resource($process)) {
- fwrite(STDERR, "Opening php binary for linting failed.\n");
- exit(1);
- }
+ if (!is_resource($process)) {
+ fwrite(STDERR, "Opening php binary for linting failed.\n");
+ exit(1);
+ }
- fwrite($pipes[0], $code);
- fclose($pipes[0]);
+ fwrite($pipes[0], $code);
+ fclose($pipes[0]);
- $stdout = stream_get_contents($pipes[1]);
- fclose($pipes[1]);
+ $stdout = stream_get_contents($pipes[1]);
+ fclose($pipes[1]);
- $stderr = stream_get_contents($pipes[2]);
- fclose($pipes[2]);
+ $stderr = stream_get_contents($pipes[2]);
+ fclose($pipes[2]);
- $rc = proc_close($process);
+ $rc = proc_close($process);
- if ($rc == 255) {
- fwrite(STDERR, "Syntax errors during lint:\n" .
- str_replace('in - on line', 'in generated code on line', $stderr) .
+ if ($rc == 255) {
+ fwrite(STDERR, "Syntax errors during lint:\n" .
+ str_replace('in - on line', 'in generated code on line', $stderr) .
"\n");
- return false;
- }
-
- echo "Lint check of geneated code okay\n\n";
- return true;
- }
-
- /**
- * Helper to output version information
- */
- protected function showVersion() {
- printf("phpab %s - Copyright (C) 2009 - 2011 by Arne Blankerts\n\n", self::VERSION);
- }
-
- /**
- * Helper to output usage information
- */
- protected function showUsage() {
- print <<<EOF
+ return false;
+ }
+
+ echo "Lint check of geneated code okay\n\n";
+ return true;
+ }
+
+ /**
+ * Helper to output version information
+ */
+ protected function showVersion() {
+ printf("phpab %s - Copyright (C) 2009 - 2011 by Arne Blankerts\n\n", self::VERSION);
+ }
+
+ /**
+ * Helper to output usage information
+ */
+ protected function showUsage() {
+ print <<<EOF
Usage: phpab [switches] <directory>
-i, --include File pattern to include (default: *.php)
@@ -434,7 +434,7 @@ protected function showUsage() {
EOF;
- }
- }
+ }
+ }
}
View
96 src/dependencysorter.php
@@ -35,65 +35,65 @@
* @license BSD License
*/
-namespace TheSeer\Tools {
+namespace TheSeer\Autoload {
- /**
- * Sorting classes by depdendency for static requires
- *
- * @author Arne Blankerts <arne@blankerts.de>
- * @copyright Arne Blankerts <arne@blankerts.de>, All rights reserved.
- */
- class ClassDependencySorter {
+ /**
+ * Sorting classes by depdendency for static requires
+ *
+ * @author Arne Blankerts <arne@blankerts.de>
+ * @copyright Arne Blankerts <arne@blankerts.de>, All rights reserved.
+ */
+ class ClassDependencySorter {
- protected $classList;
- protected $dependencies;
+ protected $classList;
+ protected $dependencies;
- protected $level;
+ protected $level;
- protected $sorted = array();
+ protected $sorted = array();
- public function __construct(Array $classes, Array $dependencies) {
- $this->classList = $classes;
- $this->dependencies = $dependencies;
- }
+ public function __construct(Array $classes, Array $dependencies) {
+ $this->classList = $classes;
+ $this->dependencies = $dependencies;
+ }
- public function process() {
- $this->level = 0;
- foreach($this->classList as $class => $file) {
- if (!in_array($class, $this->sorted)) {
- $this->resolve($class);
+ public function process() {
+ $this->level = 0;
+ foreach($this->classList as $class => $file) {
+ if (!in_array($class, $this->sorted)) {
+ $this->resolve($class);
+ }
}
- }
- $res = array();
- foreach($this->sorted as $class) {
- if (!isset($this->classList[$class])) {
- continue;
+ $res = array();
+ foreach($this->sorted as $class) {
+ if (!isset($this->classList[$class])) {
+ continue;
+ }
+ $res[$class] = $this->classList[$class];
}
- $res[$class] = $this->classList[$class];
- }
- return $res;
- }
+ return $res;
+ }
- protected function resolve($class) {
- $this->level++;
- if ($this->level==50) {
- throw new ClassDependencySorterException("Can't resolve more than 50 levels of dependencies",ClassDependencySorterException::TooManyDependencyLevels);
- }
- if (isset($this->dependencies[$class])) {
- foreach($this->dependencies[$class] as $depclass) {
- if (!in_array($depclass, $this->sorted)) {
- $this->resolve($depclass);
- }
+ protected function resolve($class) {
+ $this->level++;
+ if ($this->level==50) {
+ throw new ClassDependencySorterException("Can't resolve more than 50 levels of dependencies",ClassDependencySorterException::TooManyDependencyLevels);
}
- }
- $this->sorted[] = $class;
- $this->level--;
- }
- }
+ if (isset($this->dependencies[$class])) {
+ foreach($this->dependencies[$class] as $depclass) {
+ if (!in_array($depclass, $this->sorted)) {
+ $this->resolve($depclass);
+ }
+ }
+ }
+ $this->sorted[] = $class;
+ $this->level--;
+ }
+ }
- class ClassDependencySorterException extends \Exception {
+ class ClassDependencySorterException extends \Exception {
- const TooManyDependencyLevels = 1;
+ const TooManyDependencyLevels = 1;
- }
+ }
}
View
82 src/staticbuilder.php
@@ -35,60 +35,60 @@
* @license BSD License
*/
-namespace TheSeer\Tools {
+namespace TheSeer\Autoload {
- /**
- * Builds static require list for inclusion into projects
- *
- * @author Arne Blankerts <arne@blankerts.de>
- * @copyright Arne Blankerts <arne@blankerts.de>, All rights reserved.
- */
- class StaticBuilder extends AutoloadBuilder {
+ /**
+ * Builds static require list for inclusion into projects
+ *
+ * @author Arne Blankerts <arne@blankerts.de>
+ * @copyright Arne Blankerts <arne@blankerts.de>, All rights reserved.
+ */
+ class StaticBuilder extends AutoloadBuilder {
- protected $dependencies;
- protected $phar;
+ protected $dependencies;
+ protected $phar;
- public function setDependencies(Array $dep) {
- $this->dependencies = $dep;
- }
+ public function setDependencies(Array $dep) {
+ $this->dependencies = $dep;
+ }
- public function setPharMode($phar) {
- $this->phar = $phar;
- }
+ public function setPharMode($phar) {
+ $this->phar = $phar;
+ }
- public function render() {
- $baseDir = '';
- if ($this->phar) {
- $baseDir = "'phar://". $this->variables['___PHAR___']."' . ";
- } else if ($this->baseDir) {
- $baseDir = $this->compat ? 'dirname(__FILE__) . ' : '__DIR__ . ';
- }
+ public function render() {
+ $baseDir = '';
+ if ($this->phar) {
+ $baseDir = "'phar://". $this->variables['___PHAR___']."' . ";
+ } else if ($this->baseDir) {
+ $baseDir = $this->compat ? 'dirname(__FILE__) . ' : '__DIR__ . ';
+ }
- $entries = $this->sortByDependency();
+ $entries = $this->sortByDependency();
- $replace = array_merge($this->variables, array(
+ $replace = array_merge($this->variables, array(
'___CREATED___' => date( $this->dateformat, $this->timestamp ? $this->timestamp : time()),
'___FILELIST___' => join( $this->linebreak . $this->indent, $entries),
'___BASEDIR___' => $baseDir,
'___AUTOLOAD___' => uniqid('autoload')
- ));
+ ));
- return str_replace(array_keys($replace), array_values($replace), $this->template);
- }
+ return str_replace(array_keys($replace), array_values($replace), $this->template);
+ }
- protected function sortByDependency() {
- $sorter = new ClassDependencySorter($this->classes, $this->dependencies);
- $list = $sorter->process();
- $entries = array();
- foreach(array_unique($list) as $file) {
- $fname = realpath($file);
- if (!empty($this->baseDir) && strpos($fname, $this->baseDir)===0) {
- $fname = str_replace($this->baseDir, '', $fname);
+ protected function sortByDependency() {
+ $sorter = new ClassDependencySorter($this->classes, $this->dependencies);
+ $list = $sorter->process();
+ $entries = array();
+ foreach(array_unique($list) as $file) {
+ $fname = realpath($file);
+ if (!empty($this->baseDir) && strpos($fname, $this->baseDir)===0) {
+ $fname = str_replace($this->baseDir, '', $fname);
+ }
+ $entries[] = "require ___BASEDIR___'$fname';";
}
- $entries[] = "require ___BASEDIR___'$fname';";
- }
- return $entries;
- }
- }
+ return $entries;
+ }
+ }
}
View
434 tests/autoloadbuilder.test.php
@@ -35,223 +35,221 @@
* @license BSD License
*/
-namespace TheSeer\Tools\Tests {
-
- use TheSeer\Tools;
-
- use TheSeer\Tools\ClassFinder;
- use TheSeer\Tools\AutoloadBuilder;
-
- /**
- * Unit tests for PHPFilter iterator class
- *
- * @author Arne Blankerts <arne@blankerts.de>
- * @copyright Arne Blankerts <arne@blankerts.de>, All rights reserved.
- */
- class AutoloadBuilderTest extends \PHPUnit_Framework_TestCase {
-
- protected $classlist;
-
- public function setUp() {
- $this->classlist = array();
- $this->classlist['demo1'] = realpath(__DIR__ . '/_data/classfinder/class.php');
- $this->classlist['demo2'] = realpath(__DIR__ . '/_data/classfinder/class.php');
- }
-
- /**
- *
- * @covers \TheSeer\Tools\AutoloadBuilder::__construct
- * @covers \TheSeer\Tools\AutoloadBuilder::render
- */
- public function testDefaultRendering() {
- $ab = new \TheSeer\Tools\AutoloadBuilder($this->classlist);
- $expected = " \$classes = array(\n 'demo1' => '".__DIR__."/_data/classfinder/class.php',\n";
- $this->assertContains($expected, $ab->render());
- $expected = "require \$classes[\$cn]";
- $this->assertContains($expected, $ab->render());
- }
-
- /**
- *
- * @covers \TheSeer\Tools\AutoloadBuilder::save
- */
- public function testSaveFile() {
- $tempFileName = sprintf('%s/%s.php', sys_get_temp_dir(), uniqid());
- $ab = new \TheSeer\Tools\AutoloadBuilder($this->classlist);
- $ab->save($tempFileName);
- $this->assertFileExists($tempFileName);
- unlink($tempFileName);
- }
-
- /**
- *
- * @covers \TheSeer\Tools\AutoloadBuilder::setTemplateFile
- * @expectedException \TheSeer\Tools\AutoloadBuilderException
- */
- public function testExceptionForSettingTemplateFileToNonExisting() {
- $ab = new \TheSeer\Tools\AutoloadBuilder($this->classlist);
- $ab->setTemplateFile('NonExistend.file');
- }
-
- /**
- *
- * @covers \TheSeer\Tools\AutoloadBuilder::setTemplateFile
- */
- public function testSettingNonDefaultTemplate() {
- $ab = new \TheSeer\Tools\AutoloadBuilder($this->classlist);
- $ab->setTemplateFile(__DIR__ . '/_data/templates/simple.php');
- $expected = "'demo1' => '".__DIR__."/_data/classfinder/class.php',\n";
- $this->assertContains($expected, $ab->render());
- }
-
- /**
- *
- * @covers \TheSeer\Tools\AutoloadBuilder::setTemplateCode
- */
- public function testSettingTemplateCode() {
- $ab = new \TheSeer\Tools\AutoloadBuilder($this->classlist);
- $ab->setTemplateCode('___CLASSLIST___');
- $expected = "'demo1' => '".__DIR__."/_data/classfinder/class.php',\n";
- $this->assertContains($expected, $ab->render());
- }
-