From e2d25f173746eea0b0b7de36690be1d65c0689fd Mon Sep 17 00:00:00 2001 From: Awilum Date: Fri, 16 Oct 2020 14:20:27 +0300 Subject: [PATCH] feat(core): use Atomastic Components #478 --- composer.json | 13 +++++++--- src/flextype/Foundation/Entries/Entries.php | 11 +++----- .../Entries/Fields/CreatedAtField.php | 2 +- .../Foundation/Entries/Fields/IdField.php | 4 +-- .../Foundation/Entries/Fields/SlugField.php | 4 +-- src/flextype/Support/Parsers/Markdown.php | 4 +-- src/flextype/Support/Parsers/Shortcode.php | 4 +-- .../Support/Serializers/Frontmatter.php | 10 ++++---- src/flextype/Support/Serializers/Json.php | 4 +-- src/flextype/Support/Serializers/Yaml.php | 4 +-- src/flextype/bootstrap.php | 4 +-- src/flextype/dependencies.php | 25 +++++++++++++------ 12 files changed, 50 insertions(+), 39 deletions(-) diff --git a/composer.json b/composer.json index 17259b00..7d974aa8 100755 --- a/composer.json +++ b/composer.json @@ -27,10 +27,14 @@ "doctrine/collections": "~1.6.7", "flextype-components/arrays" : "3.0.1", - "flextype-components/strings" : "1.2.0", "flextype-components/filesystem" : "2.0.8", "flextype-components/i18n" : "1.3.0", - "flextype-components/registry" : "3.0.0", + + "atomastic/session": "^1.0.0", + "atomastic/arrays": "^1.0.0", + "atomastic/filesystem": "^1.0.0", + "atomastic/registry": "^1.0.0", + "atomastic/strings": "^1.0.0", "slim/slim": "~3.12.3", @@ -83,9 +87,10 @@ ] }, "require-dev": { - "doctrine/coding-standard": "8.1.0", "victorjonsson/markdowndocs": "~1.3.8", - "phpstan/phpstan": "~0.12.42", + "doctrine/coding-standard": "8.1.0", + "pestphp/pest": "^0.3.3", + "phpstan/phpstan": "^0.12.42", "symfony/var-dumper": "~5.1.5" } } diff --git a/src/flextype/Foundation/Entries/Entries.php b/src/flextype/Foundation/Entries/Entries.php index 6d089ca9..cc60e568 100755 --- a/src/flextype/Foundation/Entries/Entries.php +++ b/src/flextype/Foundation/Entries/Entries.php @@ -267,13 +267,10 @@ class Entries $entry_dir = $this->getDirLocation($this->storage['create']['id']); - if (! Filesystem::has($entry_dir)) { - // Try to create directory for new entry - if (Filesystem::createDir($entry_dir)) { - // Check if new entry file exists - if (! Filesystem::has($entry_file = $entry_dir . '/entry' . '.' . flextype('registry')->get('flextype.settings.entries.extension'))) { - // Create a new entry! - return Filesystem::write($entry_file, flextype('frontmatter')->encode($this->storage['create']['data'])); + if (! flextype('filesystem')->directory($entry_dir)->exists()) { + if (flextype('filesystem')->directory($entry_dir)->create()) { + if (! flextype('filesystem')->file($entry_file = $entry_dir . '/entry' . '.' . flextype('registry')->get('flextype.settings.entries.extension'))->exists()) { + return (bool) flextype('filesystem')->file($entry_file)->put(flextype('frontmatter')->encode($this->storage['create']['data'])); } return false; diff --git a/src/flextype/Foundation/Entries/Fields/CreatedAtField.php b/src/flextype/Foundation/Entries/Fields/CreatedAtField.php index b5e61d82..6fcadc04 100644 --- a/src/flextype/Foundation/Entries/Fields/CreatedAtField.php +++ b/src/flextype/Foundation/Entries/Fields/CreatedAtField.php @@ -8,7 +8,7 @@ declare(strict_types=1); */ use Flextype\Component\Filesystem\Filesystem; -use Flextype\Component\Strings\Strings; +use Atomastic\Strings\Strings; if (flextype('registry')->get('flextype.settings.entries.fields.created_at.enabled')) { flextype('emitter')->addListener('onEntryAfterInitialized', static function (): void { diff --git a/src/flextype/Foundation/Entries/Fields/IdField.php b/src/flextype/Foundation/Entries/Fields/IdField.php index 600b531d..c8c3e34d 100644 --- a/src/flextype/Foundation/Entries/Fields/IdField.php +++ b/src/flextype/Foundation/Entries/Fields/IdField.php @@ -7,12 +7,12 @@ declare(strict_types=1); * Founded by Sergey Romanenko and maintained by Flextype Community. */ -use Flextype\Component\Strings\Strings; +use Atomastic\Strings\Strings; if (flextype('registry')->get('flextype.settings.entries.fields.id.enabled')) { flextype('emitter')->addListener('onEntryAfterInitialized', static function (): void { if (flextype('entries')->getStorage('fetch_single.data.id') == null) { - flextype('entries')->setStorage('fetch_single.data.id', (string) Strings::trimSlashes(flextype('entries')->getStorage('fetch_single.id'))); + flextype('entries')->setStorage('fetch_single.data.id', (string) Strings::create(flextype('entries')->getStorage('fetch_single.id'))->trimSlashes()); } }); } diff --git a/src/flextype/Foundation/Entries/Fields/SlugField.php b/src/flextype/Foundation/Entries/Fields/SlugField.php index cf3f8e34..9bfa3288 100644 --- a/src/flextype/Foundation/Entries/Fields/SlugField.php +++ b/src/flextype/Foundation/Entries/Fields/SlugField.php @@ -7,12 +7,12 @@ declare(strict_types=1); * Founded by Sergey Romanenko and maintained by Flextype Community. */ -use Flextype\Component\Strings\Strings; +use Atomastic\Strings\Strings; if (flextype('registry')->get('flextype.settings.entries.fields.slug.enabled')) { flextype('emitter')->addListener('onEntryAfterInitialized', static function (): void { if (flextype('entries')->getStorage('fetch_single.data.slug') == null) { - $parts = Strings::segments(Strings::trimSlashes(flextype('entries')->getStorage('fetch_single.id')), '/'); + $parts = Strings::create(flextype('entries')->getStorage('fetch_single.id'))->trimSlashes()->segments(); flextype('entries')->setStorage('fetch_single.data.slug', (string) end($parts)); } }); diff --git a/src/flextype/Support/Parsers/Markdown.php b/src/flextype/Support/Parsers/Markdown.php index b2caeebe..c21b97ae 100644 --- a/src/flextype/Support/Parsers/Markdown.php +++ b/src/flextype/Support/Parsers/Markdown.php @@ -9,7 +9,7 @@ declare(strict_types=1); namespace Flextype\Support\Parsers; -use Flextype\Component\Strings\Strings; +use Atomastic\Strings\Strings; class Markdown { @@ -74,6 +74,6 @@ class Markdown public function getCacheID($input): string { - return Strings::hash('markdown' . $input); + return Strings::create('markdown' . $input)->hash()->toString(); } } diff --git a/src/flextype/Support/Parsers/Shortcode.php b/src/flextype/Support/Parsers/Shortcode.php index c0ef954d..1d6ea66a 100644 --- a/src/flextype/Support/Parsers/Shortcode.php +++ b/src/flextype/Support/Parsers/Shortcode.php @@ -9,7 +9,7 @@ declare(strict_types=1); namespace Flextype\Support\Parsers; -use Flextype\Component\Strings\Strings; +use Atomastic\Strings\Strings; class Shortcode { @@ -113,6 +113,6 @@ class Shortcode */ public function getCacheID(string $input): string { - return Strings::hash('shortcode' . $input); + return Strings::create('shortcode' . $input)->hash()->toString(); } } diff --git a/src/flextype/Support/Serializers/Frontmatter.php b/src/flextype/Support/Serializers/Frontmatter.php index 74014d7c..f8c99f0e 100644 --- a/src/flextype/Support/Serializers/Frontmatter.php +++ b/src/flextype/Support/Serializers/Frontmatter.php @@ -10,7 +10,7 @@ declare(strict_types=1); namespace Flextype\Support\Serializers; use Flextype\Component\Arrays\Arrays; -use Flextype\Component\Strings\Strings; +use Atomastic\Strings\Strings; use function array_slice; use function count; @@ -93,17 +93,17 @@ class Frontmatter $input = (string) preg_replace("/(\r\n|\r)/", "\n", $input); // Parse Frontmatter and Body - $parts = preg_split('/^[\s\r\n]?---[\s\r\n]?$/sm', PHP_EOL . Strings::trimLeft($input)); + $parts = preg_split('/^[\s\r\n]?---[\s\r\n]?$/sm', PHP_EOL . Strings::create($input)->trimLeft()->toString()); if (count($parts) < 3) { - return ['content' => Strings::trim($input)]; + return ['content' => Strings::create($input)->trim()->toString()]; } - return flextype('yaml')->decode(Strings::trim($parts[1]), false) + ['content' => Strings::trim(implode(PHP_EOL . '---' . PHP_EOL, array_slice($parts, 2)))]; + return flextype('yaml')->decode(Strings::create($parts[1])->trim()->toString(), false) + ['content' => Strings::create(implode(PHP_EOL . '---' . PHP_EOL, array_slice($parts, 2)))->trim()->toString()]; } protected function getCacheID($input): string { - return Strings::hash('frontmatter' . $input); + return Strings::create('frontmatter' . $input)->hash()->toString(); } } diff --git a/src/flextype/Support/Serializers/Json.php b/src/flextype/Support/Serializers/Json.php index 8ddcb472..f6360c7f 100644 --- a/src/flextype/Support/Serializers/Json.php +++ b/src/flextype/Support/Serializers/Json.php @@ -9,7 +9,7 @@ declare(strict_types=1); namespace Flextype\Support\Serializers; -use Flextype\Component\Strings\Strings; +use Atomastic\Strings\Strings; use RuntimeException; use function defined; @@ -109,6 +109,6 @@ class Json protected function getCacheID($input): string { - return Strings::hash('json' . $input); + return Strings::create('json' . $input)->hash()->toString(); } } diff --git a/src/flextype/Support/Serializers/Yaml.php b/src/flextype/Support/Serializers/Yaml.php index 3cef5b7a..26f83a83 100644 --- a/src/flextype/Support/Serializers/Yaml.php +++ b/src/flextype/Support/Serializers/Yaml.php @@ -9,7 +9,7 @@ declare(strict_types=1); namespace Flextype\Support\Serializers; -use Flextype\Component\Strings\Strings; +use Atomastic\Strings\Strings; use RuntimeException; use Symfony\Component\Yaml\Exception\DumpException as SymfonyYamlDumpException; use Symfony\Component\Yaml\Exception\ParseException as SymfonyYamlParseException; @@ -142,6 +142,6 @@ class Yaml protected function getCacheID($input): string { - return Strings::hash('yaml' . $input); + return Strings::create('yaml' . $input)->hash()->toString(); } } diff --git a/src/flextype/bootstrap.php b/src/flextype/bootstrap.php index 12229a75..d984236e 100755 --- a/src/flextype/bootstrap.php +++ b/src/flextype/bootstrap.php @@ -9,7 +9,7 @@ declare(strict_types=1); namespace Flextype; -use Flextype\Component\Registry\Registry; +use Atomastic\Registry\Registry; use Flextype\Foundation\Flextype; use Slim\Http\Environment; use Slim\Http\Uri; @@ -32,7 +32,7 @@ use function ucwords; /** * Init Registry */ -$registry = new Registry(); +$registry = Registry::getInstance(); /** * Preflight the Flextype diff --git a/src/flextype/dependencies.php b/src/flextype/dependencies.php index a712db04..ba0e5ab1 100644 --- a/src/flextype/dependencies.php +++ b/src/flextype/dependencies.php @@ -11,8 +11,9 @@ namespace Flextype; use Bnf\Slim3Psr15\CallableResolver; use Cocur\Slugify\Slugify; -use Flextype\Component\Strings\Strings; -use Odan\Session\PhpSession; +use Atomastic\Strings\Strings; +use Atomastic\Session\Session; +use Atomastic\Filesystem\Filesystem; use Flextype\Foundation\Cors; use Flextype\Foundation\Entries\Entries; use Flextype\Foundation\Media\MediaFiles; @@ -28,7 +29,7 @@ use Flextype\Support\Serializers\Yaml; use Intervention\Image\ImageManager; use League\Event\Emitter; use League\Flysystem\Adapter\Local; -use League\Flysystem\Filesystem; +use League\Flysystem\Filesystem as Flysystem; use League\Glide\Api\Api; use League\Glide\Manipulators\Background; use League\Glide\Manipulators\Blur; @@ -58,11 +59,19 @@ use function extension_loaded; use function in_array; use function sys_get_temp_dir; + +/** + * Add filesystem service to Flextype container + */ +flextype()->container()['filesystem'] = static function () { + return new Filesystem(); +}; + /** * Create a standard session hanndler */ flextype()->container()['session'] = static function () { - return new PhpSession(); + return new Session(); }; /** @@ -124,7 +133,7 @@ flextype()->container()['cache'] = static function () { if ($key === 'path' && in_array($driver_name, ['files', 'sqlite', 'leveldb'])) { $config['path'] = ! empty($value) ? PATH['tmp'] . '/' . $value : sys_get_temp_dir(); } else { - $config[Strings::camel($key)] = $value; + $config[Strings::create($key)->camel()->toString()] = $value; } } @@ -267,17 +276,17 @@ flextype()->container()['images'] = static function () { $imagesSettings = ['driver' => flextype('registry')->get('flextype.settings.image.driver')]; // Set source filesystem - $source = new Filesystem( + $source = new Flysystem( new Local(PATH['project'] . '/uploads/entries/') ); // Set cache filesystem - $cache = new Filesystem( + $cache = new Flysystem( new Local(PATH['tmp'] . '/glide') ); // Set watermarks filesystem - $watermarks = new Filesystem( + $watermarks = new Flysystem( new Local(PATH['project'] . '/watermarks') );