Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.0][CacheResolver] Resolver get rid of get browser path #284

Merged

Conversation

makasim
Copy link
Collaborator

@makasim makasim commented Dec 12, 2013

This PR is attemp to address @LouTerrailloune comment.

It is based on #282 PR so please consider reviewing and merging that one first.

To say briefly about changes:

  • [ResolverInterface] The resolve method can return NULL which means we dont have a cache yet and a string. String is an url where the image located.
  • [ResolverInterface] The method getBrowserPath was removed.
  • [CacheManager] The method getBrowserPath now use resolve method to find a cached image url. If it fails it generates an url of filter action.
  • Removed dependency on cache manager almost in all resolver. WebPathResolver still needed it

@@ -120,7 +119,13 @@ protected function getResolver($filter)
*/
public function getBrowserPath($path, $filter, $absolute = false)
{
return $this->getResolver($filter)->getBrowserPath($path, $filter, $absolute);
//call it to make sure the resolver for the give filter exists.
$this->getResolver($filter);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no use to this behavior. The resolve method will try to retrieve the resolver anyway and throws an exception if none is applicable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the only thing left, as far as I can see. What's the benefit of this line?

The only case I can see, would be calling an invalid filter from a template. The issue I see: the whole template would not be rendered with this line in place. Without this line, the template would be fine as it and only contain an invalid URL to an image (via generateUrl).

@LouTerrailloune
Copy link
Contributor

  1. I don't think setting the ResolverInterface:store return value to string is a good idea. For exemple, the resolver might load the image data from a database or local cache.
  2. The redirection returned by ResolverInterface:resolve is not really needed, because it's only used when the webserver is missconfigured or when the file is propagating to a CDN.

So, in my opinion, ResolverInterface:resolve should return only a string or null (equivalent to your getBrowserPath), the filterAction do no call resolve and just load the data and call ResolverInterface:store. CacheManager:resolve return the filterAction url if the resolver return null. CacheManager:getBrowserPath and ResolverInterface:getBrowserPath are removed.

@havvg
Copy link
Contributor

havvg commented Dec 13, 2013

@LouTerrailloune The Resolver is not responsible for actually loading the image data. That's the purpose of the DataLoader. Returning the URL to retrieve the given image path on this resolver is fine for ResolverInterface::resolve.

I don't think setting the ResolverInterface:store return value to string is a good idea. For exemple, the resolver might load the image data from a database or local cache.

Did you actually refer to the store method? It's returning the modified Response, not a string.

The redirection returned by ResolverInterface:resolve is not really needed, because it's only used when the webserver is missconfigured or when the file is propagating to a CDN.

The URL returned by the resolve method defines the URL to load the image from utilizing the resolver again e.g. a CDN like S3, or locally saved in the web path etc.

if ($filteredImagePath instanceof Response) {
return $filteredImagePath;
if ($url = $this->cacheManager->resolve($originalImagePath, $filter)) {
return new RedirectResponse($url, 301);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this. It's cleaner than before, but now the resolve method is responsible of detecting whether the given path has been stored within this resolver and resolve the given path to a URL utilising the resolver.

I would prefer a new method isStored or something to decouple the two facts.

The controller then can distinguish between an already stored image and a to-be-stored one.
This will also remove the ambiguous return value of the Resolver::resolve method. It's always string: the URL. While the new method will take care of the previous null return value, question would result in a false of isStored.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is a good idea in general. What should happen if one call resolve when image is not stored yet? I would say an exception.

@LouTerrailloune
Copy link
Contributor

Did you actually refer to the store method? It's returning the modified Response, not a string.

Yes, typo.

The URL returned by the resolve method defines the URL to load the image from utilizing the resolver again e.g. a CDN like S3, or locally saved in the web path etc.

Most of the time, the url of the cached picture is returned by getBrowserPath (not resolve), via the templating extension/helper.

The Resolver is not responsible for actually loading the image data. That's the purpose of the DataLoader.

May be I have a resolver that store filtered picture (cropped, watermarked, etcc) into a database. In that case, the filterAction is alway hit and resolve load data from the databse and return it in a Response.

@LouTerrailloune
Copy link
Contributor

Regarding my use case about database cache, we need to keep resolve and getBrowserPath (which can be renamed getUrl).

resolve return a Response with data (from cache) or null. (or may be a redirect)
getBrowserPath return a string or null.

In CacheManager, resolve is just a proxy that call the resolver resolve method, getBrowserPath return the result of the resolver getBrowserPath or the url of the filterAction if the resolver returns null.

@havvg
Copy link
Contributor

havvg commented Dec 13, 2013

Most of the time, the url of the cached picture is returned by getBrowserPath (not resolve), via the templating extension/helper.

That's fine because the bundles Resolver actually resulted in resolve within getBrowserPath with slight changes. That was only necessary because of the different targetPath, which will be removed. Thus the getBrowserPath will not be required anymore.

May be I have a resolver that store filtered picture (cropped, watermarked, etcc) into a database. In that case, the filterAction is alway hit and resolve load data from the databse and return it in a Response.

If you got a Resolver which stores the images in a database, and you want this resolver to serve the files, you would always hit the database. If that's ok for you, it's fine and still possible. If not, I misunderstand this use-case and would like to know what exactly you are trying to achieve.

@LouTerrailloune
Copy link
Contributor

If you got a Resolver which stores the images in a database, and you want this resolver to serve the files, you would always hit the database. If that's ok for you, it's fine and still possible. If not, I misunderstand this use-case and would like to know what exactly you are trying to achieve.

This is what I want to do. However, in this PR, resolve only return an url and then a Resolver cannot create a Response (from database in my exemple) and return it to the controller. This is why I think we need to keep the Response return value.

@havvg
Copy link
Contributor

havvg commented Dec 13, 2013

Yes that's fine, you will have to move some logic around, because you mixed up the resolver with the controller. That's an issue of the old version.

@makasim
Copy link
Collaborator Author

makasim commented Dec 13, 2013

So to sum up all the discussion:

  • resolve has to return an url (string) always.
  • if you call resolve before actually storing image you endup with an exception.
  • new method isStored has to be added.

Do I miss something?

@makasim
Copy link
Collaborator Author

makasim commented Dec 13, 2013

@LouTerrailloune we should definitly think of the cases when the image stored in database or something like that. But my main goal right now to decouple things and clean the bundle a bit. Later we can come back and rethink our use case.

@havvg
Copy link
Contributor

havvg commented Dec 14, 2013

Do I miss something?

Not as far as I can see.

@makasim
Copy link
Collaborator Author

makasim commented Dec 14, 2013

rebased against lates resolver-do-not-expose-target-path branch

@havvg
Copy link
Contributor

havvg commented Dec 15, 2013

For some reason I currently can't create a PR with Github, it's not recognising the branch. I added the ResolverInterface::isStored at https://github.com/havvg/LiipImagineBundle/tree/resolver-get-rid-of-get-browser-path

@havvg
Copy link
Contributor

havvg commented Dec 15, 2013

PR is now available, created another branch which is recognised by Github.

@makasim
Copy link
Collaborator Author

makasim commented Dec 16, 2013

I think it is almost fine but I would like to review it a bit more today in the evening to make sure I dont miss\broke anything

@havvg
Copy link
Contributor

havvg commented Dec 16, 2013

Fine by me, let me know :-)

@makasim
Copy link
Collaborator Author

makasim commented Dec 18, 2013

@havvg I fixed WebPathResolver. It was not return string on resolve always as interface demand. Also updated UPGRADE.md

* @param string $path The resource path to convert.
* @param string $filter The name of the imagine filter.
*
* @return string
*/
abstract protected function getFilePath($path, $filter);
protected function getFilePath($path, $filter)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this logic back into WebPathResolver. It's bound to the web root.

@makasim
Copy link
Collaborator Author

makasim commented Dec 18, 2013

@havvg done

@makasim
Copy link
Collaborator Author

makasim commented Dec 18, 2013

@havvg ping

havvg added a commit that referenced this pull request Dec 18, 2013
…rowser-path

[1.0][CacheResolver] Resolver get rid of get browser path
@havvg havvg merged commit 11e889a into liip:develop Dec 18, 2013
@havvg
Copy link
Contributor

havvg commented Dec 18, 2013

Thank you!

@makasim makasim deleted the resolver-get-rid-of-get-browser-path branch December 18, 2013 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Attn: Blocker This item blocks other issue(s) or PR(s) and therefore must be resolved prior.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants