Skip to content

Commit

Permalink
Welcome Fusion
Browse files Browse the repository at this point in the history
  • Loading branch information
roberto-butti committed Apr 5, 2024
1 parent f2ca9d7 commit 81b1f79
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 54 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ jobs:
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
- name: Run tests
run: |
composer run test
114 changes: 61 additions & 53 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,56 +1,64 @@
{
"name": "hi-folks/fusion",
"description": "Empowering Markdown and Frontmatter for Structured, Database-free Content Management",
"keywords": [
"hi-folks",
"fusion",
"markdown",
"eloquent",
"laravel"
],
"homepage": "https://github.com/hi-folks/fusion",
"license": "MIT",
"type": "library",
"authors": [
{
"name": "Roberto B",
"email": "[email protected]",
"role": "Developer"
}
],
"require": {
"php": "^8.2|^8.3",
"illuminate/support": "^11.0"
},
"require-dev": {
"orchestra/testbench": "^9.0"
},
"autoload": {
"psr-4": {
"HiFolks\\Fusion\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"HiFolks\\Fusion\\Tests\\": "tests"
}
},
"scripts": {
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"

},
"config": {
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"HiFolks\\Fusion\\FusionServiceProvider"
],
"aliases": {
"Fusion": "HiFolks\\Fusion\\FusionFacade"
}
}
"name": "hi-folks/fusion",
"description": "Laravel package that enhances Eloquent models to facilitate the management of structured, database-free content through Markdown files with Frontmatter.",
"keywords": [
"hi-folks",
"fusion",
"markdown",
"eloquent",
"laravel"
],
"homepage": "https://github.com/hi-folks/fusion",
"license": "MIT",
"type": "library",
"authors": [
{
"name": "Roberto B",
"email": "[email protected]",
"role": "Developer"
}
],
"require": {
"php": "^8.2|^8.3",
"illuminate/support": "^11.0",
"calebporzio/sushi": "^2.5",
"league/commonmark": "^2.4",
"spatie/yaml-front-matter": "^2.0"
},
"require-dev": {
"laravel/pint": "^1.13",
"orchestra/testbench": "^9.0",
"pestphp/pest": "^2.34"
},
"autoload": {
"psr-4": {
"HiFolks\\Fusion\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"HiFolks\\Fusion\\Tests\\": "tests"
}
},
"scripts": {
"style-fix": "vendor/bin/pint",
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest coverage"
},
"config": {
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"extra": {
"laravel": {
"providers": [
"HiFolks\\Fusion\\FusionServiceProvider"
],
"aliases": {
"Fusion": "HiFolks\\Fusion\\FusionFacade"
}
}
}
}
2 changes: 1 addition & 1 deletion config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
*/
return [

];
];
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./app</directory>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
90 changes: 90 additions & 0 deletions src/Models/FusionBaseModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace HiFolks\Fusion\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\MarkdownConverter;
use Spatie\YamlFrontMatter\YamlFrontMatter;

abstract class FusionBaseModel extends Model
{
public function getResourceFolder(): string
{

$folderName = str_replace(
'App\\Models\\',
'',
get_called_class()
);
$folderName = Str::snake($folderName);

return 'content'.DIRECTORY_SEPARATOR.$folderName;
}

public function getRouteKeyName()
{
return 'slug';
}

public function frontmatterFields(): array
{
return [
'title',
];
}

public function getRows()
{
return $this->getFrontmatterRows(
$this->frontmatterFields()
);
}

public function getFrontmatterRows($columns = [])
{

$environment = (new Environment())
->addExtension(new CommonMarkCoreExtension());
$converter = new MarkdownConverter($environment);

$filesystem = new FileSystem();
$markdowns = [];

foreach (File::allFiles(resource_path($this->getResourceFolder())) as $file) {
$slug = $file->getFilenameWithoutExtension();

$filename = $file->getRelativePathName();
$fileContent = $filesystem->get($file->getRealPath());

$object = YamlFrontMatter::parse($fileContent);

$row = [
'title' => $object->matter('title'),
'label' => $object->matter('label'),
'excerpt' => $object->matter('excerpt'),
'date' => Carbon::createFromTimestamp($object->matter('date'))->format('Y-m-d'),
'slug' => $slug,
'body' => $converter->convert($object->body()),
'real_path' => $file->getRealPath(),
'relative_path_name' => $file->getRelativePathname(),
'published' => $object->matter('published'),
'highlight' => $object->matter('highlight'),

];

foreach ($columns as $column) {
$row[$column] = $object->matter($column);
}

$markdowns[] = $row;
}

return $markdowns;
}
}
5 changes: 5 additions & 0 deletions tests/Feature/ExampleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

test('example', function () {
expect(true)->toBeTrue();
});
45 changes: 45 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/

// uses(Tests\TestCase::class)->in('Feature');

/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/

expect()->extend('toBeOne', function () {
return $this->toBe(1);
});

/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/

function something()
{
// ..
}
10 changes: 10 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Tests;

use PHPUnit\Framework\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
//
}
5 changes: 5 additions & 0 deletions tests/Unit/ExampleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

test('example', function () {
expect(true)->toBeTrue();
});

0 comments on commit 81b1f79

Please sign in to comment.