Skip to content
Snippets Groups Projects

Issue #3335627 by Shubham Rathore: Update core_version_requirement on info.yml...

3 files
+ 63
8
Compare changes
  • Side-by-side
  • Inline
Files
3
@@ -8,6 +8,9 @@ use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class ResponseSubscriber implements EventSubscriberInterface {
protected array $path = [];
protected array $ignoreList = [];
public function onResponse(ResponseEvent $event) {
$response = $event->getResponse();
// First we check if we have an JSON:API response
@@ -35,6 +38,10 @@ class ResponseSubscriber implements EventSubscriberInterface {
if (!$config->get('remove_links')) {
return;
}
if ($config->get('ignore_list')) {
$this->ignoreList = array_filter(explode("\n", $config->get('ignore_list')), 'trim');
}
$this->removeLinks($json);
@@ -42,26 +49,55 @@ class ResponseSubscriber implements EventSubscriberInterface {
$event->setResponse($response);
}
protected function removeLinks(&$data) {
protected function removeLinks(&$data, string $node = null) {
if ($node) {
$this->path[] = $node;
}
// If we find the current path on the ignore list, we skip anything below that
if ($this->isOnIgnoreList()) {
array_pop($this->path);
return;
}
if (is_scalar($data)) {
array_pop($this->path);
return;
}
if (is_object($data)) {
unset($data->links);
// If this is the JSON:API response, do not remove the links.
// The links on the response may include pager links.
if (!property_exists($data, 'jsonapi')) {
// Meta must return an object. If links are the only property,
// and the links are removed by this module, then an empty array
// will be returned instead.
// So, if meta only contains links, then remove meta as well.
if (property_exists($data, 'meta') && property_exists($data->meta, 'links')) {
unset($data->meta->links);
$property_list = get_object_vars($data->meta);
if (count($property_list) === 0) {
unset($data->meta);
}
}
unset($data->links);
}
foreach (get_object_vars($data) as $name => $attribute) {
$this->removeLinks($data->$name);
$this->removeLinks($data->$name, $name);
}
array_pop($this->path);
return;
}
if (is_array($data)) {
foreach ($data as &$item) {
$this->removeLinks($item);
foreach ($data as $i => &$item) {
$this->removeLinks($item, $i);
}
}
array_pop($this->path);
}
public static function getSubscribedEvents() {
@@ -69,4 +105,16 @@ class ResponseSubscriber implements EventSubscriberInterface {
return $events;
}
private function isOnIgnoreList(): bool {
$path = implode('.', $this->path);
for ($i = 0; $i < count($this->ignoreList); $i++) {
if (preg_match('/' . $this->ignoreList[$i] . '/', $path)) {
return true;
}
}
return false;
}
}
Loading