This repository was archived by the owner on Jul 11, 2023. It is now read-only.
forked from ollieLtd/OhFormErrorLogBundle
-
Notifications
You must be signed in to change notification settings - Fork 1
Bugfix for resource serialization #1
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,94 @@ | ||
| <?php | ||
|
|
||
| namespace Oh\FormErrorLogBundle\Logger; | ||
|
|
||
| trait SerializeData | ||
| { | ||
| private function serialize($data) | ||
| { | ||
| if (is_object($data)) { | ||
| return $this->serializeObject($data); | ||
| } elseif (is_resource($data)) { | ||
| return $this->serializeResource($data); | ||
| } elseif (is_array($data)) { | ||
| return $this->serializeArray($data); | ||
| } else { | ||
| return $this->serializeNonObject($data); | ||
| } | ||
| } | ||
|
|
||
| private function serializeObject($object) | ||
| { | ||
| $data = ''; | ||
|
|
||
| // JsonSerializable is for php 5.4 | ||
| if (class_exists('\JsonSerializable', false) && $object instanceof \JsonSerializable) { | ||
| $data = json_encode($object); | ||
|
|
||
| // otherwise we could just see if that method exists | ||
| } elseif (method_exists($object, 'jsonSerialize')) { | ||
| $data = json_encode($object->jsonSerialize()); | ||
|
|
||
| // some people create a toArray() method | ||
| } elseif (method_exists($object, 'toArray') && is_array($array = $object->toArray())) { | ||
| // JSON_PRETTY_PRINT is > PHP 5.4 | ||
| if (defined('JSON_PRETTY_PRINT')) { | ||
| $data = json_encode($array, JSON_PRETTY_PRINT); | ||
| } else { | ||
| $data = json_encode($array); | ||
| } | ||
|
|
||
| // lets try to serialize | ||
| // this could be risky if the object is too large or not implemented correctly | ||
| } elseif (method_exists($object, '__sleep') || $object instanceof Serializable) { | ||
| $data = @serialize($object); | ||
| } | ||
|
|
||
| return $data; | ||
| } | ||
|
|
||
| /** | ||
| * @param resource $resource | ||
| * @return string | ||
| */ | ||
| private function serializeResource($resource) | ||
| { | ||
| // we cann't serialize PHP resources | ||
| return ''; | ||
| } | ||
|
|
||
| /** | ||
| * @param array $array | ||
| * @return string | ||
| */ | ||
| private function serializeArray($array) | ||
| { | ||
| foreach ($array as &$value) { | ||
| if (is_object($value)) { | ||
| $value = $this->serializeObject($value); | ||
| } elseif (is_resource($value)) { | ||
| $value = $this->serializeResource($value); | ||
| } | ||
| } | ||
|
|
||
| return $this->serializeNonObject($array); | ||
| } | ||
|
|
||
| /** | ||
| * @param int|string|array|null $nonObject | ||
| * @return string | ||
| */ | ||
| private function serializeNonObject($nonObject) | ||
| { | ||
| $data = ''; | ||
| try { | ||
| $data = serialize($nonObject); | ||
| } catch (\Throwable $t) { | ||
| // do nothing, will catch in PHP >= 7.0 | ||
| } catch (\Exception $e) { | ||
| // do nothing, will catch in PHP <= 5.6 | ||
| } finally { | ||
| return $data; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this compatible with php 5.4?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this will work in the old-fashioned way where line 85 errors and nothing is caught.
This amendment doesn't change the existing behaviour on PHP 5.x and improves only when running on 7.x