mirror of
https://github.com/flextype/flextype.git
synced 2025-08-08 14:16:46 +02:00
feat(media-folders): add fetch() method with ability to execute different methods with fetch prefix. #509
This commit is contained in:
@@ -206,75 +206,72 @@ class MediaFiles
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch single file.
|
||||
* Fetch.
|
||||
*
|
||||
* @param string $path The path to file.
|
||||
* @param string $id The path to file.
|
||||
* @param array $options Options array.
|
||||
*
|
||||
* @access public
|
||||
*
|
||||
* @return self Returns instance of The Arrays class.
|
||||
*/
|
||||
public function fetchSingle(string $path, array $options = []): Arrays
|
||||
public function fetch(string $id, array $options = []): Arrays
|
||||
{
|
||||
$result = [];
|
||||
// Run event: onEntriesFetch
|
||||
flextype('emitter')->emit('onMediaFilesFetch');
|
||||
|
||||
if (filesystem()->file(flextype('media_files_meta')->getFileMetaLocation($path))->exists()) {
|
||||
$result = flextype('yaml')->decode(filesystem()->file(flextype('media_files_meta')->getFileMetaLocation($path))->get());
|
||||
if (isset($options['collection']) &&
|
||||
strings($options['collection'])->isTrue()) {
|
||||
$result = [];
|
||||
|
||||
$result['filename'] = pathinfo(str_replace('/.meta', '', flextype('media_files_meta')->getFileMetaLocation($path)))['filename'];
|
||||
$result['basename'] = explode('.', basename(flextype('media_files_meta')->getFileMetaLocation($path)))[0];
|
||||
$result['extension'] = ltrim(strstr($path, '.'), '.');
|
||||
$result['dirname'] = pathinfo(str_replace('/.meta', '', flextype('media_files_meta')->getFileMetaLocation($path)))['dirname'];
|
||||
foreach (filesystem()->find()->files()->in(flextype('media_folders_meta')->getDirectoryMetaLocation($id)) as $file) {
|
||||
$basename = $file->getBasename('.' . $file->getExtension());
|
||||
|
||||
$result['url'] = 'project/uploads/' . $path;
|
||||
$result[$basename] = flextype('yaml')->decode(filesystem()->file($file->getPathname())->get());
|
||||
$result[$basename]['filename'] = pathinfo(str_replace('/.meta', '', flextype('media_files_meta')->getFileMetaLocation($basename)))['filename'];
|
||||
$result[$basename]['basename'] = explode('.', basename(flextype('media_files_meta')->getFileMetaLocation($basename)))[0];
|
||||
$result[$basename]['extension'] = ltrim(strstr($basename, '.'), '.');
|
||||
$result[$basename]['dirname'] = pathinfo(str_replace('/.meta', '', $file->getPathname()))['dirname'];
|
||||
$result[$basename]['url'] = 'project/uploads/' . $id . '/' . $basename;
|
||||
|
||||
if (flextype('registry')->has('flextype.settings.url') && flextype('registry')->get('flextype.settings.url') !== '') {
|
||||
$fullUrl = flextype('registry')->get('flextype.settings.url');
|
||||
} else {
|
||||
$fullUrl = Uri::createFromEnvironment(new Environment($_SERVER))->getBaseUrl();
|
||||
if (flextype('registry')->has('flextype.settings.url') && flextype('registry')->get('flextype.settings.url') !== '') {
|
||||
$fullUrl = flextype('registry')->get('flextype.settings.url');
|
||||
} else {
|
||||
$fullUrl = Uri::createFromEnvironment(new Environment($_SERVER))->getBaseUrl();
|
||||
}
|
||||
|
||||
$result[$basename]['full_url'] = $fullUrl . '/project/uploads/' . $id . '/' . $basename;
|
||||
}
|
||||
|
||||
$result = filter($result, $options);
|
||||
|
||||
return arrays($result);
|
||||
} else {
|
||||
$result = [];
|
||||
|
||||
if (filesystem()->file(flextype('media_files_meta')->getFileMetaLocation($id))->exists()) {
|
||||
$result = flextype('yaml')->decode(filesystem()->file(flextype('media_files_meta')->getFileMetaLocation($id))->get());
|
||||
|
||||
$result['filename'] = pathinfo(str_replace('/.meta', '', flextype('media_files_meta')->getFileMetaLocation($id)))['filename'];
|
||||
$result['basename'] = explode('.', basename(flextype('media_files_meta')->getFileMetaLocation($id)))[0];
|
||||
$result['extension'] = ltrim(strstr($id, '.'), '.');
|
||||
$result['dirname'] = pathinfo(str_replace('/.meta', '', flextype('media_files_meta')->getFileMetaLocation($id)))['dirname'];
|
||||
|
||||
$result['url'] = 'project/uploads/' . $id;
|
||||
|
||||
if (flextype('registry')->has('flextype.settings.url') && flextype('registry')->get('flextype.settings.url') !== '') {
|
||||
$fullUrl = flextype('registry')->get('flextype.settings.url');
|
||||
} else {
|
||||
$fullUrl = Uri::createFromEnvironment(new Environment($_SERVER))->getBaseUrl();
|
||||
}
|
||||
|
||||
$result['full_url'] = $fullUrl . '/project/uploads/' . $id;
|
||||
}
|
||||
|
||||
$result['full_url'] = $fullUrl . '/project/uploads/' . $path;
|
||||
$result = filter($result, $options);
|
||||
|
||||
return arrays($result);
|
||||
}
|
||||
|
||||
$result = filter($result, $options);
|
||||
|
||||
return arrays($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch files collection.
|
||||
*
|
||||
* @param string $path Unique identifier of the files collecton.
|
||||
* @param array $options Options array.
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function fetchCollection(string $path, array $options = []): Arrays
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach (filesystem()->find()->files()->in(flextype('media_folders_meta')->getDirectoryMetaLocation($path)) as $file) {
|
||||
$basename = $file->getBasename('.' . $file->getExtension());
|
||||
|
||||
$result[$basename] = flextype('yaml')->decode(filesystem()->file($file->getPathname())->get());
|
||||
$result[$basename]['filename'] = pathinfo(str_replace('/.meta', '', flextype('media_files_meta')->getFileMetaLocation($basename)))['filename'];
|
||||
$result[$basename]['basename'] = explode('.', basename(flextype('media_files_meta')->getFileMetaLocation($basename)))[0];
|
||||
$result[$basename]['extension'] = ltrim(strstr($basename, '.'), '.');
|
||||
$result[$basename]['dirname'] = pathinfo(str_replace('/.meta', '', $file->getPathname()))['dirname'];
|
||||
$result[$basename]['url'] = 'project/uploads/' . $path . '/' . $basename;
|
||||
|
||||
if (flextype('registry')->has('flextype.settings.url') && flextype('registry')->get('flextype.settings.url') !== '') {
|
||||
$fullUrl = flextype('registry')->get('flextype.settings.url');
|
||||
} else {
|
||||
$fullUrl = Uri::createFromEnvironment(new Environment($_SERVER))->getBaseUrl();
|
||||
}
|
||||
|
||||
$result[$basename]['full_url'] = $fullUrl . '/project/uploads/' . $path . '/' . $basename;
|
||||
}
|
||||
|
||||
$result = filter($result, $options);
|
||||
|
||||
return arrays($result);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -25,55 +25,57 @@ class MediaFolders
|
||||
use Macroable;
|
||||
|
||||
/**
|
||||
* Fetch single folder.
|
||||
* Fetch.
|
||||
*
|
||||
* @param string $path The path to folder.
|
||||
* @param string $id The path to folder.
|
||||
* @param array $options Options array.
|
||||
*
|
||||
* @access public
|
||||
*
|
||||
* @return self Returns instance of The Arrays class.
|
||||
*/
|
||||
public function fetchSingle(string $path, array $options = []): Arrays
|
||||
public function fetch(string $id, array $options = []): Arrays
|
||||
{
|
||||
$result = [];
|
||||
// Run event: onEntriesFetch
|
||||
flextype('emitter')->emit('onMediaFoldersFetch');
|
||||
|
||||
if (filesystem()->directory(flextype('media_folders_meta')->getDirectoryMetaLocation($path))->exists()) {
|
||||
$result['path'] = $path;
|
||||
$result['full_path'] = str_replace('/.meta', '', flextype('media_folders_meta')->getDirectoryMetaLocation($path));
|
||||
$result['url'] = 'project/uploads/' . $path;
|
||||
// Single fetch helper
|
||||
$single = function ($id, $options) {
|
||||
$result = [];
|
||||
|
||||
if (flextype('registry')->has('flextype.settings.url') && flextype('registry')->get('flextype.settings.url') !== '') {
|
||||
$fullUrl = flextype('registry')->get('flextype.settings.url');
|
||||
} else {
|
||||
$fullUrl = Uri::createFromEnvironment(new Environment($_SERVER))->getBaseUrl();
|
||||
if (filesystem()->directory(flextype('media_folders_meta')->getDirectoryMetaLocation($id))->exists()) {
|
||||
$result['path'] = $id;
|
||||
$result['full_path'] = str_replace('/.meta', '', flextype('media_folders_meta')->getDirectoryMetaLocation($id));
|
||||
$result['url'] = 'project/uploads/' . $id;
|
||||
|
||||
if (flextype('registry')->has('flextype.settings.url') && flextype('registry')->get('flextype.settings.url') !== '') {
|
||||
$fullUrl = flextype('registry')->get('flextype.settings.url');
|
||||
} else {
|
||||
$fullUrl = Uri::createFromEnvironment(new Environment($_SERVER))->getBaseUrl();
|
||||
}
|
||||
|
||||
$result['full_url'] = $fullUrl . '/project/uploads/' . $id;
|
||||
}
|
||||
|
||||
$result['full_url'] = $fullUrl . '/project/uploads/' . $path;
|
||||
$result = filter($result, $options);
|
||||
|
||||
return arrays($result);
|
||||
};
|
||||
|
||||
if (isset($options['collection']) &&
|
||||
strings($options['collection'])->isTrue()) {
|
||||
$result = [];
|
||||
|
||||
foreach (filesystem()->find()->directories()->in(flextype('media_folders_meta')->getDirectoryMetaLocation($id)) as $folder) {
|
||||
$result[$folder->getFilename()] = $single($id . '/' . $folder->getFilename(), [])->toArray();
|
||||
}
|
||||
|
||||
$result = filter($result, $options);
|
||||
|
||||
return arrays($result);
|
||||
} else {
|
||||
return $single($id, $options);
|
||||
}
|
||||
|
||||
$result = filter($result, $options);
|
||||
|
||||
return arrays($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch folders collection.
|
||||
*
|
||||
* @param string $path The path to folder.
|
||||
* @param array $options Options array.
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function fetchCollection(string $path, array $options = []): Arrays
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach (filesystem()->find()->directories()->in(flextype('media_folders_meta')->getDirectoryMetaLocation($path)) as $folder) {
|
||||
$result[$folder->getFilename()] = $this->fetchSingle($path . '/' . $folder->getFilename())->toArray();
|
||||
}
|
||||
|
||||
$result = filter($result, $options);
|
||||
|
||||
return arrays($result);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -12,24 +12,17 @@ afterEach(function (): void {
|
||||
filesystem()->directory(PATH['project'] . '/uploads')->delete();
|
||||
});
|
||||
|
||||
test('test fetchSingle() method', function () {
|
||||
test('test fetch() method', function () {
|
||||
filesystem()->file(PATH['project'] . '/uploads/foo.txt')->put('foo');
|
||||
filesystem()->file(PATH['project'] . '/uploads/.meta/foo.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Foo', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
|
||||
filesystem()->file(PATH['project'] . '/uploads/bar.txt')->put('foo');
|
||||
filesystem()->file(PATH['project'] . '/uploads/.meta/bar.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Bar', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
|
||||
|
||||
$this->assertTrue(count(flextype('media_files')->fetchSingle('foo.txt')) > 0);
|
||||
$this->assertEquals('Foo', flextype('media_files')->fetchSingle('foo.txt')['title']);
|
||||
});
|
||||
$this->assertTrue(count(flextype('media_files')->fetch('foo.txt')) > 0);
|
||||
$this->assertEquals('Foo', flextype('media_files')->fetch('foo.txt')['title']);
|
||||
|
||||
test('test fetchCollection() method', function () {
|
||||
filesystem()->file(PATH['project'] . '/uploads/foo.txt')->put('foo');
|
||||
filesystem()->file(PATH['project'] . '/uploads/.meta/foo.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Foo', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
|
||||
filesystem()->file(PATH['project'] . '/uploads/bar.txt')->put('foo');
|
||||
filesystem()->file(PATH['project'] . '/uploads/.meta/bar.txt.yaml')->put(flextype('yaml')->encode(['title' => 'Bar', 'description' => '', 'type' => 'text/plain', 'filesize' => 3, 'uploaded_on' => 1603090370, 'exif' => []]));
|
||||
|
||||
$this->assertTrue(count(flextype('media_files')->fetchCollection('/')) == 2);
|
||||
$this->assertEquals('Foo', flextype('media_files')->fetchCollection('/')['foo.txt']['title']);
|
||||
$this->assertTrue(count(flextype('media_files')->fetch('/', ['collection' => true])) == 2);
|
||||
$this->assertEquals('Foo', flextype('media_files')->fetch('/', ['collection' => true])['foo.txt']['title']);
|
||||
});
|
||||
|
||||
test('test move() method', function () {
|
||||
|
@@ -13,18 +13,12 @@ afterEach(function (): void {
|
||||
});
|
||||
|
||||
|
||||
test('test fetchCollection() method', function () {
|
||||
test('test fetch() method', function () {
|
||||
$this->assertTrue(flextype('media_folders')->create('foo'));
|
||||
$this->assertTrue(flextype('media_folders')->create('foo/bar'));
|
||||
$this->assertTrue(flextype('media_folders')->create('foo/zed'));
|
||||
$this->assertTrue(count(flextype('media_folders')->fetchCollection('foo')) == 2);
|
||||
});
|
||||
|
||||
test('test fetchSingle() method', function () {
|
||||
$this->assertTrue(flextype('media_folders')->create('foo'));
|
||||
$this->assertTrue(flextype('media_folders')->create('foo/bar'));
|
||||
$this->assertTrue(flextype('media_folders')->create('foo/zed'));
|
||||
$this->assertTrue(count(flextype('media_folders')->fetchSingle('foo')) > 0);
|
||||
$this->assertTrue(count(flextype('media_folders')->fetch('foo', ['collection' => true])) == 2);
|
||||
$this->assertTrue(count(flextype('media_folders')->fetch('foo')) > 0);
|
||||
});
|
||||
|
||||
test('test create() method', function () {
|
||||
|
Reference in New Issue
Block a user