From bb16bb4c7d5ee9ea6afca87f1f66163572d5ebfc Mon Sep 17 00:00:00 2001 From: PleaseStand Date: Mon, 4 Apr 2016 03:26:24 +0000 Subject: [PATCH] Revert "Split ClassCollector class into own file" The AutoloadGenerator class is instantiated in the script maintenance/generateLocalAutoload.php and its equivalents in several extensions, none of which use MediaWiki's AutoLoader, so you get a "Fatal error: Class 'ClassCollector' not found". This reverts commit a379bebd03099ee056a5ae7466f0699423064ede. Change-Id: Ia6a7a1cb8a593ac86ecffdb690064e0b8ad68b47 --- autoload.php | 2 +- includes/utils/AutoloadGenerator.php | 114 ++++++++++++++++++++++++++ includes/utils/ClassCollector.php | 115 --------------------------- 3 files changed, 115 insertions(+), 116 deletions(-) delete mode 100644 includes/utils/ClassCollector.php diff --git a/autoload.php b/autoload.php index 67a235f11f..fd4f873d41 100644 --- a/autoload.php +++ b/autoload.php @@ -230,7 +230,7 @@ $wgAutoloadLocalClasses = [ 'CheckSyntax' => __DIR__ . '/maintenance/checkSyntax.php', 'CheckUsernames' => __DIR__ . '/maintenance/checkUsernames.php', 'ChronologyProtector' => __DIR__ . '/includes/db/ChronologyProtector.php', - 'ClassCollector' => __DIR__ . '/includes/utils/ClassCollector.php', + 'ClassCollector' => __DIR__ . '/includes/utils/AutoloadGenerator.php', 'CleanupAncientTables' => __DIR__ . '/maintenance/cleanupAncientTables.php', 'CleanupBlocks' => __DIR__ . '/maintenance/cleanupBlocks.php', 'CleanupPreferences' => __DIR__ . '/maintenance/cleanupPreferences.php', diff --git a/includes/utils/AutoloadGenerator.php b/includes/utils/AutoloadGenerator.php index 4101879d77..916e2f881c 100644 --- a/includes/utils/AutoloadGenerator.php +++ b/includes/utils/AutoloadGenerator.php @@ -250,3 +250,117 @@ EOD return str_replace( '\\', '/', $path ); } } + +/** + * Reads PHP code and returns the FQCN of every class defined within it. + */ +class ClassCollector { + + /** + * @var string Current namespace + */ + protected $namespace = ''; + + /** + * @var array List of FQCN detected in this pass + */ + protected $classes; + + /** + * @var array Token from token_get_all() that started an expect sequence + */ + protected $startToken; + + /** + * @var array List of tokens that are members of the current expect sequence + */ + protected $tokens; + + /** + * @var string $code PHP code (including namespace = ''; + $this->classes = []; + $this->startToken = null; + $this->tokens = []; + + foreach ( token_get_all( $code ) as $token ) { + if ( $this->startToken === null ) { + $this->tryBeginExpect( $token ); + } else { + $this->tryEndExpect( $token ); + } + } + + return $this->classes; + } + + /** + * Determine if $token begins the next expect sequence. + * + * @param array $token + */ + protected function tryBeginExpect( $token ) { + if ( is_string( $token ) ) { + return; + } + switch ( $token[0] ) { + case T_NAMESPACE: + case T_CLASS: + case T_INTERFACE: + case T_TRAIT: + case T_DOUBLE_COLON: + $this->startToken = $token; + } + } + + /** + * Accepts the next token in an expect sequence + * + * @param array + */ + protected function tryEndExpect( $token ) { + switch ( $this->startToken[0] ) { + case T_DOUBLE_COLON: + // Skip over T_CLASS after T_DOUBLE_COLON because this is something like + // "self::static" which accesses the class name. It doens't define a new class. + $this->startToken = null; + break; + case T_NAMESPACE: + if ( $token === ';' || $token === '{' ) { + $this->namespace = $this->implodeTokens() . '\\'; + } else { + $this->tokens[] = $token; + } + break; + + case T_CLASS: + case T_INTERFACE: + case T_TRAIT: + $this->tokens[] = $token; + if ( is_array( $token ) && $token[0] === T_STRING ) { + $this->classes[] = $this->namespace . $this->implodeTokens(); + } + } + } + + /** + * Returns the string representation of the tokens within the + * current expect sequence and resets the sequence. + * + * @return string + */ + protected function implodeTokens() { + $content = []; + foreach ( $this->tokens as $token ) { + $content[] = is_string( $token ) ? $token : $token[1]; + } + + $this->tokens = []; + $this->startToken = null; + + return trim( implode( '', $content ), " \n\t" ); + } +} diff --git a/includes/utils/ClassCollector.php b/includes/utils/ClassCollector.php deleted file mode 100644 index 25231df6f9..0000000000 --- a/includes/utils/ClassCollector.php +++ /dev/null @@ -1,115 +0,0 @@ -namespace = ''; - $this->classes = []; - $this->startToken = null; - $this->tokens = []; - - foreach ( token_get_all( $code ) as $token ) { - if ( $this->startToken === null ) { - $this->tryBeginExpect( $token ); - } else { - $this->tryEndExpect( $token ); - } - } - - return $this->classes; - } - - /** - * Determine if $token begins the next expect sequence. - * - * @param array $token - */ - protected function tryBeginExpect( $token ) { - if ( is_string( $token ) ) { - return; - } - switch ( $token[0] ) { - case T_NAMESPACE: - case T_CLASS: - case T_INTERFACE: - case T_TRAIT: - case T_DOUBLE_COLON: - $this->startToken = $token; - } - } - - /** - * Accepts the next token in an expect sequence - * - * @param array - */ - protected function tryEndExpect( $token ) { - switch ( $this->startToken[0] ) { - case T_DOUBLE_COLON: - // Skip over T_CLASS after T_DOUBLE_COLON because this is something like - // "self::static" which accesses the class name. It doens't define a new class. - $this->startToken = null; - break; - case T_NAMESPACE: - if ( $token === ';' || $token === '{' ) { - $this->namespace = $this->implodeTokens() . '\\'; - } else { - $this->tokens[] = $token; - } - break; - - case T_CLASS: - case T_INTERFACE: - case T_TRAIT: - $this->tokens[] = $token; - if ( is_array( $token ) && $token[0] === T_STRING ) { - $this->classes[] = $this->namespace . $this->implodeTokens(); - } - } - } - - /** - * Returns the string representation of the tokens within the - * current expect sequence and resets the sequence. - * - * @return string - */ - protected function implodeTokens() { - $content = []; - foreach ( $this->tokens as $token ) { - $content[] = is_string( $token ) ? $token : $token[1]; - } - - $this->tokens = []; - $this->startToken = null; - - return trim( implode( '', $content ), " \n\t" ); - } -} -- 2.20.1