Skip to content

Commit

Permalink
Merge branch 'v3'
Browse files Browse the repository at this point in the history
  • Loading branch information
johanrosenson committed Feb 17, 2023
2 parents 14a0703 + c791820 commit 984da45
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 130 deletions.
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
],
"require": {
"php": "^7.4|^8.0",
"ext-mbstring": "*",
"laravel/framework": "^7.0|^8.0"
},
"require-dev": {
Expand All @@ -29,12 +30,9 @@
"Devlop\\FontAwesome\\Tests\\": "tests/"
}
},
"minimum-stability": "dev",
"minimum-stability": "stable",
"prefer-stable": true,
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
},
"laravel": {
"providers": [
"Devlop\\FontAwesome\\FontAwesomeBladeServiceProvider"
Expand Down
20 changes: 20 additions & 0 deletions src/Components/SharpRegular.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Devlop\FontAwesome\Components;

use Devlop\FontAwesome\FontAwesomeBaseComponent;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

final class SharpRegular extends FontAwesomeBaseComponent
{
/**
* Create a new component instance.
*/
public function __construct(string $path, string $name)
{
parent::__construct($path, 'sharp-regular', $name);
}
}
20 changes: 20 additions & 0 deletions src/Components/SharpSolid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Devlop\FontAwesome\Components;

use Devlop\FontAwesome\FontAwesomeBaseComponent;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

final class SharpSolid extends FontAwesomeBaseComponent
{
/**
* Create a new component instance.
*/
public function __construct(string $path, string $name)
{
parent::__construct($path, 'sharp-solid', $name);
}
}
2 changes: 1 addition & 1 deletion src/FontAwesomeBaseComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function __construct(string $path, string $style, string $name)
{
$this->path = $path;
$this->style = $style;
$this->name = $name;
$this->name = mb_strtolower($name);
}

/**
Expand Down
59 changes: 17 additions & 42 deletions src/FontAwesomeBladeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Devlop\FontAwesome\Components\Duotone;
use Devlop\FontAwesome\Components\Light;
use Devlop\FontAwesome\Components\Regular;
use Devlop\FontAwesome\Components\SharpRegular;
use Devlop\FontAwesome\Components\SharpSolid;
use Devlop\FontAwesome\Components\Solid;
use Devlop\FontAwesome\Components\Thin;
use Illuminate\Support\Facades\Blade;
Expand All @@ -16,18 +18,6 @@

final class FontAwesomeBladeServiceProvider extends ServiceProvider
{
/**
* Get the services provided by the provider.
*
* @return array<class-string>
*/
public function provides() : array
{
return [
//
];
}

/**
* Register the service provider.
*/
Expand All @@ -41,7 +31,7 @@ public function register() : void
*/
public function boot() : void
{
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'fontawesome-blade');
$this->loadViewsFrom(realpath(__DIR__ . '/../resources/views'), 'fontawesome-blade');

$this->publishes(
[
Expand All @@ -50,12 +40,7 @@ public function boot() : void
'config',
);

// first check legacy config, then load the new config.
$config = $this->app['config']->get('fontawesome-blade') ?? $this->app['config']->get('fontawesome');

$path = array_key_exists('package', $config)
? rtrim($config['package'], '/') . '/svgs'
: $config['path'];
$path = $this->app['config']->get('fontawesome.path');

if (! is_string($path)) {
throw new RuntimeException(sprintf(
Expand All @@ -64,31 +49,21 @@ public function boot() : void
));
}

// if (! is_dir($path)) {
// throw new RuntimeException(sprintf(
// '"%1$s" is not a valid Font Awesome path.',
// $path,
// ));
// }

Blade::componentNamespace('Devlop\\FontAwesome\\Components', 'fa');

// legacy aliases
Blade::components([
Solid::class => 'fa.solid',
Regular::class => 'fa.regular',
Light::class => 'fa.light',
Thin::class => 'fa.thin',
Duotone::class => 'fa.duotone',
Brands::class => 'fa.brands',
]);

$this->app->when(Solid::class)->needs('$path')->give($path);
$this->app->when(Regular::class)->needs('$path')->give($path);
$this->app->when(Light::class)->needs('$path')->give($path);
$this->app->when(Thin::class)->needs('$path')->give($path);
$this->app->when(Duotone::class)->needs('$path')->give($path);
$this->app->when(Brands::class)->needs('$path')->give($path);
$this->app
->when([
Brands::class,
Duotone::class,
Light::class,
Regular::class,
Solid::class,
Thin::class,
SharpRegular::class,
SharpSolid::class,
])
->needs('$path')
->give($path);
}

private function getConfigPath(string $fileName) : string
Expand Down
142 changes: 85 additions & 57 deletions tests/ComponentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
use Devlop\FontAwesome\Components\Duotone;
use Devlop\FontAwesome\Components\Light;
use Devlop\FontAwesome\Components\Regular;
use Devlop\FontAwesome\Components\SharpRegular;
use Devlop\FontAwesome\Components\SharpSolid;
use Devlop\FontAwesome\Components\Solid;
use Devlop\FontAwesome\Components\Thin;
use Devlop\FontAwesome\FontAwesomeBaseComponent;
use Devlop\FontAwesome\FontAwesomeBladeServiceProvider;
use DirectoryIterator;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Orchestra\Testbench\TestCase;

Expand Down Expand Up @@ -45,46 +47,6 @@ protected function getEnvironmentSetUp($app) : void
);
}

/**
* (-pro) Component data provider.
*
* @return array<string,array<class-string>>
*/
public static function components() : array
{
return [
'brands' => [Brands::class],
'duotone' => [Duotone::class],
'light' => [Light::class],
'regular' => [Regular::class],
'solid' => [Solid::class],
'thin' => [Thin::class],
];
}

/**
* (-pro and -free) Component data provider.
*
* @return array<string,array<class-string>>
*/
public static function packageComponents() : array
{
$freePath = realpath(__DIR__ . '/../node_modules/@fortawesome/fontawesome-free/svgs');
$proPath = realpath(__DIR__ . '/../node_modules/@fortawesome/fontawesome-pro/svgs');

return [
'free-brands' => [$freePath, Brands::class],
'free-regular' => [$freePath, Regular::class],
'free-solid' => [$freePath, Solid::class],
'pro-brands' => [$proPath, Brands::class],
'pro-duotone' => [$proPath, Duotone::class],
'pro-light' => [$proPath, Light::class],
'pro-regular' => [$proPath, Regular::class],
'pro-solid' => [$proPath, Solid::class],
'pro-thin' => [$proPath, Thin::class],
];
}

/** @test */
public function brands_buffer_can_be_rendered() : void
{
Expand Down Expand Up @@ -130,43 +92,109 @@ public function duotone_user_crown_have_the_correct_classnames() : void

/**
* @test
* @dataProvider packageComponents
* @dataProvider caseInsensitiveOptionalPrefixedNamesProvider
*
* @param class-string $componentName
*/
public function names_are_case_insensitive_and_can_use_fa_dash_prefix_and_unprefixed(string $componentName, string $iconName) : void
{
$component = $this->app->make($componentName, [
'name' => $iconName,
]);

$this->assertIsString($this->renderComponent($component));
}

public function caseInsensitiveOptionalPrefixedNamesProvider() : array
{
$generatePermutations = function (string $componentClassName, array $iconNamePermutations) : array {
$permutations = [];

foreach ($iconNamePermutations as $iconNamePermutation) {
$key = Str::kebab(class_basename($componentClassName)) . ' ' . $iconNamePermutation;

$permutations[$key] = [
$componentClassName,
$iconNamePermutation,
];
}

return $permutations;
};

$iconNamePermutations = ['alien', 'Alien', 'ALIEN', 'alIEn', 'fa-alien', 'FA-ALIEN', 'Fa-AlieN'];

return array_merge(
$generatePermutations(Brands::class, ['hooli', 'Hooli', 'HOOLI', 'hoOLi', 'fa-hooli', 'FA-HOOLI', 'Fa-HoolI']),
$generatePermutations(Duotone::class, $iconNamePermutations),
$generatePermutations(Light::class, $iconNamePermutations),
$generatePermutations(Regular::class, $iconNamePermutations),
$generatePermutations(Solid::class, $iconNamePermutations),
$generatePermutations(Thin::class, $iconNamePermutations),
$generatePermutations(SharpRegular::class, $iconNamePermutations),
$generatePermutations(SharpSolid::class, $iconNamePermutations),
);

return $datasets;
}

/**
* @test
* @dataProvider packageComponentsProvider
*
* @param class-string $componentName
*/
public function all_icons_can_be_rendered(string $path, string $componentName) : void
{
$style = Str::of(class_basename($componentName))
->after('Fa')
->lower();
$style = (string) Str::of(class_basename($componentName))
->kebab();

$iconsPath = implode('/', [
$path,
$style,
]);

$icons = (new Collection(scandir($iconsPath)))
->filter(function (string $icon) : bool {
$ignore = [
'.',
'..',
];
foreach (new DirectoryIterator($iconsPath) as $icon) {
if ($icon->isDot()) {
continue;
}

return ! in_array($icon, $ignore, true);
})
->values()
->map(fn (string $icon) : string => Str::before($icon, '.svg'));
$iconName = Str::before($icon->getFileName(), '.svg');

foreach ($icons as $icon) {
$component = $this->app->make($componentName, [
'path' => $path,
'name' => $icon,
'name' => $iconName,
]);

$this->assertIsString($this->renderComponent($component));
}
}

/**
* (-pro and -free) Component data provider.
*
* @return array<string,array<class-string>>
*/
public static function packageComponentsProvider() : array
{
$freePath = realpath(__DIR__ . '/../node_modules/@fortawesome/fontawesome-free/svgs');
$proPath = realpath(__DIR__ . '/../node_modules/@fortawesome/fontawesome-pro/svgs');

return [
'free-brands' => [$freePath, Brands::class],
'free-regular' => [$freePath, Regular::class],
'free-solid' => [$freePath, Solid::class],
'pro-brands' => [$proPath, Brands::class],
'pro-duotone' => [$proPath, Duotone::class],
'pro-light' => [$proPath, Light::class],
'pro-regular' => [$proPath, Regular::class],
'pro-solid' => [$proPath, Solid::class],
'pro-thin' => [$proPath, Thin::class],
'pro-sharp-regular' => [$proPath, SharpRegular::class],
'pro-sharp-solid' => [$proPath, SharpSolid::class],
];
}

/**
* Render a component and return the output.
*
Expand Down
Loading

0 comments on commit 984da45

Please sign in to comment.