prismicio-community / php-kit

Community maintained development kit for Prismic and the PHP language
https://prismic.io
Other
108 stars 82 forks source link

Dispatch events before and after search form is submitted #103

Closed tremby closed 7 years ago

tremby commented 9 years ago

A new method Api#getDispatcher is added, which gets an EventDispatcher instance. Events can be subscribed to/listened for via this object.

The events prismic.pre_submit and prismic.post_submit are fired just before a search form is submitted and just after the response is received and parsed as JSON, respectively. Both events allow the SearchForm object to be retrieved, inspected and operated on, and the prismic.post_submit event additionally provides the JSON response object and a boolean denoting whether the response came from the cache or not.

Using these events, a developer can for instance alter the form before it is sent, or log queries and how long they take.

tremby commented 9 years ago

One thing I'm not totally happy with here is that this event is the only place the JSON returned from the API is made available.

I did that because my use case is profiling the queries, and without returning this JSON I can't log the number of documents returned by the query. The only way I could do that without providing the JSON (or just providing certain parts of it, like the counts of results and total results) would be to emit this event later, once the result set has been parsed to a Results object. But this will make the timing less accurate, and it also wouldn't ever emit for ->count() queries, where the result set is not actually parsed to a Results.

Any thoughts?

Is it okay as it is, or should I return just results_size and total_results_size along with it?

tremby commented 9 years ago

Anyone taken a look at this yet?

erwan commented 9 years ago

Hi Bart,

I'm not sure we want to include that, we're trying to keep the kits as simple as possible. What's the problem you're trying to solve exactly?

tremby commented 9 years ago

Currently I'm using it to log and profile queries to the Prismic API. It is helping weed out duplicate queries and find things which can be optimized.

You're already listing the Symfony Event Dispatcher package as a dependency; may as well use it... ;)

Anything I can do to get this included? Any way I can simplify it? Want me to write some documentation? Tests are included, as you can see.

tremby commented 7 years ago

Any reason? You don't think it's useful?

srenault commented 7 years ago

Sorry @tremby. I should leave a comment after closing. I think we don't really need this at the moment. If more users ask for it, we could add it further.