From 1de7885c789013952c9613cbf256760c7c66f6dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20NAGY?= Date: Tue, 6 Nov 2018 13:20:42 +0100 Subject: [PATCH 1/4] Method should be compatible with the parent method. --- Apigee/Mint/BankDetail.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Apigee/Mint/BankDetail.php b/Apigee/Mint/BankDetail.php index 6d053e8..4cd82fb 100644 --- a/Apigee/Mint/BankDetail.php +++ b/Apigee/Mint/BankDetail.php @@ -145,7 +145,7 @@ public function loadFromRawData($data, $reset = false) } } - public function save($save_method) + public function save($save_method = 'auto') { if ($this->id == null) { $this->post(null, $this->__toString()); From b3b1f1be39c57249be0258b61cec76e6e15a8458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20NAGY?= Date: Tue, 6 Nov 2018 13:22:20 +0100 Subject: [PATCH 2/4] Implement __wake() method for Developer class. --- Apigee/Mint/Developer.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Apigee/Mint/Developer.php b/Apigee/Mint/Developer.php index d089eb4..98b7a07 100644 --- a/Apigee/Mint/Developer.php +++ b/Apigee/Mint/Developer.php @@ -137,6 +137,17 @@ public function __construct(OrgConfig $config) $this->initValues(); } + /** + * Upon unserialization re-initialize the object. + */ + public function __wakeup() + { + // Re-initialize the organization configuration. + $config = devconnect_default_org_config($this->config->orgName); + $base_url = '/mint/organizations/' . rawurlencode($config->orgName) . '/developers'; + $this->init($config, $base_url); + } + /** * {@inheritdoc} */ From be2e52da7ae2c4cef1a8f274ecffe3b01097e9d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20NAGY?= Date: Tue, 6 Nov 2018 13:27:31 +0100 Subject: [PATCH 3/4] Add static cache for Developer. --- Apigee/ManagementAPI/Developer.php | 141 +++++++++++++++++++++++++---- 1 file changed, 124 insertions(+), 17 deletions(-) diff --git a/Apigee/ManagementAPI/Developer.php b/Apigee/ManagementAPI/Developer.php index 4c99a2b..3000e19 100644 --- a/Apigee/ManagementAPI/Developer.php +++ b/Apigee/ManagementAPI/Developer.php @@ -369,11 +369,39 @@ public function setPageSize($size) */ public function __construct(OrgConfig $config) { - $this->init($config, '/o/' . rawurlencode($config->orgName) . '/developers'); + $this->organizationName = $config->orgName; + $this->init($config, '/o/' . rawurlencode($this->organizationName) . '/developers'); $this->blankValues(); $this->pageSize = self::MAX_ITEMS_PER_PAGE; } + /** + * Gets the static cache. + * + * @param string $organization + * The requested organization. + * @param bool $reset + * Resets the static cache for the requested organization. + * + * @return array + * Returns the static cache for the requested organization. + */ + protected static function &getStaticCache($organization, $reset = false) + { + static $data = array(); + + // Initialize static caches. + if (!array_key_exists($organization, $data) || $reset) { + $data[$organization] = array( + 'developers' => array(), + 'all_cached' => false, + 'all_expanded' => false, + ); + } + + return $data[$organization]; + } + /** * Loads a developer from the Management API using $email as the unique key. * @@ -385,12 +413,27 @@ public function __construct(OrgConfig $config) * @param string $email * This can be either the developer's email address or the unique * developerId. + * @param bool $reset + * Whether to reset static cache or not. */ - public function load($email) + public function load($email, $reset = false) { - $this->get(rawurlencode($email)); - $developer = $this->responseObj; - self::loadFromResponse($this, $developer); + // Reset only one developer. + if ($reset) { + self::resetCache($this->organizationName, array($email)); + } + + $cache = &static::getStaticCache($this->organizationName); + + if (!array_key_exists($email, $cache['developers']) || $cache['developers'][$email] === null) { + $this->get(rawurlencode($email)); + $developer = $this->responseObj; + self::loadFromResponse($this, $developer); + $cache['developers'][$email] = $this; + } else { + $developer = $cache['developers'][$email]->toArray(); + self::loadFromResponse($this, $developer); + } } /** @@ -408,18 +451,21 @@ protected static function loadFromResponse(Developer &$developer, array $respons $developer->firstName = $response['firstName']; $developer->lastName = $response['lastName']; $developer->userName = $response['userName']; - $developer->organizationName = $response['organizationName']; $developer->status = $response['status']; $developer->attributes = array(); if (array_key_exists('attributes', $response) && is_array($response['attributes'])) { - foreach ($response['attributes'] as $attribute) { - $developer->attributes[$attribute['name']] = @$attribute['value']; + foreach ($response['attributes'] as $key => $attribute) { + if (is_array($attribute)) { + $developer->attributes[$attribute['name']] = @$attribute['value']; + } else { + $developer->attributes[$key]= $attribute; + } } } $developer->createdAt = $response['createdAt']; $developer->createdBy = $response['createdBy']; - $developer->modifiedAt = $response['lastModifiedAt']; - $developer->modifiedBy = $response['lastModifiedBy']; + $developer->modifiedAt = array_key_exists('lastModifiedAt', $response) ? $response['lastModifiedAt'] : $response['modifiedAt']; + $developer->modifiedBy = array_key_exists('lastModifiedBy', $response) ? $response['lastModifiedBy'] : $response['modifiedBy'];; if (array_key_exists('companies', $response)) { $developer->companies = $response['companies']; } else { @@ -467,6 +513,9 @@ public function validate($email = null) * previous email value. * * @throws \Apigee\Exceptions\ParameterException + * In case of an invalid email, firstName, lastName or userName parameter. + * @throws \Apigee\Exceptions\ResponseException + * If there was a response error other than 404. */ public function save($forceUpdate = false, $oldEmail = null) { @@ -529,6 +578,11 @@ public function save($forceUpdate = false, $oldEmail = null) $this->post($url, $payload); } self::loadFromResponse($this, $this->responseObj); + + // Update static cache. + $cache = &static::getStaticCache($this->organizationName); + $cache['developers'][$this->email] = $this; + // We must cache the DebugData from the developer-save call so that // we can make it available to clients AFTER the "action" call below. $responseData = DebugData::toArray(); @@ -557,6 +611,9 @@ public function delete($email = null) { $email = $email ? : $this->email; $this->httpDelete(rawurlencode($email)); + + static::resetCache($this->organizationName, array($email)); + if ($email == $this->email) { $this->blankValues(); } @@ -565,10 +622,19 @@ public function delete($email = null) /** * Returns an array of all developer emails for this org. * + * @param bool $reset + * Whether to reset the static cache for the developers list or not. + * * @return string[] */ - public function listDevelopers() + public function listDevelopers($reset = false) { + // Get cached data. + $cache = &static::getStaticCache($this->organizationName, $reset); + if ($cache['all_cached']) { + return array_keys($cache['developers']); + } + $developers = array(); if ($this->pagingEnabled) { // Scroll through pages, saving the last key on each page. @@ -602,6 +668,13 @@ public function listDevelopers() $this->get(); $developers = $this->responseObj; } + + // Update static cache. + $new_developers = array_diff($developers, array_keys($cache['developers'])); + $cache['developers'] += array_fill_keys($new_developers, null); + $cache['all_cached'] = true; + $cache['all_expanded'] = empty($new_developers); + return $developers; } @@ -613,11 +686,19 @@ public function listDevelopers() * a canonical listing of a developer's apps, you should invoke * DeveloperApp::getList() or DeveloperApp::getListDetail(). * + * @param bool $reset + * Whether to reset the static cache for the developers list or not. + * * @return Developer[] */ - public function loadAllDevelopers() + public function loadAllDevelopers($reset = false) { - $developers = array(); + $cache = &static::getStaticCache($this->organizationName, $reset); + + if ($cache['all_cached'] && $cache['all_expanded']) { + return $cache['developers']; + } + if ($this->pagingEnabled) { $lastKey = null; while (true) { @@ -639,7 +720,7 @@ public function loadAllDevelopers() foreach ($developerSubset['developer'] as $dev) { $developer = new Developer($this->config); self::loadFromResponse($developer, $dev); - $developers[] = $developer; + $cache['developers'][$developer->email] = $developer; } if ($subsetCount == $this->pageSize) { $lastDeveloper = end($developerSubset['developer']); @@ -654,10 +735,13 @@ public function loadAllDevelopers() foreach ($developerList['developer'] as $dev) { $developer = new Developer($this->config); self::loadFromResponse($developer, $dev); - $developers[] = $developer; + $cache['developers'][$developer->email] = $developer; } } - return $developers; + + $cache['all_cached'] = true; + $cache['all_expanded'] = true; + return $cache['developers']; } /** @@ -699,7 +783,6 @@ public function blankValues() $this->firstName = null; $this->lastName = null; $this->userName = null; - $this->organizationName = null; $this->status = null; $this->attributes = array(); $this->createdAt = null; @@ -751,4 +834,28 @@ public function fromArray(array $array) } } } + + /** + * Resets the internal static developers cache. + * + * @param string $organization + * The name of the organization for the cache needs to be cleared. + * @param array|null $ids + * (optional) If specified, the cache is reset for the developers with the + * given ids only. + */ + public static function resetCache($organization, $ids = array()) + { + $cache = &static::getStaticCache($organization); + if (empty($ids)) { + $cache['developers'] = array(); + $cache['all_expanded'] = false; + } + else { + foreach ($ids as $id) { + unset($cache['developers'][$id]); + } + } + $cache['all_cached'] = false; + } } From a2ee27b4718f7dbd4b8aaac23345e1a3a27b7d9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20NAGY?= Date: Fri, 7 Dec 2018 09:43:50 +0100 Subject: [PATCH 4/4] Remove developer cache on SDK level #DRUPS-32 --- Apigee/ManagementAPI/Developer.php | 123 +++-------------------------- 1 file changed, 11 insertions(+), 112 deletions(-) diff --git a/Apigee/ManagementAPI/Developer.php b/Apigee/ManagementAPI/Developer.php index 3000e19..c9b5ba5 100644 --- a/Apigee/ManagementAPI/Developer.php +++ b/Apigee/ManagementAPI/Developer.php @@ -375,33 +375,6 @@ public function __construct(OrgConfig $config) $this->pageSize = self::MAX_ITEMS_PER_PAGE; } - /** - * Gets the static cache. - * - * @param string $organization - * The requested organization. - * @param bool $reset - * Resets the static cache for the requested organization. - * - * @return array - * Returns the static cache for the requested organization. - */ - protected static function &getStaticCache($organization, $reset = false) - { - static $data = array(); - - // Initialize static caches. - if (!array_key_exists($organization, $data) || $reset) { - $data[$organization] = array( - 'developers' => array(), - 'all_cached' => false, - 'all_expanded' => false, - ); - } - - return $data[$organization]; - } - /** * Loads a developer from the Management API using $email as the unique key. * @@ -413,27 +386,12 @@ protected static function &getStaticCache($organization, $reset = false) * @param string $email * This can be either the developer's email address or the unique * developerId. - * @param bool $reset - * Whether to reset static cache or not. */ - public function load($email, $reset = false) + public function load($email) { - // Reset only one developer. - if ($reset) { - self::resetCache($this->organizationName, array($email)); - } - - $cache = &static::getStaticCache($this->organizationName); - - if (!array_key_exists($email, $cache['developers']) || $cache['developers'][$email] === null) { - $this->get(rawurlencode($email)); - $developer = $this->responseObj; - self::loadFromResponse($this, $developer); - $cache['developers'][$email] = $this; - } else { - $developer = $cache['developers'][$email]->toArray(); - self::loadFromResponse($this, $developer); - } + $this->get(rawurlencode($email)); + $developer = $this->responseObj; + self::loadFromResponse($this, $developer); } /** @@ -456,7 +414,7 @@ protected static function loadFromResponse(Developer &$developer, array $respons if (array_key_exists('attributes', $response) && is_array($response['attributes'])) { foreach ($response['attributes'] as $key => $attribute) { if (is_array($attribute)) { - $developer->attributes[$attribute['name']] = @$attribute['value']; + $developer->attributes[$attribute['name']] = array_key_exists('value', $attribute) ? $attribute['value'] : null; } else { $developer->attributes[$key]= $attribute; } @@ -578,11 +536,6 @@ public function save($forceUpdate = false, $oldEmail = null) $this->post($url, $payload); } self::loadFromResponse($this, $this->responseObj); - - // Update static cache. - $cache = &static::getStaticCache($this->organizationName); - $cache['developers'][$this->email] = $this; - // We must cache the DebugData from the developer-save call so that // we can make it available to clients AFTER the "action" call below. $responseData = DebugData::toArray(); @@ -611,9 +564,6 @@ public function delete($email = null) { $email = $email ? : $this->email; $this->httpDelete(rawurlencode($email)); - - static::resetCache($this->organizationName, array($email)); - if ($email == $this->email) { $this->blankValues(); } @@ -622,19 +572,10 @@ public function delete($email = null) /** * Returns an array of all developer emails for this org. * - * @param bool $reset - * Whether to reset the static cache for the developers list or not. - * * @return string[] */ - public function listDevelopers($reset = false) + public function listDevelopers() { - // Get cached data. - $cache = &static::getStaticCache($this->organizationName, $reset); - if ($cache['all_cached']) { - return array_keys($cache['developers']); - } - $developers = array(); if ($this->pagingEnabled) { // Scroll through pages, saving the last key on each page. @@ -668,13 +609,6 @@ public function listDevelopers($reset = false) $this->get(); $developers = $this->responseObj; } - - // Update static cache. - $new_developers = array_diff($developers, array_keys($cache['developers'])); - $cache['developers'] += array_fill_keys($new_developers, null); - $cache['all_cached'] = true; - $cache['all_expanded'] = empty($new_developers); - return $developers; } @@ -686,19 +620,11 @@ public function listDevelopers($reset = false) * a canonical listing of a developer's apps, you should invoke * DeveloperApp::getList() or DeveloperApp::getListDetail(). * - * @param bool $reset - * Whether to reset the static cache for the developers list or not. - * * @return Developer[] */ - public function loadAllDevelopers($reset = false) + public function loadAllDevelopers() { - $cache = &static::getStaticCache($this->organizationName, $reset); - - if ($cache['all_cached'] && $cache['all_expanded']) { - return $cache['developers']; - } - + $developers = array(); if ($this->pagingEnabled) { $lastKey = null; while (true) { @@ -720,7 +646,7 @@ public function loadAllDevelopers($reset = false) foreach ($developerSubset['developer'] as $dev) { $developer = new Developer($this->config); self::loadFromResponse($developer, $dev); - $cache['developers'][$developer->email] = $developer; + $developers[] = $developer; } if ($subsetCount == $this->pageSize) { $lastDeveloper = end($developerSubset['developer']); @@ -735,13 +661,10 @@ public function loadAllDevelopers($reset = false) foreach ($developerList['developer'] as $dev) { $developer = new Developer($this->config); self::loadFromResponse($developer, $dev); - $cache['developers'][$developer->email] = $developer; + $developers[] = $developer; } } - - $cache['all_cached'] = true; - $cache['all_expanded'] = true; - return $cache['developers']; + return $developers; } /** @@ -834,28 +757,4 @@ public function fromArray(array $array) } } } - - /** - * Resets the internal static developers cache. - * - * @param string $organization - * The name of the organization for the cache needs to be cleared. - * @param array|null $ids - * (optional) If specified, the cache is reset for the developers with the - * given ids only. - */ - public static function resetCache($organization, $ids = array()) - { - $cache = &static::getStaticCache($organization); - if (empty($ids)) { - $cache['developers'] = array(); - $cache['all_expanded'] = false; - } - else { - foreach ($ids as $id) { - unset($cache['developers'][$id]); - } - } - $cache['all_cached'] = false; - } }