-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from punktDeForks/feature/exchangeable-storage
FEATURE: Exchangeable proxy aware storages and targets
- Loading branch information
Showing
8 changed files
with
188 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Sitegeist\MagicWand\ResourceManagement; | ||
|
||
use Neos\Flow\Annotations as Flow; | ||
use Neos\Flow\Core\Bootstrap; | ||
use Neos\Flow\Mvc\Routing\UriBuilder; | ||
use Neos\Flow\ResourceManagement\ResourceManager; | ||
use Neos\Flow\ResourceManagement\Target\FileSystemTarget; | ||
use Sitegeist\MagicWand\Domain\Service\ConfigurationService; | ||
|
||
class ProxyAwareFileSystemTarget extends FileSystemTarget implements ProxyAwareTargetInterface | ||
{ | ||
use ProxyAwareTargetTrait; | ||
|
||
/** | ||
* @Flow\Inject | ||
* @var Bootstrap | ||
*/ | ||
protected $bootstrap; | ||
|
||
/** | ||
* @var UriBuilder | ||
* @Flow\Inject | ||
*/ | ||
protected $uriBuilder; | ||
|
||
/** | ||
* @var ResourceManager | ||
* @Flow\Inject | ||
*/ | ||
protected $resourceManager; | ||
|
||
/** | ||
* @var ConfigurationService | ||
* @Flow\Inject | ||
*/ | ||
protected $configurationService; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Sitegeist\MagicWand\ResourceManagement; | ||
|
||
use Neos\Flow\ResourceManagement\ResourceMetaDataInterface; | ||
|
||
interface ProxyAwareStorageInterface | ||
{ | ||
public function resourceIsPresentInStorage(ResourceMetaDataInterface $resource): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Sitegeist\MagicWand\ResourceManagement; | ||
|
||
|
||
interface ProxyAwareTargetInterface | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
namespace Sitegeist\MagicWand\ResourceManagement; | ||
|
||
use Neos\Flow\Annotations as Flow; | ||
use Neos\Flow\Core\Bootstrap; | ||
use Neos\Flow\Http\Exception as HttpException; | ||
use Neos\Flow\Http\HttpRequestHandlerInterface; | ||
use Neos\Flow\Mvc\ActionRequest; | ||
use Neos\Flow\Mvc\Routing\Exception\MissingActionNameException; | ||
use Neos\Flow\Mvc\Routing\UriBuilder; | ||
use Neos\Flow\ResourceManagement\CollectionInterface; | ||
use Neos\Flow\ResourceManagement\PersistentResource; | ||
use Neos\Flow\ResourceManagement\ResourceManager; | ||
use Neos\Flow\ResourceManagement\Storage\StorageObject; | ||
use Neos\Flow\ResourceManagement\Target\Exception; | ||
use Sitegeist\MagicWand\Domain\Service\ConfigurationService; | ||
|
||
trait ProxyAwareTargetTrait | ||
{ | ||
public function initializeObject() | ||
{ | ||
// initialize uriBuilder with request | ||
$requestHandler = $this->bootstrap->getActiveRequestHandler(); | ||
if ($requestHandler instanceof HttpRequestHandlerInterface) { | ||
$request = ActionRequest::fromHttpRequest($requestHandler->getComponentContext()->getHttpRequest()); | ||
$this->uriBuilder->setRequest($request); | ||
} | ||
parent::initializeObject(); | ||
} | ||
|
||
/** | ||
* @param CollectionInterface $collection The collection to publish | ||
* @param callable $callback Function called after each resource publishing | ||
* @return void | ||
*/ | ||
public function publishCollection(CollectionInterface $collection, callable $callback = null) | ||
{ | ||
if (!$this->configurationService->getCurrentConfigurationByPath('resourceProxy')) { | ||
parent::publishCollection($collection, $callback); | ||
return; | ||
} | ||
|
||
/** | ||
* @var ProxyAwareWritableFileSystemStorage $storage | ||
*/ | ||
$storage = $collection->getStorage(); | ||
if (!$storage instanceof ProxyAwareStorageInterface) { | ||
parent::publishCollection($collection, $callback); | ||
return; | ||
} | ||
|
||
foreach ($collection->getObjects($callback) as $object) { | ||
/** @var StorageObject $object */ | ||
if ($storage->resourceIsPresentInStorage($object) === false) { | ||
// this storage ignores resources that are not yet in the filesystem as they | ||
// are optimistically created during read operations | ||
continue; | ||
} | ||
$sourceStream = $object->getStream(); | ||
$this->publishFile($sourceStream, $this->getRelativePublicationPathAndFilename($object)); | ||
fclose($sourceStream); | ||
} | ||
} | ||
|
||
/** | ||
* @param PersistentResource $resource | ||
* @return string | ||
* @throws Exception | ||
* @throws HttpException | ||
* @throws MissingActionNameException | ||
*/ | ||
public function getPublicPersistentResourceUri(PersistentResource $resource) | ||
{ | ||
if (!$this->configurationService->getCurrentConfigurationByPath('resourceProxy')) { | ||
return parent::getPublicPersistentResourceUri($resource); | ||
} | ||
|
||
$collection = $this->resourceManager->getCollection($resource->getCollectionName()); | ||
$storage = $collection->getStorage(); | ||
|
||
if (!$storage instanceof ProxyAwareStorageInterface) { | ||
return parent::getPublicPersistentResourceUri($resource); | ||
} | ||
|
||
if ($storage->resourceIsPresentInStorage($resource)) { | ||
return parent::getPublicPersistentResourceUri($resource); | ||
} | ||
|
||
// build uri to resource controller that will fetch and publish | ||
// the resource asynchronously | ||
return $this->uriBuilder->uriFor( | ||
'index', | ||
['resourceIdentifier' => $resource], | ||
'Resource', | ||
'Sitegeist.MagicWand' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ section of your composer.json**. | |
|
||
* Wilhelm Behncke - [email protected] | ||
* Martin Ficzel - [email protected] | ||
* ... and others | ||
|
||
*The development and the public-releases of this package is generously sponsored by our employer https://www.sitegeist.de.* | ||
|
||
|
@@ -108,6 +109,15 @@ the commands.** | |
``` | ||
**Note:** Use this command on a regular basis, because your stash tends to grow **very** large. | ||
## Resource proxies | ||
While cloning the database to your local dev system is manageable even for larger projects, downloading all the assets is often not an option. | ||
For this case the package offers the concept of resource proxies. Once activated, only the resources that are actually used are downloaded just at the moment they are rendered. | ||
This is done by custom implementations of `WritableFileSystemStorage` and `ProxyAwareFileSystemSymlinkTarget` and works out of the box if you use this storage and target in you local development environment. | ||
If you use other local storages, for example a local S3 storage, you can easily build your own proxy aware versions implementing the interfaces `ProxyAwareStorageInterface` and `ProxyAwareTargetInterface`of this package. | ||
## Installation | ||
Sitegeist.Magicwand is available via packagist. Just add `"sitegeist/magicwand" : "~1.0"` to the require-dev section of the composer.json or run `composer require --dev sitegeist/magicwand`. We use semantic-versioning so every breaking change will increase the major-version number. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters