Skip to content

Commit

Permalink
Merge pull request #83 from barryvdh/feat-v3
Browse files Browse the repository at this point in the history
Remove cache, change util, bump to v3
  • Loading branch information
barryvdh authored Mar 15, 2022
2 parents 2a16de2 + 9ea6581 commit e58c1cf
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 93 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
php: [7.4, 8.0, 8.1]
php: [8.0, 8.1]
dependency-version: [prefer-lowest, prefer-stable]

name: PHP${{ matrix.php }} - ${{ matrix.dependency-version }}
Expand Down
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@
}
],
"require": {
"php": "^7.4|^8.0",
"php": "^8.0",
"studio-42/elfinder": "^2.1.60",
"league/flysystem": "^1.1",
"league/flysystem-cached-adapter": "^1.1",
"league/flysystem": "^3",
"intervention/image": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^9",
"squizlabs/php_codesniffer": "^3.5",
"phpstan/phpstan": "^1.4",
"league/flysystem-memory": "^1"
"league/flysystem-memory": "^3"
},
"suggest": {
"league/glide": "1.x to display images through Glide"
Expand All @@ -45,7 +44,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "0.2-dev"
"dev-master": "0.4-dev"
}
}
}
108 changes: 26 additions & 82 deletions src/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
use League\Flysystem\Cached\CachedAdapter;
use League\Flysystem\Cached\CacheInterface;
use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemOperator;
use League\Flysystem\Util;
use League\Flysystem\FilesystemInterface;
use League\Flysystem\Cached\Storage\Memory as MemoryStore;
use League\Flysystem\WhitespacePathNormalizer;
use League\Glide\Urls\UrlBuilderFactory;
use Barryvdh\elFinderFlysystemDriver\Plugin\GetUrl;
use Barryvdh\elFinderFlysystemDriver\Cache\SessionStore;
Expand All @@ -30,12 +32,9 @@ class Driver extends elFinderVolumeDriver
**/
protected $driverId = 'fls';

/** @var FilesystemInterface $fs */
/** @var FilesystemOperator $fs */
protected $fs;

/** @var CacheInterface $fs */
protected $fscache;

/** @var UrlBuilder $urlBuilder */
protected $urlBuilder = null;

Expand Down Expand Up @@ -72,40 +71,14 @@ public function mount(array $opts)
return parent::mount($opts);
}

protected function clearcache()
{
parent::clearcache();

// clear cached adapter cache
if ($this->fscache) {
$this->fscache->flush();
}
}

/**
* Find the icon based on the used Adapter
*
* @return string
*/
protected function getIcon()
{
try {
$adapter = $this->fs->getAdapter();
} catch (\Exception $e) {
$adapter = null;
}

if ($adapter instanceof CachedAdapter) {
$adapter = $adapter->getAdapter();
}

if ($adapter instanceof League\Flysystem\Adapter\AbstractFtpAdapter) {
$icon = 'volume_icon_ftp.png';
} elseif ($adapter instanceof League\Flysystem\Dropbox\DropboxAdapter) {
$icon = 'volume_icon_dropbox.png';
} else {
$icon = 'volume_icon_local.png';
}
$icon = 'volume_icon_local.png';

$parentUrl = defined('ELFINDER_IMG_PARENT_URL') ? (rtrim(ELFINDER_IMG_PARENT_URL, '/') . '/') : '';
return $parentUrl . 'img/' . $icon;
Expand All @@ -120,39 +93,11 @@ protected function getIcon()
protected function init()
{
$this->fs = $this->options['filesystem'];
if (!($this->fs instanceof FilesystemInterface)) {
return $this->setError('A filesystem instance is required');
}

if ($this->fs instanceof Filesystem) {
$adapter = $this->fs->getAdapter();

// If the Flysystem adapter already is a cache, try to determine the cache.
if ($adapter instanceof CachedAdapter) {
// Try to get the cache (method doesn't exist in all versions)
if (method_exists($adapter, 'getCache')) {
$this->fscache = $adapter->getCache();
} elseif ($this->options['fscache'] instanceof CacheInterface) {
$this->fscache = $this->options['fscache'];
}
} elseif ($this->options['cache']) {
switch ($this->options['cache']) {
case 'session':
$this->fscache = new SessionStore($this->session, 'fls_cache_' . $this->id);
break;
case 'memory':
$this->fscache = new MemoryStore();
break;
}

if ($this->fscache) {
$adapter = new CachedAdapter($adapter, $this->fscache);
$this->fs = new Filesystem($adapter, $this->fs->getConfig());
}
}
if (!($this->fs instanceof FilesystemOperator)) {
return $this->setError('A FilesystemOperator instance is required');
}

$this->fs->addPlugin(new GetUrl());
// $this->fs->addPlugin(new GetUrl());

$this->options['icon'] = $this->options['icon'] ?: (empty($this->options['rootCssClass'])? $this->getIcon() : '');
$this->root = $this->options['path'];
Expand All @@ -173,19 +118,6 @@ protected function init()
return true;
}

/**
* Configure after successful mount.
*
* @return void
**/
protected function configure()
{
parent::configure();

if ($this->fscache && $this->isMyReload()) {
$this->fscache->flush();
}
}

/**
* Return parent directory path
Expand All @@ -195,7 +127,7 @@ protected function configure()
**/
protected function _dirname($path)
{
return Util::dirname($path) ?: '/';
return dirname($path) ?: '/';
}

/**
Expand Down Expand Up @@ -278,8 +210,20 @@ protected function _stat($path)
}

try {
$meta = $this->fs->getMetadata($path);
$meta = [
'mimetype' => null,
'extension' => null,
'size' => null,
'type' => $this->fs->fileExists($path) ? 'file' : 'dir',
];

if ($meta['type'] === 'file') {
$meta['mimetype'] = $this->fs->mimeType($path);
$meta['timestamp'] = $this->fs->lastModified($path);
$meta['size'] = $this->fs->fileSize($path);
}
} catch (\Exception $e) {
var_dump($e->getMessage());
return array();
}

Expand Down Expand Up @@ -308,7 +252,7 @@ protected function _stat($path)
if(isset($meta['mimetype'])) {
$stat['mime'] = $meta['mimetype'];
} else {
$stat['mime'] = $this->fs->getMimetype($path);
$stat['mime'] = $this->fs->mimeType($path);
}

$imgMimes = ['image/jpeg', 'image/png', 'image/gif'];
Expand All @@ -325,9 +269,9 @@ protected function _stat($path)
}
}

if (!isset($stat['url']) && $this->fs->getUrl()) {
$stat['url'] = 1;
}
// if (!isset($stat['url']) && $this->fs->getUrl()) {
// $stat['url'] = 1;
// }

return $stat;
}
Expand Down Expand Up @@ -592,7 +536,7 @@ protected function _basename($path)
**/
protected function _joinPath($dir, $name)
{
return Util::normalizePath($dir . $this->separator . $name);
return (new WhitespacePathNormalizer())->normalizePath($dir . $this->separator . $name);
}

/**
Expand Down
8 changes: 3 additions & 5 deletions tests/DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

namespace Barryvdh\elFinderFlysystemDriver\Tests;

use Barryvdh\elFinderFlysystemDriver\Driver;
use League\Flysystem\Adapter\Local;
use League\Flysystem\InMemory\InMemoryFilesystemAdapter;
use PHPUnit\Framework\TestCase;
use League\Flysystem\Filesystem;
use League\Flysystem\Memory\MemoryAdapter;

class DriverTest extends TestCase
{
Expand All @@ -17,11 +15,11 @@ protected function setUp(): void
{
$_SERVER['REQUEST_METHOD'] = 'GET';

$adapter = new MemoryAdapter();
$adapter = new InMemoryFilesystemAdapter();
$filesystem = new Filesystem($adapter);

// Set a fake file
$filesystem->put('dir1/file1.txt', 'Hello!');
$filesystem->write('dir1/file1.txt', 'Hello!');

$this->filesystem = $filesystem;
}
Expand Down

0 comments on commit e58c1cf

Please sign in to comment.