66class RemoteDatasource extends DataSource implements iDatasource
77{
88
9+ private static $ transformer = null ;
910 private static $ url_result = null ;
10-
1111 private static $ cacheable = null ;
1212
1313 public static function getName ()
@@ -132,23 +132,7 @@ public static function isValidURL($url, $timeout = 6, $format = 'xml', $fetch_UR
132132 if (trim ($ url ) == '' ) {
133133 return __ ('This is a required field ' );
134134 } elseif ($ fetch_URL === true ) {
135- $ gateway = new Gateway ;
136- $ gateway ->init ($ url );
137- $ gateway ->setopt ('TIMEOUT ' , $ timeout );
138-
139- // Set the approtiate Accept: headers depending on the format of the URL.
140- if ($ format == 'xml ' ) {
141- $ gateway ->setopt ('HTTPHEADER ' , array ('Accept: text/xml, */* ' ));
142- } elseif ($ format == 'json ' ) {
143- $ gateway ->setopt ('HTTPHEADER ' , array ('Accept: application/json, */* ' ));
144- } elseif ($ format == 'csv ' ) {
145- $ gateway ->setopt ('HTTPHEADER ' , array ('Accept: text/csv, */* ' ));
146- }
147-
148- self ::prepareGateway ($ gateway );
149-
150- $ data = $ gateway ->exec ();
151- $ info = $ gateway ->getInfoLast ();
135+ list ($ data , $ info ) = self ::fetch ($ url , $ format , $ timeout );
152136
153137 // 28 is CURLE_OPERATION_TIMEOUTED
154138 if (isset ($ info ['curl_error ' ]) && $ info ['curl_error ' ] == 28 ) {
@@ -664,27 +648,8 @@ public function execute(array &$param_pool = null)
664648 || (time () - $ cachedData ['creation ' ]) > ($ this ->dsParamCACHE * 60 ) // The cache is old.
665649 ) {
666650 if (Mutex::acquire ($ cache_id , $ this ->dsParamTIMEOUT , TMP )) {
667- $ ch = new Gateway ;
668- $ ch ->init ($ this ->dsParamURL );
669- $ ch ->setopt ('TIMEOUT ' , $ this ->dsParamTIMEOUT );
670-
671- // Set the approtiate Accept: headers depending on the format of the URL.
672- if ($ this ->dsParamFORMAT == 'xml ' ) {
673- $ ch ->setopt ('HTTPHEADER ' , array ('Accept: text/xml, */* ' ));
674- } elseif ($ this ->dsParamFORMAT == 'json ' ) {
675- $ ch ->setopt ('HTTPHEADER ' , array ('Accept: application/json, */* ' ));
676- } elseif ($ this ->dsParamFORMAT == 'csv ' ) {
677- $ ch ->setopt ('HTTPHEADER ' , array ('Accept: text/csv, */* ' ));
678- }
679-
680- self ::prepareGateway ($ ch );
681-
682- $ data = $ ch ->exec ();
683- $ info = $ ch ->getInfoLast ();
684-
651+ list ($ data , $ info ) = self ::fetch ($ this ->dsParamURL , $ this ->dsParamFORMAT , $ this ->dsParamTIMEOUT );
685652 Mutex::release ($ cache_id , TMP );
686-
687- $ data = trim ($ data );
688653 $ writeToCache = true ;
689654
690655 // Handle any response that is not a 200, or the content type does not include XML, JSON, plain or text
@@ -819,6 +784,35 @@ public function execute(array &$param_pool = null)
819784 return $ result ;
820785 }
821786
787+ /**
788+ * Given a URL, Format and Timeout, this function will initalise
789+ * Symphony's Gateway class to retrieve the contents of the URL.
790+ *
791+ * @param string $url
792+ * @param string $format
793+ * @param integer $timeout
794+ * @return array
795+ */
796+ public static function fetch ($ url , $ format , $ timeout )
797+ {
798+ $ ch = new Gateway ;
799+ $ ch ->init ($ url );
800+ $ ch ->setopt ('TIMEOUT ' , $ timeout );
801+
802+ // Set the approtiate Accept: headers depending on the format of the URL.
803+ if ($ transformer = self ::getTransformer ($ format )) {
804+ $ accepts = $ transformer ->accepts ();
805+ $ ch ->setopt ('HTTPHEADER ' , array ('Accept: ' . $ accepts ));
806+ }
807+
808+ self ::prepareGateway ($ ch );
809+
810+ return array (
811+ trim ($ ch ->exec ()),
812+ $ ch ->getInfoLast ()
813+ );
814+ }
815+
822816 /**
823817 * Given the result (a string), and a desired format, this
824818 * function will transform it to the desired format and return
@@ -828,16 +822,35 @@ public function execute(array &$param_pool = null)
828822 * @return string
829823 */
830824 public static function transformResult ($ data , $ format )
825+ {
826+ if ($ transformer = self ::getTransformer ($ format )) {
827+ $ data = $ transformer ->transform ($ data );
828+ } else {
829+ $ data = '' ;
830+ }
831+
832+ return $ data ;
833+ }
834+
835+ /**
836+ * Given the format, this function will look for the file
837+ * and create a new transformer.
838+ *
839+ * @param string $format
840+ * @return Transformer
841+ */
842+ public static function getTransformer ($ format )
831843 {
832844 $ transformer = EXTENSIONS . '/remote_datasource/lib/class. ' . strtolower ($ format ) . '.php ' ;
833845
834- if (file_exists ($ transformer )) {
835- $ classname = require_once $ transformer ;
836- $ classname = new $ classname ;
837- $ data = $ classname ->transform ($ data );
846+ if (!isset (self ::$ transformer )) {
847+ if (file_exists ($ transformer )) {
848+ $ classname = require_once $ transformer ;
849+ self ::$ transformer = new $ classname ;
850+ }
838851 }
839852
840- return $ data ;
853+ return self :: $ transformer ;
841854 }
842855}
843856
0 commit comments