From cacb3977518cde88add4d77059ce94b17ec4472e Mon Sep 17 00:00:00 2001 From: Nick Vahalik Date: Mon, 27 Jul 2015 16:55:30 -0500 Subject: [PATCH 1/9] 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/9] 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/9] 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 45f2371cc784356846b00c1e22e41b9c631a0ef1 Mon Sep 17 00:00:00 2001 From: Nick Vahalik Date: Thu, 30 Jul 2015 13:23:45 -0500 Subject: [PATCH 4/9] Added Taxonomy Term resource. --- src/DrupalClient.php | 7 ++ src/Factory.php | 1 + src/Resources/TaxonomyTerm.php | 142 +++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+) create mode 100644 src/Resources/TaxonomyTerm.php diff --git a/src/DrupalClient.php b/src/DrupalClient.php index 27cccb3..7523869 100644 --- a/src/DrupalClient.php +++ b/src/DrupalClient.php @@ -56,4 +56,11 @@ public function getNode($id) { return $this->getResource('node', $id); } + /** + * @param $id + * @return \EclipseGc\DrupalOrg\Api\Resources\TaxonomyTerm + */ + public function getTaxonomyTerm($id) { + return $this->getResource('taxonomy_term', $id); + } } \ No newline at end of file diff --git a/src/Factory.php b/src/Factory.php index ed2a16e..f165a23 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -13,6 +13,7 @@ class Factory implements FactoryInterface { protected $objectTypes = array( 'user' => '\EclipseGc\DrupalOrg\Api\Resources\User', 'node' => '\EclipseGc\DrupalOrg\Api\Resources\Node', + 'taxonomy_term' => '\EclipseGc\DrupalOrg\Api\Resources\TaxonomyTerm', ); function __construct(Client $client) { diff --git a/src/Resources/TaxonomyTerm.php b/src/Resources/TaxonomyTerm.php new file mode 100644 index 0000000..b91fd15 --- /dev/null +++ b/src/Resources/TaxonomyTerm.php @@ -0,0 +1,142 @@ +factory = $factory; + $this->tid = $tid; + $this->name = $name; + $this->description = $description; + $this->weight = $weight; + $this->node_count = $node_count; + $this->url = $url; + $this->parent = $parent; + $this->parents_all = $parents_all; + } + + /** + * @return mixed + */ + public function getTid() { + return $this->tid; + } + + /** + * @return mixed + */ + public function getName() { + return $this->name; + } + + /** + * @return mixed + */ + public function getDescription() { + return $this->description; + } + + /** + * @return mixed + */ + public function getWeight() { + return $this->weight; + } + + /** + * @return mixed + */ + public function getNodeCount() { + return $this->node_count; + } + + /** + * @return mixed + */ + public function getUrl() { + return $this->url; + } + + /** + * @return mixed + */ + public function getParent() { + return $this->getResource($this->parent['resource'], $this->parent['id']); + } + + /** + * @return mixed + */ + public function getParentsAll() { + $parents_all = []; + foreach ($this->parents_all as $id => $parent) { + $parents_all[$id] = $this->getResource($parent['resource'], $parent['id']); + }; + return $parents_all; + } +} \ No newline at end of file From c4c2577b0b55df3e622e5bded4e086ca811a1873 Mon Sep 17 00:00:00 2001 From: Nick Vahalik Date: Fri, 31 Jul 2015 12:00:39 -0500 Subject: [PATCH 5/9] Updated ProjectIssue class with changes in the schema from the API. Added ProjectModule Node subclass. Added ProjectRelease Node subclass. Updated TaxonomyTerm. Not all taxonomy terms are built the same. E.g. some have no description because they are very basic (e.g. Drupal core version attached to a ProjectRelease.) --- src/Resources/Node/ProjectIssue.php | 7 +- src/Resources/Node/ProjectModule.php | 373 ++++++++++++++++++++++++++ src/Resources/Node/ProjectRelease.php | 237 ++++++++++++++++ src/Resources/TaxonomyTerm.php | 6 +- 4 files changed, 618 insertions(+), 5 deletions(-) create mode 100644 src/Resources/Node/ProjectModule.php create mode 100644 src/Resources/Node/ProjectRelease.php diff --git a/src/Resources/Node/ProjectIssue.php b/src/Resources/Node/ProjectIssue.php index 422deca..3d02980 100644 --- a/src/Resources/Node/ProjectIssue.php +++ b/src/Resources/Node/ProjectIssue.php @@ -28,6 +28,8 @@ class ProjectIssue extends Node { protected $project; + protected $issueCredit; + protected $issueFiles; protected $issueParent; @@ -36,7 +38,7 @@ class ProjectIssue extends Node { protected $issueVersion; - public function __construct(FactoryInterface $factory, $body, $taxonomy_vocabulary_9, $field_issue_status, $field_issue_priority, $field_issue_category, $field_issue_component, $field_issue_assigned, $field_project, $field_issue_files, $field_issue_parent, $field_issue_related, $field_issue_version, $nid, $vid, $is_new, $type, $title, $language, $url, $edit_url, $status, $promote, $sticky, $created, $changed, $author, $book_ancestors, $comment, $comments, $comment_count, $comment_count_new, $has_new_content, $last_comment_timestamp) { + public function __construct(FactoryInterface $factory, $body, $taxonomy_vocabulary_9, $field_issue_status, $field_issue_priority, $field_issue_category, $field_issue_component, $field_project, $field_issue_files, $field_issue_related, $field_issue_version, $field_issue_credit, $nid, $vid, $is_new, $type, $title, $language, $url, $edit_url, $status, $promote, $sticky, $created, $changed, $author, $book_ancestors, $comment, $comments, $comment_count, $comment_count_new, $has_new_content, $last_comment_timestamp = 0, $field_issue_assigned = '') { $this->taxonomy = $taxonomy_vocabulary_9; $this->issueStatus = $field_issue_status; $this->issuePriority = $field_issue_priority; @@ -45,8 +47,9 @@ public function __construct(FactoryInterface $factory, $body, $taxonomy_vocabula $this->issueAssigned = $field_issue_assigned; $this->project = $field_project; $this->issueFiles = $field_issue_files; - $this->issueParent = $field_issue_parent; +// $this->issueParent = $field_issue_parent; $this->issueRelated = $field_issue_related; + $this->issueCredit = $field_issue_credit; $this->issueVersion = $field_issue_version; $this->bookAncestors = $book_ancestors; parent::__construct($factory, $body, $nid, $vid, $is_new, $type, $title, $language, $url, $edit_url, $status, $promote, $sticky, $created, $changed, $author, $has_new_content); diff --git a/src/Resources/Node/ProjectModule.php b/src/Resources/Node/ProjectModule.php new file mode 100644 index 0000000..114916b --- /dev/null +++ b/src/Resources/Node/ProjectModule.php @@ -0,0 +1,373 @@ +developmentStatus = $taxonomy_vocabulary_46; + $this->maintenanceStatus = $taxonomy_vocabulary_44; + $this->tags = $taxonomy_vocabulary_3; + $this->projectType = $field_project_type; + $this->machineName = $field_project_machine_name; + $this->hasIssueQueue = $field_project_has_issue_queue; + $this->components = $field_project_components; + $this->defaultComponent = $field_project_default_component; + $this->projectIssueGuidelines = $field_project_issue_guidelines; + $this->hasReleases = $field_project_has_releases; + $this->releaseVersionFormat = $field_release_version_format; + $this->hompage = $field_project_homepage; + $this->changelog = $field_project_changelog; + $this->demo = $field_project_demo; + $this->documentation = $field_project_documentation; + $this->screenshots = $field_project_screenshots; + $this->license = $field_project_license; + $this->images = $field_project_images; + $this->supportingOrganizations = $field_supporting_organizations; + $this->downloadCount = $field_download_count; + $this->phpCsErrors = $field_project_phpcs_errors; + $this->phpCsFull = $field_project_phpcs_full; + $this->phpCsTimestamp = $field_project_phpcs_ts; + $this->phpCsWarnings = $field_project_phpcs_warnings; + + parent::__construct($factory, $body, $nid, $vid, $is_new, $type, $title, $language, $url, $edit_url, $status, $promote, $sticky, $created, $changed, $author, $has_new_content); + $this->setCommentData($comment, $comments, $comment_count, $comment_count_new, $last_comment_timestamp); + } + + /** + * @return mixed + */ + public function getMaintenanceStatus() { + return $this->maintenanceStatus; + } + + /** + * @return mixed + */ + public function getDevelopmentStatus() { + return $this->developmentStatus; + } + + /** + * @return mixed + */ + public function getTags() { + $items = []; + foreach ($this->tags as $listItem) { + $items[] = $this->getResource($listItem['resource'], $listItem['id']); + } + return $items; + } + + /** + * @return mixed + */ + public function getProjectType() { + return $this->projectType; + } + + /** + * @return mixed + */ + public function getMachineName() { + return $this->machineName; + } + + /** + * @return mixed + */ + public function getHasIssueQueue() { + return $this->hasIssueQueue; + } + + /** + * @return mixed + */ + public function getComponents() { + return $this->components; + } + + /** + * @return mixed + */ + public function getDefaultComponent() { + return $this->defaultComponent; + } + + /** + * @return mixed + */ + public function getProjectIssueGuidelines() { + return $this->projectIssueGuidelines; + } + + /** + * @return mixed + */ + public function getHasReleases() { + return $this->hasReleases; + } + + /** + * @return mixed + */ + public function getReleaseVersionFormat() { + return $this->releaseVersionFormat; + } + + /** + * @return mixed + */ + public function getHomepage() { + return $this->homepage; + } + + /** + * @return mixed + */ + public function getChangelog() { + return $this->changelog; + } + + /** + * @return mixed + */ + public function getDemo() { + return $this->demo; + } + + /** + * @return mixed + */ + public function getDocumentation() { + return $this->documentation; + } + + /** + * @return mixed + */ + public function getScreenshots() { + return $this->screenshots; + } + + /** + * @return mixed + */ + public function getLicense() { + return $this->license; + } + + /** + * @return mixed + */ + public function getImages() { + return $this->images; + } + + /** + * @return mixed + */ + public function getSupportingOrganizations() { + return $this->supportingOrganizations; + } + + /** + * @return mixed + */ + public function getDownloadCount() { + return $this->downloadCount; + } + + /** + * @return mixed + */ + public function getPhpCsErrors() { + return $this->phpCsErrors; + } + + /** + * @return mixed + */ + public function getPhpCsFull() { + return $this->phpCsFull; + } + + /** + * @return mixed + */ + public function getPhpCsTimestamp() { + return $this->phpCsTimestamp; + } + + /** + * @return mixed + */ + public function getPhpCsWarnings() { + return $this->phpCsWarnings; + } + +} \ No newline at end of file diff --git a/src/Resources/Node/ProjectRelease.php b/src/Resources/Node/ProjectRelease.php new file mode 100644 index 0000000..540d2fd --- /dev/null +++ b/src/Resources/Node/ProjectRelease.php @@ -0,0 +1,237 @@ +drupalCoreVersion = $taxonomy_vocabulary_6; + $this->tags = $taxonomy_vocabulary_7; + $this->project = $field_release_project; + $this->version = $field_release_version; + $this->versionMajor = $field_release_version_major; + $this->versionMinor = $field_release_version_minor; + $this->versionPatch = $field_release_version_patch; + $this->versionExtra = $field_release_version_extra; + $this->weight = $field_release_version_ext_weight; + $this->delta = $field_release_version_ext_delta; + $this->vcsLabel = $field_release_vcs_label; + $this->buildType = $field_release_build_type; + $this->updateStatus = $field_release_update_status; + $this->releaseFiles = $field_release_files; + + $this->bookAncestors = $book_ancestors; + + parent::__construct($factory, $body, $nid, $vid, $is_new, $type, $title, $language, $url, $edit_url, $status, $promote, $sticky, $created, $changed, $author, $has_new_content); + $this->setCommentData($comment, $comments, $comment_count, $comment_count_new, $last_comment_timestamp); + } + + /** + * @return int + */ + public function getDrupalCoreVersion() { + return $this->factory->createObjectType($this->drupalCoreVersion['type'], $this->drupalCoreVersion['id']); + } + + /** + * @return int + */ + public function getTags() { + $items = []; + foreach ($this->tags as $listItem) { + $items[] = $this->factory->createObjectType($listItem['type'], $listItem['id']); + } + return $items; + } + + /** + * @return boolean + */ + public function getProject() { + return $this->factory->createObjectType($this->project['type'], $this->project['id']); + } + + /** + * @return string + */ + public function getVersion() { + return $this->version; + } + + /** + * @return string + */ + public function getVersionMajor() { + return $this->versionMajor; + } + + /** + * @return string + */ + public function getVersionMinor() { + return $this->versionMinor; + } + + /** + * @return string + */ + public function getVersionPatch() { + return $this->versionPatch; + } + + /** + * @return string + */ + public function getVersionExtra() { + return $this->versionExtra; + } + + /** + * @return mixed + */ + public function getWeight() { + return $this->weight; + } + + /** + * @return mixed + */ + public function getDelta() { + return $this->delta; + } + + /** + * @return mixed + */ + public function getVcsLabel() { + return $this->vcsLabel; + } + + /** + * @return mixed + */ + public function getBuildType() { + return $this->buildType; + } + + /** + * @return mixed + */ + public function getUpdateStatus() { + return $this->updateStatus; + } + + /** + * @return mixed + */ + public function getReleaseFiles() { + return $this->releaseFiles; + } +} \ No newline at end of file diff --git a/src/Resources/TaxonomyTerm.php b/src/Resources/TaxonomyTerm.php index b91fd15..19122f1 100644 --- a/src/Resources/TaxonomyTerm.php +++ b/src/Resources/TaxonomyTerm.php @@ -68,7 +68,7 @@ public static function getClass(array $data) { * @param $parent * @param $parents_all */ - public function __construct(FactoryInterface $factory, $tid, $name, $description, $weight, $node_count, $url, $parent, $parents_all) { + public function __construct(FactoryInterface $factory, $tid, $name, $node_count = 0, $url = '', $parent = array(), $parents_all = array(), $weight = 0, $description = '') { $this->factory = $factory; $this->tid = $tid; $this->name = $name; @@ -123,14 +123,14 @@ public function getUrl() { } /** - * @return mixed + * @return TaxonomyTerm */ public function getParent() { return $this->getResource($this->parent['resource'], $this->parent['id']); } /** - * @return mixed + * @return array */ public function getParentsAll() { $parents_all = []; From 97d3e0f7b37316d50139282d191ee640f184b3e0 Mon Sep 17 00:00:00 2001 From: Nick Vahalik Date: Fri, 31 Jul 2015 12:01:04 -0500 Subject: [PATCH 6/9] Adding support for paged resources. --- src/Factory.php | 38 +++++++++++++++++ src/FactoryInterface.php | 16 +++++++ src/FileCacheFactory.php | 49 +++++++++++++++++++++- src/Resources/GetResource.php | 4 ++ src/Resources/PagedInterface.php | 14 +++++++ src/Resources/ResourceList.php | 72 ++++++++++++++++++++++++++++++++ 6 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 src/Resources/PagedInterface.php create mode 100644 src/Resources/ResourceList.php diff --git a/src/Factory.php b/src/Factory.php index f165a23..8be66aa 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -7,6 +7,7 @@ namespace EclipseGc\DrupalOrg\Api; use GuzzleHttp\Client; +use GuzzleHttp\Message\Request; class Factory implements FactoryInterface { @@ -28,6 +29,23 @@ public function getObjectTypeClass($type, array $data) { throw new \Exception(sprintf('No object type %s', $type)); } + public function createList($type, $params, array $data = array()) { + $reflector = new \ReflectionClass('\EclipseGc\DrupalOrg\Api\Resources\ResourceList'); + $data['type'] = $type; + $data['params'] = $params; + if (!isset($data['factory'])) { + $data['factory'] = $this; + } + $arguments = []; + foreach ($reflector->getMethod('__construct')->getParameters() as $param) { + $param_name = $param->getName(); + if (array_key_exists($param_name, $data)) { + $arguments[] = $data[$param_name]; + } + } + return $reflector->newInstanceArgs($arguments); + } + public function createObjectType($type, array $data = array()) { $objectClass = $this->getObjectTypeClass($type, $data); $reflector = new \ReflectionClass($objectClass); @@ -58,6 +76,26 @@ public function request($entity_type, $id) { return $request; } + public function pagedRequest($entity_type, array $params, $page = 0) { + if (!isset($params['page'])) { + $params['page'] = $page; + } + // You have to add the .json or it doesn't work? + $request = $this->client()->createRequest('GET', $entity_type . '.json'); + // Add our request params. + $request->getQuery()->merge($params); + + var_dump($request->getUrl()); + + $request = $this->client()->send($request); + + if ($request->getStatusCode() != 200) { + throw new \Exception(sprintf('Status code was not OK. %d returned instead.', $request->getStatusCode())); + } + return $request; + + } + /** * Statically stores and returns a guzzle client. * diff --git a/src/FactoryInterface.php b/src/FactoryInterface.php index d0bcb39..cce0f41 100644 --- a/src/FactoryInterface.php +++ b/src/FactoryInterface.php @@ -32,6 +32,20 @@ public function getObjectTypeClass($type, array $data); */ public function request($entity_type, $id); + + /** + * Makes a paged request. + * + * @param $entity_type + * The type of entity. + * @param $params + * Search parameters. + * @param $page + * The integer of the page to load. + * @return \GuzzleHttp\Message\ResponseInterface + */ + public function pagedRequest($entity_type, array $params, $page = 0); + /** * Results in a full object for the provided data. * @@ -43,4 +57,6 @@ public function request($entity_type, $id); * @return \EclipseGc\DrupalOrg\Api\Resources\ResourceInterface */ public function createObjectType($type, array $data = array()); + + public function createList($type, $params, array $data = array()); } \ No newline at end of file diff --git a/src/FileCacheFactory.php b/src/FileCacheFactory.php index a3b79de..f1807a9 100644 --- a/src/FileCacheFactory.php +++ b/src/FileCacheFactory.php @@ -90,13 +90,46 @@ public function request($entity_type, $id) { 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; + + echo $cacheFilePath . PHP_EOL; + + if (file_exists($cacheFilePath)) { + echo 'cache hit!' . PHP_EOL; + $data = unserialize(file_get_contents($cacheFilePath)); + $data = new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode($data))); + } + else { + echo 'cache miss!' . PHP_EOL; + $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 */ @@ -110,4 +143,18 @@ public function getObjectTypeClass($type, array $data) { 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); + } } \ No newline at end of file diff --git a/src/Resources/GetResource.php b/src/Resources/GetResource.php index cb8d434..5128a9f 100644 --- a/src/Resources/GetResource.php +++ b/src/Resources/GetResource.php @@ -18,4 +18,8 @@ public function getResource($resource_type, $id) { return $this->factory->createObjectType($resource_type, $response->json()); } + public function getPagedResource($resource_type, array $params) { + $response = $this->factory->pagedRequest($resource_type, $params); + return $this->factory->createList($resource_type, $params, $response->json()); + } } diff --git a/src/Resources/PagedInterface.php b/src/Resources/PagedInterface.php new file mode 100644 index 0000000..ea6b416 --- /dev/null +++ b/src/Resources/PagedInterface.php @@ -0,0 +1,14 @@ +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; + } +} \ No newline at end of file From feb8db3cdb3f36929ba853c7d3b38590f54915ae Mon Sep 17 00:00:00 2001 From: Nick Vahalik Date: Wed, 14 Oct 2015 10:18:10 -0500 Subject: [PATCH 7/9] Removing other commits. --- composer.json | 2 +- src/DrupalClient.php | 7 -- src/Factory.php | 1 - src/FileCacheFactory.php | 160 --------------------------------- src/Resources/GetResource.php | 2 +- src/Resources/TaxonomyTerm.php | 142 ----------------------------- 6 files changed, 2 insertions(+), 312 deletions(-) delete mode 100644 src/FileCacheFactory.php delete mode 100644 src/Resources/TaxonomyTerm.php 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/DrupalClient.php b/src/DrupalClient.php index 7523869..27cccb3 100644 --- a/src/DrupalClient.php +++ b/src/DrupalClient.php @@ -56,11 +56,4 @@ public function getNode($id) { return $this->getResource('node', $id); } - /** - * @param $id - * @return \EclipseGc\DrupalOrg\Api\Resources\TaxonomyTerm - */ - public function getTaxonomyTerm($id) { - return $this->getResource('taxonomy_term', $id); - } } \ No newline at end of file diff --git a/src/Factory.php b/src/Factory.php index 8be66aa..9718a8e 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -14,7 +14,6 @@ class Factory implements FactoryInterface { protected $objectTypes = array( 'user' => '\EclipseGc\DrupalOrg\Api\Resources\User', 'node' => '\EclipseGc\DrupalOrg\Api\Resources\Node', - 'taxonomy_term' => '\EclipseGc\DrupalOrg\Api\Resources\TaxonomyTerm', ); function __construct(Client $client) { diff --git a/src/FileCacheFactory.php b/src/FileCacheFactory.php deleted file mode 100644 index f1807a9..0000000 --- a/src/FileCacheFactory.php +++ /dev/null @@ -1,160 +0,0 @@ -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($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; - - echo $cacheFilePath . PHP_EOL; - - if (file_exists($cacheFilePath)) { - echo 'cache hit!' . PHP_EOL; - $data = unserialize(file_get_contents($cacheFilePath)); - $data = new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode($data))); - } - else { - echo 'cache miss!' . PHP_EOL; - $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); - } -} \ No newline at end of file diff --git a/src/Resources/GetResource.php b/src/Resources/GetResource.php index 5128a9f..10b34e8 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()); } diff --git a/src/Resources/TaxonomyTerm.php b/src/Resources/TaxonomyTerm.php deleted file mode 100644 index 19122f1..0000000 --- a/src/Resources/TaxonomyTerm.php +++ /dev/null @@ -1,142 +0,0 @@ -factory = $factory; - $this->tid = $tid; - $this->name = $name; - $this->description = $description; - $this->weight = $weight; - $this->node_count = $node_count; - $this->url = $url; - $this->parent = $parent; - $this->parents_all = $parents_all; - } - - /** - * @return mixed - */ - public function getTid() { - return $this->tid; - } - - /** - * @return mixed - */ - public function getName() { - return $this->name; - } - - /** - * @return mixed - */ - public function getDescription() { - return $this->description; - } - - /** - * @return mixed - */ - public function getWeight() { - return $this->weight; - } - - /** - * @return mixed - */ - public function getNodeCount() { - return $this->node_count; - } - - /** - * @return mixed - */ - public function getUrl() { - return $this->url; - } - - /** - * @return TaxonomyTerm - */ - public function getParent() { - return $this->getResource($this->parent['resource'], $this->parent['id']); - } - - /** - * @return array - */ - public function getParentsAll() { - $parents_all = []; - foreach ($this->parents_all as $id => $parent) { - $parents_all[$id] = $this->getResource($parent['resource'], $parent['id']); - }; - return $parents_all; - } -} \ No newline at end of file From 11e0a703123bb1dfd58dcc14761dbff97aad0fe4 Mon Sep 17 00:00:00 2001 From: Nick Vahalik Date: Wed, 14 Oct 2015 10:20:01 -0500 Subject: [PATCH 8/9] Adding newlines. --- src/Resources/PagedInterface.php | 2 +- src/Resources/ResourceList.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Resources/PagedInterface.php b/src/Resources/PagedInterface.php index ea6b416..95ba20b 100644 --- a/src/Resources/PagedInterface.php +++ b/src/Resources/PagedInterface.php @@ -11,4 +11,4 @@ public function __construct(FactoryInterface $factoryInterface, $type, $params, public function getAll(); public function getSomeResources($number = 100); -} \ No newline at end of file +} diff --git a/src/Resources/ResourceList.php b/src/Resources/ResourceList.php index 3b853b8..609bba5 100644 --- a/src/Resources/ResourceList.php +++ b/src/Resources/ResourceList.php @@ -69,4 +69,4 @@ public function getSomeResources($number = 100) { } return $items; } -} \ No newline at end of file +} From 59c62d1230f39e03e47b9a7dc1b3bb80154a6f87 Mon Sep 17 00:00:00 2001 From: Nick Vahalik Date: Wed, 14 Oct 2015 10:21:41 -0500 Subject: [PATCH 9/9] Removing other commits. --- src/Resources/Node/ProjectIssue.php | 7 +- src/Resources/Node/ProjectModule.php | 373 -------------------------- src/Resources/Node/ProjectRelease.php | 237 ---------------- 3 files changed, 2 insertions(+), 615 deletions(-) delete mode 100644 src/Resources/Node/ProjectModule.php delete mode 100644 src/Resources/Node/ProjectRelease.php diff --git a/src/Resources/Node/ProjectIssue.php b/src/Resources/Node/ProjectIssue.php index 3d02980..422deca 100644 --- a/src/Resources/Node/ProjectIssue.php +++ b/src/Resources/Node/ProjectIssue.php @@ -28,8 +28,6 @@ class ProjectIssue extends Node { protected $project; - protected $issueCredit; - protected $issueFiles; protected $issueParent; @@ -38,7 +36,7 @@ class ProjectIssue extends Node { protected $issueVersion; - public function __construct(FactoryInterface $factory, $body, $taxonomy_vocabulary_9, $field_issue_status, $field_issue_priority, $field_issue_category, $field_issue_component, $field_project, $field_issue_files, $field_issue_related, $field_issue_version, $field_issue_credit, $nid, $vid, $is_new, $type, $title, $language, $url, $edit_url, $status, $promote, $sticky, $created, $changed, $author, $book_ancestors, $comment, $comments, $comment_count, $comment_count_new, $has_new_content, $last_comment_timestamp = 0, $field_issue_assigned = '') { + public function __construct(FactoryInterface $factory, $body, $taxonomy_vocabulary_9, $field_issue_status, $field_issue_priority, $field_issue_category, $field_issue_component, $field_issue_assigned, $field_project, $field_issue_files, $field_issue_parent, $field_issue_related, $field_issue_version, $nid, $vid, $is_new, $type, $title, $language, $url, $edit_url, $status, $promote, $sticky, $created, $changed, $author, $book_ancestors, $comment, $comments, $comment_count, $comment_count_new, $has_new_content, $last_comment_timestamp) { $this->taxonomy = $taxonomy_vocabulary_9; $this->issueStatus = $field_issue_status; $this->issuePriority = $field_issue_priority; @@ -47,9 +45,8 @@ public function __construct(FactoryInterface $factory, $body, $taxonomy_vocabula $this->issueAssigned = $field_issue_assigned; $this->project = $field_project; $this->issueFiles = $field_issue_files; -// $this->issueParent = $field_issue_parent; + $this->issueParent = $field_issue_parent; $this->issueRelated = $field_issue_related; - $this->issueCredit = $field_issue_credit; $this->issueVersion = $field_issue_version; $this->bookAncestors = $book_ancestors; parent::__construct($factory, $body, $nid, $vid, $is_new, $type, $title, $language, $url, $edit_url, $status, $promote, $sticky, $created, $changed, $author, $has_new_content); diff --git a/src/Resources/Node/ProjectModule.php b/src/Resources/Node/ProjectModule.php deleted file mode 100644 index 114916b..0000000 --- a/src/Resources/Node/ProjectModule.php +++ /dev/null @@ -1,373 +0,0 @@ -developmentStatus = $taxonomy_vocabulary_46; - $this->maintenanceStatus = $taxonomy_vocabulary_44; - $this->tags = $taxonomy_vocabulary_3; - $this->projectType = $field_project_type; - $this->machineName = $field_project_machine_name; - $this->hasIssueQueue = $field_project_has_issue_queue; - $this->components = $field_project_components; - $this->defaultComponent = $field_project_default_component; - $this->projectIssueGuidelines = $field_project_issue_guidelines; - $this->hasReleases = $field_project_has_releases; - $this->releaseVersionFormat = $field_release_version_format; - $this->hompage = $field_project_homepage; - $this->changelog = $field_project_changelog; - $this->demo = $field_project_demo; - $this->documentation = $field_project_documentation; - $this->screenshots = $field_project_screenshots; - $this->license = $field_project_license; - $this->images = $field_project_images; - $this->supportingOrganizations = $field_supporting_organizations; - $this->downloadCount = $field_download_count; - $this->phpCsErrors = $field_project_phpcs_errors; - $this->phpCsFull = $field_project_phpcs_full; - $this->phpCsTimestamp = $field_project_phpcs_ts; - $this->phpCsWarnings = $field_project_phpcs_warnings; - - parent::__construct($factory, $body, $nid, $vid, $is_new, $type, $title, $language, $url, $edit_url, $status, $promote, $sticky, $created, $changed, $author, $has_new_content); - $this->setCommentData($comment, $comments, $comment_count, $comment_count_new, $last_comment_timestamp); - } - - /** - * @return mixed - */ - public function getMaintenanceStatus() { - return $this->maintenanceStatus; - } - - /** - * @return mixed - */ - public function getDevelopmentStatus() { - return $this->developmentStatus; - } - - /** - * @return mixed - */ - public function getTags() { - $items = []; - foreach ($this->tags as $listItem) { - $items[] = $this->getResource($listItem['resource'], $listItem['id']); - } - return $items; - } - - /** - * @return mixed - */ - public function getProjectType() { - return $this->projectType; - } - - /** - * @return mixed - */ - public function getMachineName() { - return $this->machineName; - } - - /** - * @return mixed - */ - public function getHasIssueQueue() { - return $this->hasIssueQueue; - } - - /** - * @return mixed - */ - public function getComponents() { - return $this->components; - } - - /** - * @return mixed - */ - public function getDefaultComponent() { - return $this->defaultComponent; - } - - /** - * @return mixed - */ - public function getProjectIssueGuidelines() { - return $this->projectIssueGuidelines; - } - - /** - * @return mixed - */ - public function getHasReleases() { - return $this->hasReleases; - } - - /** - * @return mixed - */ - public function getReleaseVersionFormat() { - return $this->releaseVersionFormat; - } - - /** - * @return mixed - */ - public function getHomepage() { - return $this->homepage; - } - - /** - * @return mixed - */ - public function getChangelog() { - return $this->changelog; - } - - /** - * @return mixed - */ - public function getDemo() { - return $this->demo; - } - - /** - * @return mixed - */ - public function getDocumentation() { - return $this->documentation; - } - - /** - * @return mixed - */ - public function getScreenshots() { - return $this->screenshots; - } - - /** - * @return mixed - */ - public function getLicense() { - return $this->license; - } - - /** - * @return mixed - */ - public function getImages() { - return $this->images; - } - - /** - * @return mixed - */ - public function getSupportingOrganizations() { - return $this->supportingOrganizations; - } - - /** - * @return mixed - */ - public function getDownloadCount() { - return $this->downloadCount; - } - - /** - * @return mixed - */ - public function getPhpCsErrors() { - return $this->phpCsErrors; - } - - /** - * @return mixed - */ - public function getPhpCsFull() { - return $this->phpCsFull; - } - - /** - * @return mixed - */ - public function getPhpCsTimestamp() { - return $this->phpCsTimestamp; - } - - /** - * @return mixed - */ - public function getPhpCsWarnings() { - return $this->phpCsWarnings; - } - -} \ No newline at end of file diff --git a/src/Resources/Node/ProjectRelease.php b/src/Resources/Node/ProjectRelease.php deleted file mode 100644 index 540d2fd..0000000 --- a/src/Resources/Node/ProjectRelease.php +++ /dev/null @@ -1,237 +0,0 @@ -drupalCoreVersion = $taxonomy_vocabulary_6; - $this->tags = $taxonomy_vocabulary_7; - $this->project = $field_release_project; - $this->version = $field_release_version; - $this->versionMajor = $field_release_version_major; - $this->versionMinor = $field_release_version_minor; - $this->versionPatch = $field_release_version_patch; - $this->versionExtra = $field_release_version_extra; - $this->weight = $field_release_version_ext_weight; - $this->delta = $field_release_version_ext_delta; - $this->vcsLabel = $field_release_vcs_label; - $this->buildType = $field_release_build_type; - $this->updateStatus = $field_release_update_status; - $this->releaseFiles = $field_release_files; - - $this->bookAncestors = $book_ancestors; - - parent::__construct($factory, $body, $nid, $vid, $is_new, $type, $title, $language, $url, $edit_url, $status, $promote, $sticky, $created, $changed, $author, $has_new_content); - $this->setCommentData($comment, $comments, $comment_count, $comment_count_new, $last_comment_timestamp); - } - - /** - * @return int - */ - public function getDrupalCoreVersion() { - return $this->factory->createObjectType($this->drupalCoreVersion['type'], $this->drupalCoreVersion['id']); - } - - /** - * @return int - */ - public function getTags() { - $items = []; - foreach ($this->tags as $listItem) { - $items[] = $this->factory->createObjectType($listItem['type'], $listItem['id']); - } - return $items; - } - - /** - * @return boolean - */ - public function getProject() { - return $this->factory->createObjectType($this->project['type'], $this->project['id']); - } - - /** - * @return string - */ - public function getVersion() { - return $this->version; - } - - /** - * @return string - */ - public function getVersionMajor() { - return $this->versionMajor; - } - - /** - * @return string - */ - public function getVersionMinor() { - return $this->versionMinor; - } - - /** - * @return string - */ - public function getVersionPatch() { - return $this->versionPatch; - } - - /** - * @return string - */ - public function getVersionExtra() { - return $this->versionExtra; - } - - /** - * @return mixed - */ - public function getWeight() { - return $this->weight; - } - - /** - * @return mixed - */ - public function getDelta() { - return $this->delta; - } - - /** - * @return mixed - */ - public function getVcsLabel() { - return $this->vcsLabel; - } - - /** - * @return mixed - */ - public function getBuildType() { - return $this->buildType; - } - - /** - * @return mixed - */ - public function getUpdateStatus() { - return $this->updateStatus; - } - - /** - * @return mixed - */ - public function getReleaseFiles() { - return $this->releaseFiles; - } -} \ No newline at end of file