r70285 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r70284‎ | r70285 | r70286 >
Date:19:37, 1 August 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Changes for 0.6.6 - Improvements to how instances of services and feature classes are created and handled over.
Modified paths:
  • /trunk/extensions/Maps/Features/Maps_ParserFunctions.php (modified) (history)
  • /trunk/extensions/Maps/Includes/Maps_Mapper.php (modified) (history)
  • /trunk/extensions/Maps/Services/Maps_MappingService.php (modified) (history)
  • /trunk/extensions/Maps/Services/iMappingService.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Maps/Services/Maps_MappingService.php
@@ -79,6 +79,89 @@
8080 private $dependencies = array();
8181
8282 /**
 83+ * Retuns an instance of a MapsMappingService. The service name is validated
 84+ * and aliases are resolved and a check is made if the feature is supported.
 85+ * If the feature is not supported, or the service does not exist, defaulting
 86+ * will be used.
 87+ *
 88+ * @since 0.6.6
 89+ *
 90+ * @param string $service
 91+ * @param string $feature
 92+ *
 93+ * @return iMappingService
 94+ */
 95+ public static function getServiceInstance( $service, $feature ) {
 96+ global $egMapsServices;
 97+ return $egMapsServices[self::getValidService( $service, $feature )];
 98+ }
 99+
 100+ /**
 101+ * Returns a valid service. When an invalid service is provided, the default one will be returned.
 102+ * Aliases are also chancged into the main service names @see MapsMapper::getMainServiceName.
 103+ *
 104+ * @since 0.6.6
 105+ *
 106+ * @param string $service
 107+ * @param string $feature
 108+ *
 109+ * @return string
 110+ */
 111+ public static function getValidService( $service, $feature ) {
 112+ global $egMapsServices, $egMapsDefaultService, $egMapsDefaultServices, $shouldChange;
 113+
 114+ // Get rid of any aliases.
 115+ $service = self::getMainServiceName( $service );
 116+
 117+ // If the service is not loaded into maps, it should be changed.
 118+ $shouldChange = !array_key_exists( $service, $egMapsServices );
 119+
 120+ // If it should not be changed, ensure the service supports this feature.
 121+ if ( !$shouldChange ) {
 122+ $shouldChange = $egMapsServices[$service]->getFeature( $feature ) === false;
 123+ }
 124+
 125+ // Change the service to the most specific default value available.
 126+ // Note: the default services should support their corresponding features.
 127+ // If they don't, a fatal error will occur later on.
 128+ if ( $shouldChange ) {
 129+ if ( array_key_exists( $feature, $egMapsDefaultServices ) ) {
 130+ $service = $egMapsDefaultServices[$feature];
 131+ }
 132+ else {
 133+ $service = $egMapsDefaultService;
 134+ }
 135+ }
 136+
 137+ return $service;
 138+ }
 139+
 140+ /**
 141+ * Checks if the service name is an alias for an actual service,
 142+ * and changes it into the main service name if this is the case.
 143+ *
 144+ * @since 0.6.6
 145+ *
 146+ * @param string $service
 147+ *
 148+ * @return string
 149+ */
 150+ private static function getMainServiceName( $service ) {
 151+ global $egMapsServices;
 152+
 153+ if ( !array_key_exists( $service, $egMapsServices ) ) {
 154+ foreach ( $egMapsServices as $serviceObject ) {
 155+ if ( $serviceObject->hasAlias( $service ) ) {
 156+ $service = $serviceObject->getName();
 157+ break;
 158+ }
 159+ }
 160+ }
 161+
 162+ return $service;
 163+ }
 164+
 165+ /**
83166 * Constructor. Creates a new instance of MapsMappingService.
84167 *
85168 * @since 0.6.3
@@ -191,6 +274,21 @@
192275 }
193276
194277 /**
 278+ * @see iMappingService::getFeatureInstance
 279+ *
 280+ * @since 0.6.6
 281+ */
 282+ public function getFeatureInstance( $featureName ) {
 283+ $className = $this->getFeature( $featureName );
 284+
 285+ if ( $className === false || !class_exists( $className ) ) {
 286+ // TODO: log/throw error, as this should not happen
 287+ }
 288+
 289+ return new $className( $this );
 290+ }
 291+
 292+ /**
195293 * @see iMappingService::getAliases
196294 *
197295 * @since 0.6.3
Index: trunk/extensions/Maps/Services/iMappingService.php
@@ -91,6 +91,17 @@
9292 function getFeature( $featureName );
9393
9494 /**
 95+ * Returns an instance of the class handling the provided feature with this service, or false if there is none.
 96+ *
 97+ * @since 0.6.6
 98+ *
 99+ * @param string $featureName.
 100+ *
 101+ * @return iMappingFeature or false
 102+ */
 103+ function getFeatureInstance( $featureName );
 104+
 105+ /**
95106 * Returns a list of aliases.
96107 *
97108 * @since 0.6.5
Index: trunk/extensions/Maps/Includes/Maps_Mapper.php
@@ -95,69 +95,15 @@
9696 $location = MapsCoordinateParser::parseAndFormat( $location );
9797 }
9898 }
99 -
 99+
100100 /**
101 - * Returns a valid service. When an invalid service is provided, the default one will be returned.
102 - * Aliases are also chancged into the main service names @see MapsMapper::getMainServiceName.
103 - *
104 - * @param string $service
105 - * @param string $feature
106 - *
107 - * @return string
 101+ * @deprecated Method moved to MapsMappingService. Will be removed in 0.7.
108102 */
109103 public static function getValidService( $service, $feature ) {
110 - global $egMapsServices, $egMapsDefaultService, $egMapsDefaultServices, $shouldChange;
111 -
112 - // Get rid of any aliases.
113 - $service = self::getMainServiceName( $service );
114 -
115 - // If the service is not loaded into maps, it should be changed.
116 - $shouldChange = !array_key_exists( $service, $egMapsServices );
117 -
118 - // If it should not be changed, ensure the service supports this feature.
119 - if ( ! $shouldChange ) {
120 - $shouldChange = $egMapsServices[$service]->getFeature( $feature ) === false;
121 - }
122 -
123 - // Change the service to the most specific default value available.
124 - // Note: the default services should support their corresponding features.
125 - // If they don't, a fatal error will occur later on.
126 - if ( $shouldChange ) {
127 - if ( array_key_exists( $feature, $egMapsDefaultServices ) ) {
128 - $service = $egMapsDefaultServices[$feature];
129 - }
130 - else {
131 - $service = $egMapsDefaultService;
132 - }
133 - }
134 -
135 - return $service;
 104+ MapsMappingService::getValidService( $service, $feature );
136105 }
137106
138107 /**
139 - * Checks if the service name is an alias for an actual service,
140 - * and changes it into the main service name if this is the case.
141 - *
142 - * @param string $service
143 - *
144 - * @return string
145 - */
146 - private static function getMainServiceName( $service ) {
147 - global $egMapsServices;
148 -
149 - if ( !array_key_exists( $service, $egMapsServices ) ) {
150 - foreach ( $egMapsServices as $serviceObject ) {
151 - if ( $serviceObject->hasAlias( $service ) ) {
152 - $service = $serviceObject->getName();
153 - break;
154 - }
155 - }
156 - }
157 -
158 - return $service;
159 - }
160 -
161 - /**
162108 * Determines if a value is a valid map dimension, and optionally corrects it.
163109 *
164110 * @since 0.6
Index: trunk/extensions/Maps/Features/Maps_ParserFunctions.php
@@ -82,16 +82,12 @@
8383 }
8484 }
8585
86 - // Make sure the service name is valid, and then get the class associated with it.
87 - $serviceName = MapsMapper::getValidService( $setService ? $serviceName : '', $parserFunction );
88 - $service = $egMapsServices[$serviceName];
 86+ // Get the instance of the service class.
 87+ $service = MapsMappingService::getServiceInstance( $setService ? $serviceName : '', $parserFunction );
8988
90 - // Get the name of the class handling the current parser function and service.
91 - $className = $service->getFeature( $parserFunction );
92 - $mapClass = new $className( $service );
 89+ // Get an instance of the class handling the current parser function and service.
 90+ $mapClass = $service->getFeatureInstance( $parserFunction );
9391
94 - $manager = new ValidatorManager();
95 -
9692 /*
9793 * Assembliy of the allowed parameters and their information.
9894 * The main parameters (the ones that are shared by everything) are overidden
@@ -103,6 +99,8 @@
104100 $parameterInfo = array_merge_recursive( $parameterInfo, $service->getParameterInfo() );
105101 $parameterInfo = array_merge_recursive( $parameterInfo, $mapClass->getSpecificParameterInfo() );
106102
 103+ $manager = new ValidatorManager();
 104+
107105 $displayMap = $manager->manageParameters(
108106 $parameters,
109107 $parameterInfo,

Status & tagging log