diff --git a/src/FileCacheFactory.php b/src/FileCacheFactory.php new file mode 100644 index 0000000..dd4e3ca --- /dev/null +++ b/src/FileCacheFactory.php @@ -0,0 +1,151 @@ +baseFactory = $factory; + + if (!strstr($cacheDirectory, '/') !== FALSE) { + if (realpath($cacheDirectory) == '') { + // It's not local, so default to the home directory. + $cacheDirectory = $_SERVER['HOME'] . '/' . $cacheDirectory; + } + else { + $cacheDirectory = realpath($cacheDirectory); + } + } + + if (!file_exists($cacheDirectory)) { + // Try to create it. + if (!mkdir($cacheDirectory)) { + throw new \Exception('Unable to create cache directory: ' . $cacheDirectory); + } + } + + if (!is_writable($cacheDirectory)) { + throw new \Exception('Cache directory is not writable: ' . $cacheDirectory); + } + + $this->cacheDirectory = $cacheDirectory; + } + + /** + * Caching request mechanism. Returns cached requests if we have them. + * @inheritdoc + */ + public function request($entity_type, $id) { + $cacheFilePath = $this->cacheDirectory . '/' . $entity_type . '/' . $id; + + if (file_exists($cacheFilePath)) { + $data = unserialize(file_get_contents($cacheFilePath)); + if (isset($data->list)) { + $data = reset($data->list); + } + $data = new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode($data))); + } + else { + // No data. + $data = $this->baseFactory->request($entity_type, $id); + + if (!file_exists($this->cacheDirectory . '/' . $entity_type)) { + mkdir($this->cacheDirectory . '/' . $entity_type); + } + else { + file_put_contents($cacheFilePath, serialize($data->json())); + } + } + + return $data; + } + + /** + * Caching request mechanism. Returns cached requests if we have them. + * @inheritdoc + */ + public function pagedRequest($entity_type, array $params, $page = 0) { + $paramKeys = array_keys($params); + $entityKey = $entity_type . '-' . implode('-', $paramKeys); + $paramIds = implode('-', array_values($params)) . '-' . $page; + $cacheFilePath = $this->cacheDirectory . '/' . $entityKey . '/' . $paramIds; + + if (file_exists($cacheFilePath)) { + $data = unserialize(file_get_contents($cacheFilePath)); + $data = new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode($data))); + } + else { + $data = $this->baseFactory->pagedRequest($entity_type, $params, $page); + + if (!file_exists($this->cacheDirectory . '/' . $entityKey)) { + mkdir($this->cacheDirectory . '/' . $entityKey); + } + else { + file_put_contents($cacheFilePath, serialize($data->json())); + } + } + + return $data; + } + + /** + * @inheritdoc + */ + public function getObjectTypeClass($type, array $data) { + return $this->baseFactory->getObjectTypeClass($type, $data); + } + + /** + * @inheritdoc + */ + public function createObjectType($type, array $data = array()) { + return $this->baseFactory->createObjectType($type, $data); + } + + /** + * Creates a list based on a response. + * + * @param $entityType + * @param $params + * @param array $data + * @param int $page + * @return object + */ + public function createList($entityType, $params, array $data = array(), $page = 0) { + $data['factory'] = $this; + return $this->baseFactory->createList($entityType, $params, $data, $page); + } +}