From cacb3977518cde88add4d77059ce94b17ec4472e Mon Sep 17 00:00:00 2001 From: Nick Vahalik Date: Mon, 27 Jul 2015 16:55:30 -0500 Subject: [PATCH 1/6] Updated for my fork. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 71e8383..702182f 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "eclipsegc/drupal-org-api", + "name": "nvahalik/drupal-org-api", "type": "library", "description": "Drupal.org API SDK for PHP.", "keywords": ["drupal", "drupal.org"], From 4e0fcd36a0f76d5293b80b316378538ba6e3a06e Mon Sep 17 00:00:00 2001 From: Nick Vahalik Date: Thu, 30 Jul 2015 13:23:12 -0500 Subject: [PATCH 2/6] PHP 5.5 really wants this to be public. --- src/Resources/GetResource.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Resources/GetResource.php b/src/Resources/GetResource.php index ba407ba..cb8d434 100644 --- a/src/Resources/GetResource.php +++ b/src/Resources/GetResource.php @@ -13,7 +13,7 @@ trait GetResource { */ protected $factory; - protected function getResource($resource_type, $id) { + public function getResource($resource_type, $id) { $response = $this->factory->request($resource_type, $id); return $this->factory->createObjectType($resource_type, $response->json()); } From 58b8edaf872188fc4d918ee3f603a37954cf6167 Mon Sep 17 00:00:00 2001 From: Nick Vahalik Date: Thu, 30 Jul 2015 13:23:33 -0500 Subject: [PATCH 3/6] Added file cache factory. --- src/FileCacheFactory.php | 113 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 src/FileCacheFactory.php diff --git a/src/FileCacheFactory.php b/src/FileCacheFactory.php new file mode 100644 index 0000000..a3b79de --- /dev/null +++ b/src/FileCacheFactory.php @@ -0,0 +1,113 @@ +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; + + echo $cacheFilePath . PHP_EOL; + + if (file_exists($cacheFilePath)) { + echo 'cache hit!' . PHP_EOL; + $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. + echo 'cache miss!' . PHP_EOL; + $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(json_decode($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); + } +} \ No newline at end of file From 5b8cca19f90f670ad23750282b5a430be4a64a0b Mon Sep 17 00:00:00 2001 From: Nick Vahalik Date: Wed, 14 Oct 2015 09:03:42 -0500 Subject: [PATCH 4/6] Adding paged resource caching. Cleanup. --- src/FileCacheFactory.php | 50 +++++++++++++++++++--- src/Resources/ResourceList.php | 76 ++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 6 deletions(-) create mode 100644 src/Resources/ResourceList.php diff --git a/src/FileCacheFactory.php b/src/FileCacheFactory.php index a3b79de..dd4e3ca 100644 --- a/src/FileCacheFactory.php +++ b/src/FileCacheFactory.php @@ -71,10 +71,7 @@ public function __construct(FactoryInterface $factory, $cacheDirectory = '.drupa public function request($entity_type, $id) { $cacheFilePath = $this->cacheDirectory . '/' . $entity_type . '/' . $id; - echo $cacheFilePath . PHP_EOL; - if (file_exists($cacheFilePath)) { - echo 'cache hit!' . PHP_EOL; $data = unserialize(file_get_contents($cacheFilePath)); if (isset($data->list)) { $data = reset($data->list); @@ -83,14 +80,41 @@ public function request($entity_type, $id) { } else { // No data. - echo 'cache miss!' . PHP_EOL; $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(json_decode($data->json()))); + 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())); } } @@ -110,4 +134,18 @@ public function getObjectTypeClass($type, array $data) { public function createObjectType($type, array $data = array()) { return $this->baseFactory->createObjectType($type, $data); } -} \ No newline at end of file + + /** + * 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); + } +} diff --git a/src/Resources/ResourceList.php b/src/Resources/ResourceList.php new file mode 100644 index 0000000..4c218ae --- /dev/null +++ b/src/Resources/ResourceList.php @@ -0,0 +1,76 @@ +params = $params; + $this->factory = $factory; + $this->type = $type; + $this->list = $list; + $this->loadedResults = count($this->list); + preg_match('#page=(\d+)#', $last, $matches); + $this->lastPage = $matches[1]; + $this->approxResults = $this->lastPage + 1 * 100; + $this->loadedPages = 0; + } + + public function getItems() { + $items = []; + foreach ($this->list as $listItem) { + $items[] = $this->factory->createObjectType($this->type, $listItem); + } + return $items; + } + + public function getTotalResults() { + if ($this->totalResults === NULL) { + $results = $this->factory->pagedRequest($this->type, $this->params, $this->lastPage); + $this->totalResults = $this->lastPage * 100 + count($results->json()['list']); + } + return $this->totalResults; + } + + public function loadAllObjects() { + for ($i = $this->loadedPages + 1; $i <= $this->lastPage; $i++) { + $results = $this->factory->pagedRequest($this->type, $this->params, $i); + $data = $results->json(); + $this->list = array_merge($this->list, $data['list']); + $this->loadedResults += count($data['list']); + } + } + + public function getAll() { + $this->loadAllObjects(); + $items = []; + foreach ($this->list as $listItem) { + $items[] = $this->factory->createObjectType($this->type, $listItem); + } + return $items; + } + + public function getSomeResources($number = 100) { + $items = []; + foreach (array_slice($this->list, 0, $number) as $listItem) { + $items[] = $this->factory->createObjectType($this->type, $listItem); + } + return $items; + } + + public function getLast() { + return $this->factory->createList($this->type, end($this->list)); + } +} \ No newline at end of file From 7ddfe60c12ad14a55ef3f3fa3ce26ee911521194 Mon Sep 17 00:00:00 2001 From: Nick Vahalik Date: Wed, 14 Oct 2015 10:11:56 -0500 Subject: [PATCH 5/6] Reverting unrelated commits. --- composer.json | 2 +- src/Resources/GetResource.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 702182f..71e8383 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "nvahalik/drupal-org-api", + "name": "eclipsegc/drupal-org-api", "type": "library", "description": "Drupal.org API SDK for PHP.", "keywords": ["drupal", "drupal.org"], diff --git a/src/Resources/GetResource.php b/src/Resources/GetResource.php index cb8d434..ba407ba 100644 --- a/src/Resources/GetResource.php +++ b/src/Resources/GetResource.php @@ -13,7 +13,7 @@ trait GetResource { */ protected $factory; - public function getResource($resource_type, $id) { + protected function getResource($resource_type, $id) { $response = $this->factory->request($resource_type, $id); return $this->factory->createObjectType($resource_type, $response->json()); } From c3799f0503e62e9dbfc5e5f83e9e97589a8512f0 Mon Sep 17 00:00:00 2001 From: Nick Vahalik Date: Wed, 14 Oct 2015 10:13:42 -0500 Subject: [PATCH 6/6] Removing resource list. --- src/Resources/ResourceList.php | 76 ---------------------------------- 1 file changed, 76 deletions(-) delete mode 100644 src/Resources/ResourceList.php diff --git a/src/Resources/ResourceList.php b/src/Resources/ResourceList.php deleted file mode 100644 index 4c218ae..0000000 --- a/src/Resources/ResourceList.php +++ /dev/null @@ -1,76 +0,0 @@ -params = $params; - $this->factory = $factory; - $this->type = $type; - $this->list = $list; - $this->loadedResults = count($this->list); - preg_match('#page=(\d+)#', $last, $matches); - $this->lastPage = $matches[1]; - $this->approxResults = $this->lastPage + 1 * 100; - $this->loadedPages = 0; - } - - public function getItems() { - $items = []; - foreach ($this->list as $listItem) { - $items[] = $this->factory->createObjectType($this->type, $listItem); - } - return $items; - } - - public function getTotalResults() { - if ($this->totalResults === NULL) { - $results = $this->factory->pagedRequest($this->type, $this->params, $this->lastPage); - $this->totalResults = $this->lastPage * 100 + count($results->json()['list']); - } - return $this->totalResults; - } - - public function loadAllObjects() { - for ($i = $this->loadedPages + 1; $i <= $this->lastPage; $i++) { - $results = $this->factory->pagedRequest($this->type, $this->params, $i); - $data = $results->json(); - $this->list = array_merge($this->list, $data['list']); - $this->loadedResults += count($data['list']); - } - } - - public function getAll() { - $this->loadAllObjects(); - $items = []; - foreach ($this->list as $listItem) { - $items[] = $this->factory->createObjectType($this->type, $listItem); - } - return $items; - } - - public function getSomeResources($number = 100) { - $items = []; - foreach (array_slice($this->list, 0, $number) as $listItem) { - $items[] = $this->factory->createObjectType($this->type, $listItem); - } - return $items; - } - - public function getLast() { - return $this->factory->createList($this->type, end($this->list)); - } -} \ No newline at end of file