/** * Start discovery process * * @param ResponseInterface|null $response * @param CacheProvider|null $cache * @return array */ public function start(ResponseInterface $response = null, CacheProvider $cache = null) { $cacheDir = $this->config->getCacheDirectory(); $cacheKey = $this->config->getApiProperty('id'); $cacheLifetime = $this->config->getCacheLifetime(); $response = $response ?: new Response(); $cache = $cache ?: new FilesystemCache($cacheDir); if ($cache->contains($cacheKey)) { $classMap = $cache->fetch($cacheKey); } else { $classMap = $this->mapClasses(); $cache->save($cacheKey, $classMap, $cacheLifetime); } $api = $this->buildApi($classMap); $body = sprintf('%s=%s;', $this->config->getApiDescriptor(), json_encode($api, \JSON_UNESCAPED_UNICODE)); $response->getBody()->write($body); if (function_exists('openssl_random_pseudo_bytes')) { $token1 = bin2hex(openssl_random_pseudo_bytes(16)); $token2 = bin2hex(openssl_random_pseudo_bytes(16)); } else { $token1 = uniqid(); $token2 = uniqid(); } if (isset($_COOKIE['Ext-Direct-Token1'])) { $token1 = $_COOKIE['Ext-Direct-Token1']; } else { session_id($token1); } session_start(); if (isset($_SESSION['Ext-Direct-Token2'])) { $token2 = $_SESSION['Ext-Direct-Token2']; } $_SESSION['Ext-Direct-Token2'] = $token2; setcookie('Ext-Direct-Token1', $token1, 0, '/', session_get_cookie_params()['domain']); $response->getBody()->write(sprintf('Ext.define(\'Ext.overrides.data.Connection\',{' . 'override:\'Ext.data.Connection\',request:function(o){o=Ext.apply(o||{},{' . 'withCredentials:true,cors:true,' . 'headers:{\'Ext-Direct-Token1\':\'%s\',\'Ext-Direct-Token2\':\'%s\'}});' . 'this.callParent([o]);}});', $token1, $token2)); $this->response = $response->withHeader('Content-Type', 'text/javascript')->withHeader('Set-Ext-Direct-Token1', $token1)->withHeader('Set-Ext-Direct-Token2', $token2); }