88use ScriptFUSION \Porter \Connector \Recoverable \RecoverableException ;
99use ScriptFUSION \Porter \Connector \Recoverable \RecoverableExceptionHandler ;
1010use ScriptFUSION \Porter \Connector \Recoverable \StatelessRecoverableExceptionHandler ;
11+ use function Amp \call ;
12+ use function Amp \Promise \all ;
13+ use function ScriptFUSION \Retry \retry ;
14+ use function ScriptFUSION \Retry \retryAsync ;
1115
1216/**
13- * Connector whose lifecycle is synchronised with an import operation. Ensures correct ConnectionContext is delivered
14- * to the wrapped connector and intercepts failed connections to facilitate automatic retries .
17+ * Connector whose lifecycle is synchronised with an import operation. Intercepts failed connections to facilitate
18+ * automatic retries and manages caching .
1519 *
1620 * Do not store references to this connector that would prevent it expiring when an import operation ends.
1721 *
@@ -21,8 +25,6 @@ final class ImportConnector implements ConnectorWrapper
2125{
2226 private $ connector ;
2327
24- private $ connectionContext ;
25-
2628 /**
2729 * User-defined exception handler called when a recoverable exception is thrown by Connector::fetch().
2830 *
@@ -41,45 +43,49 @@ final class ImportConnector implements ConnectorWrapper
4143
4244 /**
4345 * @param Connector|AsyncConnector $connector Wrapped connector.
44- * @param ConnectionContext $connectionContext Connection context.
4546 * @param RecoverableExceptionHandler $recoverableExceptionHandler User's recoverable exception handler.
4647 * @param int $maxFetchAttempts
48+ * @param bool $mustCache True if the response must be cached, otherwise false.
4749 */
4850 public function __construct (
4951 $ connector ,
50- ConnectionContext $ connectionContext ,
5152 RecoverableExceptionHandler $ recoverableExceptionHandler ,
52- int $ maxFetchAttempts
53+ int $ maxFetchAttempts ,
54+ bool $ mustCache
5355 ) {
54- if ($ connectionContext -> mustCache () && !$ connector instanceof CachingConnector) {
56+ if ($ mustCache && !$ connector instanceof CachingConnector) {
5557 throw CacheUnavailableException::createUnsupported ();
5658 }
5759
58- $ this ->connector = clone $ connector ;
59- $ this ->connectionContext = $ connectionContext ;
60+ $ this ->connector = clone (
61+ $ connector instanceof CachingConnector && !$ mustCache
62+ // Bypass cache when not required.
63+ ? $ connector ->getWrappedConnector ()
64+ : $ connector
65+ );
6066 $ this ->userExceptionHandler = $ recoverableExceptionHandler ;
6167 $ this ->maxFetchAttempts = $ maxFetchAttempts ;
6268 }
6369
6470 public function fetch (string $ source )
6571 {
66- return \ ScriptFUSION \ Retry \ retry (
72+ return retry (
6773 $ this ->maxFetchAttempts ,
6874 function () use ($ source ) {
69- return $ this ->connector ->fetch ($ source, $ this -> connectionContext );
75+ return $ this ->connector ->fetch ($ source );
7076 },
7177 $ this ->createExceptionHandler ()
7278 );
7379 }
7480
7581 public function fetchAsync (string $ source ): Promise
7682 {
77- return \ ScriptFUSION \ Retry \ retryAsync (
83+ return retryAsync (
7884 $ this ->maxFetchAttempts ,
7985 function () use ($ source ): Promise {
80- return \ Amp \ call (
86+ return call (
8187 function () use ($ source ) {
82- return $ this ->connector ->fetchAsync ($ source, $ this -> connectionContext );
88+ return $ this ->connector ->fetchAsync ($ source );
8389 }
8490 );
8591 },
@@ -108,14 +114,15 @@ private function createExceptionHandler(): \Closure
108114 /*
109115 * Handlers may return a Promise, but all other return values are discarded. Although the underlying
110116 * library supports returning false, Porter only allows exceptions to short-circuit. However,
111- * Porter does nothing to restrict promises that return false, although it is discouraged.
117+ * Porter does nothing to restrict promises that return false, although it is discouraged and may be
118+ * prevented in future. TODO: Mask promise return values.
112119 */
113120 return ($ promises = array_filter (
114121 $ results ,
115122 static function ($ value ): bool {
116123 return $ value instanceof Promise;
117124 }
118- )) ? \ Amp \ Promise \ all ($ promises ) : null ;
125+ )) ? all ($ promises ) : null ;
119126 };
120127 }
121128
0 commit comments