Skip to content
This repository was archived by the owner on Apr 27, 2026. It is now read-only.

Commit c7de082

Browse files
committed
Move methods to Service/API.
1 parent 4b84794 commit c7de082

7 files changed

Lines changed: 109 additions & 100 deletions

File tree

src/Command/CommandBase.php

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,6 @@ abstract class CommandBase extends Command implements MultiAwareInterface
166166
*/
167167
private $synopsis = [];
168168

169-
private static $cachedNextDeployment = [];
170-
171169
/**
172170
* {@inheritdoc}
173171
*/
@@ -2489,92 +2487,4 @@ protected function hasExternalGitHost(Project $project)
24892487

24902488
return $ssh->hostIsInternal($project->getGitUrl()) === false;
24912489
}
2492-
2493-
/**
2494-
* Loads the next environment deployment and caches it statically.
2495-
*
2496-
* The static cache means it can be reused while running a sub-command.
2497-
*
2498-
* @param Environment $environment
2499-
* @param bool $reset
2500-
* @return EnvironmentDeployment
2501-
*/
2502-
protected function loadNextDeployment(Environment $environment, $reset = false)
2503-
{
2504-
$cacheKey = $environment->project . ':' . $environment->id;
2505-
if (isset(self::$cachedNextDeployment[$cacheKey]) && !$reset) {
2506-
return self::$cachedNextDeployment[$cacheKey];
2507-
}
2508-
$progress = new ProgressMessage($this->stdErr);
2509-
try {
2510-
$progress->show('Loading deployment information...');
2511-
$next = $environment->getNextDeployment();
2512-
if (!$next) {
2513-
throw new EnvironmentStateException('No next deployment found', $environment);
2514-
}
2515-
} finally {
2516-
$progress->done();
2517-
}
2518-
return self::$cachedNextDeployment[$cacheKey] = $next;
2519-
}
2520-
2521-
/**
2522-
* Lists services in a deployment.
2523-
*
2524-
* @param EnvironmentDeployment $deployment
2525-
*
2526-
* @return array<string, WebApp||Worker|Service>
2527-
* An array of services keyed by the service name.
2528-
*/
2529-
protected function allServices(EnvironmentDeployment $deployment)
2530-
{
2531-
$webapps = $deployment->webapps;
2532-
$workers = $deployment->workers;
2533-
$services = $deployment->services;
2534-
ksort($webapps, SORT_STRING|SORT_FLAG_CASE);
2535-
ksort($workers, SORT_STRING|SORT_FLAG_CASE);
2536-
ksort($services, SORT_STRING|SORT_FLAG_CASE);
2537-
return array_merge($webapps, $workers, $services);
2538-
}
2539-
2540-
/**
2541-
* Check if project supports guaranteed resources.
2542-
*
2543-
* @param array $projectInfo
2544-
*
2545-
* @return bool
2546-
* True if guaranteed CPU is supported, false otherwise.
2547-
*/
2548-
protected function supportsGuaranteedCPU(array $projectInfo)
2549-
{
2550-
return !empty($projectInfo["settings"]["enable_guaranteed_resources"]) &&
2551-
!empty($projectInfo["capabilities"]["guaranteed_resources"]["enabled"]);
2552-
}
2553-
2554-
/**
2555-
* Check if environment has guaranteed CPU.
2556-
*
2557-
* @param \Platformsh\Client\Model\Environment $environment
2558-
*
2559-
* @return bool
2560-
*/
2561-
protected function environmentHasGuaranteedCPU(Environment $environment)
2562-
{
2563-
$nextDeployment = $this->loadNextDeployment($environment);
2564-
if ($this->supportsGuaranteedCPU($nextDeployment->project_info)) {
2565-
$containerProfiles = $nextDeployment->container_profiles;
2566-
$services = $this->allServices($nextDeployment);
2567-
foreach ($services as $service) {
2568-
$properties = $service->getProperties();
2569-
if (isset($properties['container_profile']) && isset($containerProfiles[$properties['container_profile']][$properties['resources']['profile_size']])) {
2570-
$profileInfo = $containerProfiles[$properties['container_profile']][$properties['resources']['profile_size']];
2571-
if (isset($profileInfo['cpu_type']) && $profileInfo['cpu_type'] == 'guaranteed') {
2572-
return true;
2573-
}
2574-
}
2575-
}
2576-
}
2577-
2578-
return false;
2579-
}
25802490
}

src/Command/Environment/EnvironmentActivateCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ protected function activateMultiple(array $environments, InputInterface $input,
113113
continue;
114114
}
115115

116-
$hasGuaranteedCPU = $this->environmentHasGuaranteedCPU($environment);
116+
$hasGuaranteedCPU = $this->api()->environmentHasGuaranteedCPU($environment);
117117
$question = "Are you sure you want to activate the environment " . $this->api()->getEnvironmentLabel($environment) . "?";
118118
if ($resourcesInit === 'parent' && $hasGuaranteedCPU && $this->config()->has('warnings.guaranteed_resources_msg')) {
119119
$question = trim($this->config()->get('warnings.guaranteed_resources_msg'))

src/Command/Environment/EnvironmentBranchCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
143143
$this->stdErr->writeln('Resource sizes will be inherited from the parent environment.');
144144
}
145145

146-
$hasGuaranteedCPU = $this->environmentHasGuaranteedCPU($parentEnvironment);
146+
$hasGuaranteedCPU = $this->api()->environmentHasGuaranteedCPU($parentEnvironment);
147147
if ($resourcesInit === 'parent' && $hasGuaranteedCPU && $this->config()->has('warnings.guaranteed_resources_msg')) {
148148
/** @var \Platformsh\Cli\Service\QuestionHelper $questionHelper */
149149
$questionHelper = $this->getService('question_helper');

src/Command/Resources/ResourcesGetCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
5454
$environment = $this->getSelectedEnvironment();
5555

5656
try {
57-
$nextDeployment = $this->loadNextDeployment($environment);
57+
$nextDeployment = $this->api()->loadNextDeployment($environment);
5858
} catch (EnvironmentStateException $e) {
5959
if ($environment->status === 'inactive') {
6060
$this->stdErr->writeln(sprintf('The environment %s is not active so resource configuration cannot be read.', $this->api()->getEnvironmentLabel($environment, 'comment')));
@@ -63,7 +63,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
6363
throw $e;
6464
}
6565

66-
$services = $this->allServices($nextDeployment);
66+
$services = $this->api()->allServices($nextDeployment);
6767
if (empty($services)) {
6868
$this->stdErr->writeln('No apps or services found');
6969
return 1;

src/Command/Resources/ResourcesSetCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
7676
$environment = $this->getSelectedEnvironment();
7777

7878
try {
79-
$nextDeployment = $this->loadNextDeployment($environment);
79+
$nextDeployment = $this->api()->loadNextDeployment($environment);
8080
} catch (EnvironmentStateException $e) {
8181
if ($environment->status === 'inactive') {
8282
$this->stdErr->writeln(sprintf('The environment %s is not active so resources cannot be configured.', $this->api()->getEnvironmentLabel($environment, 'comment')));
@@ -85,7 +85,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
8585
throw $e;
8686
}
8787

88-
$services = $this->allServices($nextDeployment);
88+
$services = $this->api()->allServices($nextDeployment);
8989
if (empty($services)) {
9090
$this->stdErr->writeln('No apps or services found');
9191
return 1;
@@ -138,7 +138,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
138138
$containerProfiles = $nextDeployment->container_profiles;
139139

140140
// Remove guaranteed profiles if project does not support it.
141-
$supportsGuaranteedCPU = $this->supportsGuaranteedCPU($nextDeployment->project_info);
141+
$supportsGuaranteedCPU = $this->api()->supportsGuaranteedCPU($nextDeployment->project_info);
142142
foreach ($containerProfiles as $profileName => $profile) {
143143
foreach ($profile as $sizeName => $sizeInfo) {
144144
if (!$supportsGuaranteedCPU && $sizeInfo['cpu_type'] == 'guaranteed') {

src/Command/Resources/ResourcesSizeListCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
3838
}
3939

4040
$environment = $this->getSelectedEnvironment();
41-
$nextDeployment = $this->loadNextDeployment($environment);
41+
$nextDeployment = $this->api()->loadNextDeployment($environment);
4242

43-
$services = $this->allServices($nextDeployment);
43+
$services = $this->api()->allServices($nextDeployment);
4444
if (empty($services)) {
4545
$this->stdErr->writeln('No apps or services found');
4646
return 1;
@@ -84,7 +84,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
8484
$table = $this->getService('table');
8585

8686
$rows = [];
87-
$supportsGuaranteedCPU = $this->supportsGuaranteedCPU($nextDeployment->project_info);
87+
$supportsGuaranteedCPU = $this->api()->supportsGuaranteedCPU($nextDeployment->project_info);
8888
$defaultColumns = $this->defaultColumns;
8989
if ($supportsGuaranteedCPU) {
9090
$defaultColumns[] = 'cpu_type';

src/Service/Api.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use GuzzleHttp\Event\ErrorEvent;
1111
use GuzzleHttp\Exception\BadResponseException;
1212
use GuzzleHttp\Message\ResponseInterface;
13+
use Platformsh\Cli\Console\ProgressMessage;
1314
use Platformsh\Cli\CredentialHelper\KeyringUnavailableException;
1415
use Platformsh\Cli\CredentialHelper\Manager;
1516
use Platformsh\Cli\CredentialHelper\SessionStorage as CredentialHelperStorage;
@@ -25,6 +26,9 @@
2526
use Platformsh\Client\Exception\EnvironmentStateException;
2627
use Platformsh\Client\Model\BasicProjectInfo;
2728
use Platformsh\Client\Model\Deployment\EnvironmentDeployment;
29+
use Platformsh\Client\Model\Deployment\Service;
30+
use Platformsh\Client\Model\Deployment\WebApp;
31+
use Platformsh\Client\Model\Deployment\Worker;
2832
use Platformsh\Client\Model\Environment;
2933
use Platformsh\Client\Model\EnvironmentType;
3034
use Platformsh\Client\Model\Organization\Member;
@@ -74,6 +78,13 @@ class Api
7478
/** @var FileLock */
7579
private $fileLock;
7680

81+
/**
82+
* Cached next deployment.
83+
*
84+
* @var array
85+
*/
86+
private static array $cachedNextDeployment = [];
87+
7788
/**
7889
* The library's API client object.
7990
*
@@ -1811,4 +1822,92 @@ public function getAutoscalingSettings(Environment $environment)
18111822
}
18121823
return $settings;
18131824
}
1825+
1826+
/**
1827+
* Loads the next environment deployment and caches it statically.
1828+
*
1829+
* The static cache means it can be reused while running a sub-command.
1830+
*
1831+
* @param Environment $environment
1832+
* @param bool $reset
1833+
* @return EnvironmentDeployment
1834+
*/
1835+
public function loadNextDeployment(Environment $environment, $reset = false)
1836+
{
1837+
$cacheKey = $environment->project . ':' . $environment->id;
1838+
if (isset(self::$cachedNextDeployment[$cacheKey]) && !$reset) {
1839+
return self::$cachedNextDeployment[$cacheKey];
1840+
}
1841+
$progress = new ProgressMessage($this->stdErr);
1842+
try {
1843+
$progress->show('Loading deployment information...');
1844+
$next = $environment->getNextDeployment();
1845+
if (!$next) {
1846+
throw new EnvironmentStateException('No next deployment found', $environment);
1847+
}
1848+
} finally {
1849+
$progress->done();
1850+
}
1851+
return self::$cachedNextDeployment[$cacheKey] = $next;
1852+
}
1853+
1854+
/**
1855+
* Lists services in a deployment.
1856+
*
1857+
* @param EnvironmentDeployment $deployment
1858+
*
1859+
* @return array<string, WebApp||Worker|Service>
1860+
* An array of services keyed by the service name.
1861+
*/
1862+
public function allServices(EnvironmentDeployment $deployment)
1863+
{
1864+
$webapps = $deployment->webapps;
1865+
$workers = $deployment->workers;
1866+
$services = $deployment->services;
1867+
ksort($webapps, SORT_STRING|SORT_FLAG_CASE);
1868+
ksort($workers, SORT_STRING|SORT_FLAG_CASE);
1869+
ksort($services, SORT_STRING|SORT_FLAG_CASE);
1870+
return array_merge($webapps, $workers, $services);
1871+
}
1872+
1873+
/**
1874+
* Check if project supports guaranteed resources.
1875+
*
1876+
* @param array $projectInfo
1877+
*
1878+
* @return bool
1879+
* True if guaranteed CPU is supported, false otherwise.
1880+
*/
1881+
public function supportsGuaranteedCPU(array $projectInfo)
1882+
{
1883+
return !empty($projectInfo["settings"]["enable_guaranteed_resources"]) &&
1884+
!empty($projectInfo["capabilities"]["guaranteed_resources"]["enabled"]);
1885+
}
1886+
1887+
/**
1888+
* Check if environment has guaranteed CPU.
1889+
*
1890+
* @param \Platformsh\Client\Model\Environment $environment
1891+
*
1892+
* @return bool
1893+
*/
1894+
public function environmentHasGuaranteedCPU(Environment $environment)
1895+
{
1896+
$nextDeployment = $this->loadNextDeployment($environment);
1897+
if ($this->supportsGuaranteedCPU($nextDeployment->project_info)) {
1898+
$containerProfiles = $nextDeployment->container_profiles;
1899+
$services = $this->allServices($nextDeployment);
1900+
foreach ($services as $service) {
1901+
$properties = $service->getProperties();
1902+
if (isset($properties['container_profile']) && isset($containerProfiles[$properties['container_profile']][$properties['resources']['profile_size']])) {
1903+
$profileInfo = $containerProfiles[$properties['container_profile']][$properties['resources']['profile_size']];
1904+
if (isset($profileInfo['cpu_type']) && $profileInfo['cpu_type'] == 'guaranteed') {
1905+
return true;
1906+
}
1907+
}
1908+
}
1909+
}
1910+
1911+
return false;
1912+
}
18141913
}

0 commit comments

Comments
 (0)