MaxSem has uploaded a new change for review. https://gerrit.wikimedia.org/r/255484
Change subject: [proof of concept] Make ContextSource traitable ...................................................................... [proof of concept] Make ContextSource traitable As discussed in https://tools.wmflabs.org/meetbot/wikimedia-office/2015/wikimedia-office.2015-11-25-22.00.html , this is a demo of how traits can be used. Not to be merged, of course :) Change-Id: I6f6faaeabe8f71820031e69627114b402d43dad1 --- M autoload.php M includes/OutputPage.php M includes/context/ContextSource.php A includes/context/TContextSource.php M includes/utils/AutoloadGenerator.php M tests/phpunit/structure/AutoLoaderTest.php 6 files changed, 184 insertions(+), 174 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/84/255484/1 diff --git a/autoload.php b/autoload.php index 2844dc7..ff9389e 100644 --- a/autoload.php +++ b/autoload.php @@ -164,8 +164,8 @@ 'BenchIfSwitch' => __DIR__ . '/maintenance/benchmarks/bench_if_switch.php', 'BenchStrtrStrReplace' => __DIR__ . '/maintenance/benchmarks/bench_strtr_str_replace.php', 'BenchUtf8TitleCheck' => __DIR__ . '/maintenance/benchmarks/bench_utf8_title_check.php', - 'BenchWikimediaBaseConvert' => __DIR__ . '/maintenance/benchmarks/bench_Wikimedia_base_convert.php', 'BenchWfIsWindows' => __DIR__ . '/maintenance/benchmarks/bench_wfIsWindows.php', + 'BenchWikimediaBaseConvert' => __DIR__ . '/maintenance/benchmarks/bench_Wikimedia_base_convert.php', 'BenchmarkDeleteTruncate' => __DIR__ . '/maintenance/benchmarks/bench_delete_truncate.php', 'BenchmarkHooks' => __DIR__ . '/maintenance/benchmarks/benchmarkHooks.php', 'BenchmarkParse' => __DIR__ . '/maintenance/benchmarks/benchmarkParse.php', @@ -1232,6 +1232,7 @@ 'SwiftFileOpHandle' => __DIR__ . '/includes/filebackend/SwiftFileBackend.php', 'SwiftVirtualRESTService' => __DIR__ . '/includes/libs/virtualrest/SwiftVirtualRESTService.php', 'SyncFileBackend' => __DIR__ . '/maintenance/syncFileBackend.php', + 'TContextSource' => __DIR__ . '/includes/context/TContextSource.php', 'TableCleanup' => __DIR__ . '/maintenance/cleanupTable.inc', 'TableCleanupTest' => __DIR__ . '/maintenance/cleanupTable.inc', 'TableDiffFormatter' => __DIR__ . '/includes/diff/TableDiffFormatter.php', diff --git a/includes/OutputPage.php b/includes/OutputPage.php index c35204d..c869147 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -38,7 +38,9 @@ * * @todo document */ -class OutputPage extends ContextSource { +class OutputPage implements IContextSource { + use TContextSource; + /** @var array Should be private. Used with addMeta() which adds "<meta>" */ protected $mMetatags = array(); diff --git a/includes/context/ContextSource.php b/includes/context/ContextSource.php index a8850fc..3d50c85 100644 --- a/includes/context/ContextSource.php +++ b/includes/context/ContextSource.php @@ -26,175 +26,5 @@ * @since 1.18 */ abstract class ContextSource implements IContextSource { - /** - * @var IContextSource - */ - private $context; - - /** - * Get the base IContextSource object - * @since 1.18 - * @return IContextSource - */ - public function getContext() { - if ( $this->context === null ) { - $class = get_class( $this ); - wfDebug( __METHOD__ . " ($class): called and \$context is null. " . - "Using RequestContext::getMain() for sanity\n" ); - $this->context = RequestContext::getMain(); - } - - return $this->context; - } - - /** - * Set the IContextSource object - * - * @since 1.18 - * @param IContextSource $context - */ - public function setContext( IContextSource $context ) { - $this->context = $context; - } - - /** - * Get the Config object - * - * @since 1.23 - * @return Config - */ - public function getConfig() { - return $this->getContext()->getConfig(); - } - - /** - * Get the WebRequest object - * - * @since 1.18 - * @return WebRequest - */ - public function getRequest() { - return $this->getContext()->getRequest(); - } - - /** - * Get the Title object - * - * @since 1.18 - * @return Title|null - */ - public function getTitle() { - return $this->getContext()->getTitle(); - } - - /** - * Check whether a WikiPage object can be get with getWikiPage(). - * Callers should expect that an exception is thrown from getWikiPage() - * if this method returns false. - * - * @since 1.19 - * @return bool - */ - public function canUseWikiPage() { - return $this->getContext()->canUseWikiPage(); - } - - /** - * Get the WikiPage object. - * May throw an exception if there's no Title object set or the Title object - * belongs to a special namespace that doesn't have WikiPage, so use first - * canUseWikiPage() to check whether this method can be called safely. - * - * @since 1.19 - * @return WikiPage - */ - public function getWikiPage() { - return $this->getContext()->getWikiPage(); - } - - /** - * Get the OutputPage object - * - * @since 1.18 - * @return OutputPage - */ - public function getOutput() { - return $this->getContext()->getOutput(); - } - - /** - * Get the User object - * - * @since 1.18 - * @return User - */ - public function getUser() { - return $this->getContext()->getUser(); - } - - /** - * Get the Language object - * - * @since 1.19 - * @return Language - */ - public function getLanguage() { - return $this->getContext()->getLanguage(); - } - - /** - * Get the Skin object - * - * @since 1.18 - * @return Skin - */ - public function getSkin() { - return $this->getContext()->getSkin(); - } - - /** - * Get the Timing object - * - * @since 1.27 - * @return Timing - */ - public function getTiming() { - return $this->getContext()->getTiming(); - } - - /** - * Get the Stats object - * - * @since 1.25 - * @return BufferingStatsdDataFactory - */ - public function getStats() { - return $this->getContext()->getStats(); - } - - - /** - * Get a Message object with context set - * Parameters are the same as wfMessage() - * - * @since 1.18 - * @param mixed ... - * @return Message - */ - public function msg( /* $args */ ) { - $args = func_get_args(); - - return call_user_func_array( array( $this->getContext(), 'msg' ), $args ); - } - - /** - * Export the resolved user IP, HTTP headers, user ID, and session ID. - * The result will be reasonably sized to allow for serialization. - * - * @return array - * @since 1.21 - */ - public function exportSession() { - return $this->getContext()->exportSession(); - } + use TContextSource; } diff --git a/includes/context/TContextSource.php b/includes/context/TContextSource.php new file mode 100644 index 0000000..013b16d --- /dev/null +++ b/includes/context/TContextSource.php @@ -0,0 +1,175 @@ +<?php + +trait TContextSource { + /** + * @var IContextSource + */ + private $context; + + /** + * Get the base IContextSource object + * @since 1.18 + * @return IContextSource + */ + public function getContext() { + if ( $this->context === null ) { + $class = get_class( $this ); + wfDebug( __METHOD__ . " ($class): called and \$context is null. " . + "Using RequestContext::getMain() for sanity\n" ); + $this->context = RequestContext::getMain(); + } + + return $this->context; + } + + /** + * Set the IContextSource object + * + * @since 1.18 + * @param IContextSource $context + */ + public function setContext( IContextSource $context ) { + $this->context = $context; + } + + /** + * Get the Config object + * + * @since 1.23 + * @return Config + */ + public function getConfig() { + return $this->getContext()->getConfig(); + } + + /** + * Get the WebRequest object + * + * @since 1.18 + * @return WebRequest + */ + public function getRequest() { + return $this->getContext()->getRequest(); + } + + /** + * Get the Title object + * + * @since 1.18 + * @return Title|null + */ + public function getTitle() { + return $this->getContext()->getTitle(); + } + + /** + * Check whether a WikiPage object can be get with getWikiPage(). + * Callers should expect that an exception is thrown from getWikiPage() + * if this method returns false. + * + * @since 1.19 + * @return bool + */ + public function canUseWikiPage() { + return $this->getContext()->canUseWikiPage(); + } + + /** + * Get the WikiPage object. + * May throw an exception if there's no Title object set or the Title object + * belongs to a special namespace that doesn't have WikiPage, so use first + * canUseWikiPage() to check whether this method can be called safely. + * + * @since 1.19 + * @return WikiPage + */ + public function getWikiPage() { + return $this->getContext()->getWikiPage(); + } + + /** + * Get the OutputPage object + * + * @since 1.18 + * @return OutputPage + */ + public function getOutput() { + return $this->getContext()->getOutput(); + } + + /** + * Get the User object + * + * @since 1.18 + * @return User + */ + public function getUser() { + return $this->getContext()->getUser(); + } + + /** + * Get the Language object + * + * @since 1.19 + * @return Language + */ + public function getLanguage() { + return $this->getContext()->getLanguage(); + } + + /** + * Get the Skin object + * + * @since 1.18 + * @return Skin + */ + public function getSkin() { + return $this->getContext()->getSkin(); + } + + /** + * Get the Timing object + * + * @since 1.27 + * @return Timing + */ + public function getTiming() { + return $this->getContext()->getTiming(); + } + + /** + * Get the Stats object + * + * @since 1.25 + * @return BufferingStatsdDataFactory + */ + public function getStats() { + return $this->getContext()->getStats(); + } + + + /** + * Get a Message object with context set + * Parameters are the same as wfMessage() + * + * @since 1.18 + * @param mixed ... + * @return Message + */ + public function msg( /* $args */ ) { + $args = func_get_args(); + + return call_user_func_array( array( $this->getContext(), 'msg' ), $args ); + } + + /** + * Export the resolved user IP, HTTP headers, user ID, and session ID. + * The result will be reasonably sized to allow for serialization. + * + * @return array + * @since 1.21 + */ + public function exportSession() { + return $this->getContext()->exportSession(); + } +} \ No newline at end of file diff --git a/includes/utils/AutoloadGenerator.php b/includes/utils/AutoloadGenerator.php index 7d63156..69426d7 100644 --- a/includes/utils/AutoloadGenerator.php +++ b/includes/utils/AutoloadGenerator.php @@ -310,6 +310,7 @@ case T_NAMESPACE: case T_CLASS: case T_INTERFACE: + case T_TRAIT: $this->startToken = $token; } } @@ -331,6 +332,7 @@ 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(); diff --git a/tests/phpunit/structure/AutoLoaderTest.php b/tests/phpunit/structure/AutoLoaderTest.php index 8674329..e5b7a93 100644 --- a/tests/phpunit/structure/AutoLoaderTest.php +++ b/tests/phpunit/structure/AutoLoaderTest.php @@ -71,7 +71,7 @@ $matches = array(); preg_match_all( '/ ^ [\t ]* (?: - (?:final\s+)? (?:abstract\s+)? (?:class|interface) \s+ + (?:final\s+)? (?:abstract\s+)? (?:class|interface|trait) \s+ (?P<class> [a-zA-Z0-9_]+) | class_alias \s* \( \s* -- To view, visit https://gerrit.wikimedia.org/r/255484 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I6f6faaeabe8f71820031e69627114b402d43dad1 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: MaxSem <maxsem.w...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits