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

Addons can get their directory without needing it in the manifest #2761

Merged
merged 1 commit into from
Oct 28, 2020
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"autoload-dev": {
"psr-4": {
"Tests\\": "tests",
"Foo\\Bar\\": "tests/fixtures/Addon"
"Foo\\Bar\\": "tests/Fixtures/Addon"
}
}
}
40 changes: 35 additions & 5 deletions src/Extend/Addon.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Statamic\Extend;

use Facades\Statamic\Licensing\LicenseManager;
use ReflectionClass;
use Statamic\Facades\File;
use Statamic\Facades\Path;
use Statamic\Facades\URL;
Expand Down Expand Up @@ -63,6 +64,13 @@ final class Addon
*/
protected $autoload;

/**
* The service provider class.
*
* @var string
*/
protected $provider;

/**
* The name of the addon. eg. "Bloodhound Search".
*
Expand Down Expand Up @@ -162,8 +170,8 @@ public static function makeFromPackage(array $package)
$instance = self::make($package['id']);

$keys = [
'id', 'slug', 'editions', 'marketplaceId', 'marketplaceSlug', 'marketplaceSellerSlug', 'name', 'namespace', 'directory',
'autoload', 'description', 'package', 'version', 'latestVersion', 'url', 'developer', 'developerUrl', 'isCommercial',
'id', 'slug', 'editions', 'marketplaceId', 'marketplaceSlug', 'marketplaceSellerSlug', 'name', 'namespace',
'autoload', 'provider', 'description', 'package', 'version', 'latestVersion', 'url', 'developer', 'developerUrl', 'isCommercial',
];

foreach (Arr::only($package, $keys) as $key => $value) {
Expand Down Expand Up @@ -297,7 +305,7 @@ public function name($name = null)
public function hasFile($path)
{
if (! $this->directory()) {
throw new \Exception('Cannot check files without a directory specified.');
throw new \Exception('Cannot check files without a provider specified.');
}

return File::exists(Path::assemble($this->directory(), $path));
Expand All @@ -312,7 +320,7 @@ public function hasFile($path)
public function getFile($path)
{
if (! $this->directory()) {
throw new \Exception('Cannot get files without a directory specified.');
throw new \Exception('Cannot get files without a provider specified.');
}

return File::get(Path::assemble($this->directory(), $path));
Expand All @@ -327,7 +335,7 @@ public function getFile($path)
public function putFile($path, $contents)
{
if (! $this->directory()) {
throw new \Exception('Cannot write files without a directory specified.');
throw new \Exception('Cannot write files without a provider specified.');
}

File::put(
Expand Down Expand Up @@ -393,6 +401,28 @@ public function __call($method, $args)
return $this;
}

/**
* The directory the package is located within. eg. "/path/to/vendor/statamic/bloodhound".
*
* @return string
*/
public function directory()
{
if (! $this->provider) {
return null;
}

if ($this->directory) {
return $this->directory;
}

$reflector = new ReflectionClass($this->provider);

$dir = Str::removeRight(dirname($reflector->getFileName()), rtrim($this->autoload, '/'));

return $this->directory = Str::removeRight($dir, '/');
}

public function existsOnMarketplace()
{
return $this->marketplaceSlug() !== null;
Expand Down
2 changes: 1 addition & 1 deletion src/Extend/Manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ protected function formatPackage($package)
'latestVersion' => data_get($marketplaceData, 'latest_version', null),
'version' => Str::removeLeft($package['version'], 'v'),
'namespace' => $namespace,
'directory' => $directory,
'autoload' => $autoload,
'provider' => $provider,

// Local data for marketplace GUI?
'name' => $statamic['name'] ?? Arr::last($providerParts),
Expand Down
50 changes: 30 additions & 20 deletions tests/Extend/AddonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
namespace Statamic\Testing\Extend;

use Facades\Statamic\Licensing\LicenseManager;
use Foo\Bar\TestAddonServiceProvider;
use Illuminate\Support\Collection;
use Statamic\Extend\Addon;
use Statamic\Facades\File;
use Tests\TestCase;

class AddonTest extends TestCase
{
protected $addonFixtureDir;

public function setUp(): void
{
parent::setUp();
$this->addonFixtureDir = realpath(__DIR__.'/../Fixtures/Addon');
}

/** @test */
public function it_creates_an_instance_with_a_name()
{
Expand Down Expand Up @@ -100,7 +109,8 @@ public function it_creates_an_instance_from_a_package()
$this->assertEquals('Test Addon', $addon->name());
$this->assertEquals('Test description', $addon->description());
$this->assertEquals('Vendor\\TestAddon', $addon->namespace());
$this->assertEquals('/path/to/addon', $addon->directory());
$this->assertEquals($this->addonFixtureDir, $addon->directory());
$this->assertEquals('', $addon->autoload());
$this->assertEquals('http://test-url.com', $addon->url());
$this->assertEquals('Test Developer LLC', $addon->developer());
$this->assertEquals('http://test-developer.com', $addon->developerUrl());
Expand All @@ -111,10 +121,10 @@ public function it_creates_an_instance_from_a_package()
/** @test */
public function it_checks_if_a_file_exists()
{
$addon = Addon::make('Test Addon')->directory('/path/to/addon');
$addon = $this->makeFromPackage();

File::shouldReceive('exists')->with('/path/to/addon/test.txt')->andReturnTrue();
File::shouldReceive('exists')->with('/path/to/addon/notfound.txt')->andReturnFalse();
File::shouldReceive('exists')->with($this->addonFixtureDir.'/test.txt')->andReturnTrue();
File::shouldReceive('exists')->with($this->addonFixtureDir.'/notfound.txt')->andReturnFalse();

$this->assertTrue($addon->hasFile('test.txt'));
$this->assertFalse($addon->hasFile('notfound.txt'));
Expand All @@ -123,33 +133,33 @@ public function it_checks_if_a_file_exists()
/** @test */
public function it_gets_file_contents()
{
$addon = Addon::make('Test Addon')->directory('/path/to/addon');
$addon = $this->makeFromPackage();

File::shouldReceive('get')->with('/path/to/addon/test.txt')->andReturn('the file contents');
File::shouldReceive('get')->with($this->addonFixtureDir.'/test.txt')->andReturn('the file contents');

$this->assertEquals('the file contents', $addon->getFile('test.txt'));
}

/** @test */
public function it_writes_file_contents()
{
$addon = Addon::make('Test Addon')->directory('/path/to/addon');
$addon = $this->makeFromPackage();

File::shouldReceive('put')->with('/path/to/addon/test.txt', 'the file contents');
File::shouldReceive('put')->with($this->addonFixtureDir.'/test.txt', 'the file contents');

$addon->putFile('test.txt', 'the file contents');
}

/** @test */
public function it_doesnt_allow_getting_files_if_no_directory_is_set()
public function it_doesnt_allow_getting_files_if_no_provider_is_set()
{
File::spy();
$addon = $this->makeFromPackage(['directory' => null]);
$addon = $this->makeFromPackage(['provider' => null]);

try {
$addon->getFile('foo.txt', 'foo');
} catch (\Exception $e) {
$this->assertEquals('Cannot get files without a directory specified.', $e->getMessage());
$this->assertEquals('Cannot get files without a provider specified.', $e->getMessage());
File::shouldNotHaveReceived('get');

return;
Expand All @@ -159,15 +169,15 @@ public function it_doesnt_allow_getting_files_if_no_directory_is_set()
}

/** @test */
public function it_doesnt_allow_checking_for_files_if_no_directory_is_set()
public function it_doesnt_allow_checking_for_files_if_no_provider_is_set()
{
File::spy();
$addon = $this->makeFromPackage(['directory' => null]);
$addon = $this->makeFromPackage(['provider' => null]);

try {
$addon->hasFile('foo.txt', 'foo');
} catch (\Exception $e) {
$this->assertEquals('Cannot check files without a directory specified.', $e->getMessage());
$this->assertEquals('Cannot check files without a provider specified.', $e->getMessage());
File::shouldNotHaveReceived('get');

return;
Expand All @@ -177,15 +187,15 @@ public function it_doesnt_allow_checking_for_files_if_no_directory_is_set()
}

/** @test */
public function it_doesnt_allow_writing_files_if_no_directory_is_set()
public function it_doesnt_allow_writing_files_if_no_provider_is_set()
{
File::spy();
$addon = $this->makeFromPackage(['directory' => null]);
$addon = $this->makeFromPackage(['provider' => null]);

try {
$addon->putFile('foo.txt', 'foo');
} catch (\Exception $e) {
$this->assertEquals('Cannot write files without a directory specified.', $e->getMessage());
$this->assertEquals('Cannot write files without a provider specified.', $e->getMessage());
File::shouldNotHaveReceived('put');

return;
Expand Down Expand Up @@ -230,15 +240,15 @@ public function it_gets_the_license()
$this->assertEquals('the license', Addon::make('foo/bar')->license());
}

private function makeFromPackage($attributes)
private function makeFromPackage($attributes = [])
{
return Addon::makeFromPackage(array_merge([
'id' => 'vendor/test-addon',
'name' => 'Test Addon',
'description' => 'Test description',
'namespace' => 'Vendor\\TestAddon',
'directory' => '/path/to/addon',
'autoload' => 'src',
'provider' => TestAddonServiceProvider::class,
'autoload' => '',
'url' => 'http://test-url.com',
'developer' => 'Test Developer LLC',
'developerUrl' => 'http://test-developer.com',
Expand Down
10 changes: 10 additions & 0 deletions tests/Fixtures/Addon/TestAddonServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Foo\Bar;

use Statamic\Providers\AddonServiceProvider;

class TestAddonServiceProvider extends AddonServiceProvider
{
//
}