Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue #1742 where the creation of GPM object tried to connect t… #1746

Merged
merged 1 commit into from
Nov 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions system/src/Grav/Common/GPM/GPM.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

class GPM extends Iterator
{
/** @var callable */
private $callback;

/**
* Local installed Packages
* @var Local\Packages
Expand All @@ -29,6 +32,9 @@ class GPM extends Iterator
*/
private $repository;

/** @var bool */
private $shouldRefresh;

/**
* @var Remote\GravCore
*/
Expand All @@ -47,18 +53,33 @@ class GPM extends Iterator
];

/**
* Creates a new GPM instance with Local and Remote packages available
* Loads Remote Packages available
*/
private function retrieveRemoteRepository()
{
if (!$this->repository) {
$this->repository = new Remote\Packages($this->shouldRefresh, $this->callback);
}
}

/**
* Creates a new GPM instance with Local packages available
* @param boolean $refresh Applies to Remote Packages only and forces a refetch of data
* @param callable $callback Either a function or callback in array notation
*/
public function __construct($refresh = false, $callback = null)
{
$this->installed = new Local\Packages();
try {
$this->repository = new Remote\Packages($refresh, $callback);
$this->grav = new Remote\GravCore($refresh, $callback);
} catch (\Exception $e) {
}
$this->shouldRefresh = $refresh;
$this->callback = $callback;
}

/**
* Loads Remote Grav package available
*/
public function loadRemoteGrav()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot see this being called from anywhere...?

{
$this->grav = new Remote\GravCore($this->refresh, $this->callback);
}

/**
Expand Down Expand Up @@ -268,6 +289,7 @@ public function getUpdatablePlugins()
*/
public function getLatestVersionOfPackage($package_name)
{
$this->retrieveRemoteRepository();
$repository = $this->repository['plugins'];
if (isset($repository[$package_name])) {
return $repository[$package_name]->available ?: $repository[$package_name]->version;
Expand Down Expand Up @@ -310,6 +332,7 @@ public function isPluginUpdatable($plugin)
public function getUpdatableThemes()
{
$items = [];
$this->retrieveRemoteRepository();
$repository = $this->repository['themes'];

// local cache to speed things up
Expand Down Expand Up @@ -357,6 +380,7 @@ public function isThemeUpdatable($theme)
*/
public function getReleaseType($package_name)
{
$this->retrieveRemoteRepository();
$repository = $this->repository['plugins'];
if (isset($repository[$package_name])) {
return $repository[$package_name]->release_type;
Expand Down Expand Up @@ -405,6 +429,7 @@ public function isTestingRelease($package_name)
*/
public function getRepositoryPlugin($slug)
{
$this->retrieveRemoteRepository();
return @$this->repository['plugins'][$slug];
}

Expand Down Expand Up @@ -443,6 +468,7 @@ public function getRepositoryThemes()
*/
public function getRepository()
{
$this->retrieveRemoteRepository();
return $this->repository;
}

Expand Down
43 changes: 43 additions & 0 deletions tests/unit/Grav/Common/GPM/GPMTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,47 @@ public function testCalculateVersionNumberFromDependencyVersion()
$this->assertSame(null, $this->gpm->calculateVersionNumberFromDependencyVersion('*'));
$this->assertSame('2.0.2', $this->gpm->calculateVersionNumberFromDependencyVersion('2.0.2'));
}

public function testRemoteRepositoryIsEmptyAfterConstruct()
{
$gpm = new GPM();
$reflection = new \ReflectionClass(get_class($gpm));
$repository = $reflection->getProperty('repository');
$repository->setAccessible(true);

$this->assertSame(null, $repository->getValue($gpm));
}

public function testLocalRepositoryIsNotEmptyAfterConstruct()
{
$gpm = new GPM();
$reflection = new \ReflectionClass(get_class($gpm));
$repository = $reflection->getProperty('installed');
$repository->setAccessible(true);

$this->assertInstanceOf( '\Grav\Common\GPM\Local\Packages', $repository->getValue($gpm));
}

public function testGetRepository()
{
$this->assertInstanceOf('\Grav\Common\GPM\Remote\Packages', $this->gpm->getRepository());
}

public function testLatestVersionOfPackage()
{
$gpm = new GPM();
$this->assertNotNull($gpm->getLatestVersionOfPackage('admin'));
}

public function testReleaseType()
{
$gpm = new GPM();
$this->assertNotNull($gpm->getReleaseType('admin'));
}

public function testGetRepositoryPlugin()
{
$gpm = new GPM();
$this->assertNotNull($gpm->getRepositoryPlugin('admin'));
}
}