Skip to content

Disable show ObjectManager/Models/Collection internals with var_dump #30326

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

Open
wants to merge 16 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions lib/internal/Magento/Framework/Data/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Data\Collection\EntityFactoryInterface;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Option\ArrayInterface;

/**
* Data collection
Expand All @@ -19,8 +17,10 @@
*
* @api
* @since 100.0.2
*
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
*/
class Collection implements \IteratorAggregate, \Countable, ArrayInterface, CollectionDataSourceInterface
class Collection implements \IteratorAggregate, \Countable, OptionSourceInterface, CollectionDataSourceInterface
Copy link
Contributor

Choose a reason for hiding this comment

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

Also not clear why you changed interface here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like it’s not really related to the name of this PR, also this isn’t backward compatible change. Can we extract it to separate PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, i see that svc fails anyway, so we’ll need approval. No need to do anything

{
const SORT_ORDER_ASC = 'ASC';

Expand Down Expand Up @@ -767,6 +767,7 @@ protected function _toOptionArray($valueField = 'id', $labelField = 'name', $add
$additional['label'] = $labelField;

foreach ($this as $item) {
$data = [];
foreach ($additional as $code => $field) {
$data[$code] = $item->getData($field);
}
Expand Down Expand Up @@ -915,4 +916,16 @@ public function __wakeup()
$objectManager = ObjectManager::getInstance();
$this->_entityFactory = $objectManager->get(EntityFactoryInterface::class);
}

/**
* Export only scalar and arrays properties for var_dump
*
* @return array
*/
public function __debugInfo()
{
return array_filter(get_object_vars($this), function ($v) {
return is_scalar($v) || is_array($v);
});
}
}
39 changes: 27 additions & 12 deletions lib/internal/Magento/Framework/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ public function getDataByKey($key)
/**
* Get value from _data array without parse key
*
* @param string $key
* @return mixed
* @param string $key
* @return mixed
*/
protected function _getData($key)
{
Expand Down Expand Up @@ -263,7 +263,7 @@ public function toArray(array $keys = [])
/**
* The "__" style wrapper for toArray method
*
* @param array $keys
* @param array $keys
* @return array
*/
public function convertToArray(array $keys = [])
Expand Down Expand Up @@ -373,9 +373,9 @@ public function toString($format = '')
/**
* Set/Get attribute wrapper
*
* @param string $method
* @param array $args
* @return mixed
* @param string $method
* @param array $args
* @return mixed
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function __call($method, $args)
Expand Down Expand Up @@ -438,11 +438,11 @@ protected function _underscore($name)
*
* Example: key1="value1" key2="value2" ...
*
* @param array $keys array of accepted keys
* @param string $valueSeparator separator between key and value
* @param string $fieldSeparator separator between key/value pairs
* @param string $quote quoting sign
* @return string
* @param array $keys array of accepted keys
* @param string $valueSeparator separator between key and value
* @param string $fieldSeparator separator between key/value pairs
* @param string $quote quoting sign
* @return string
*/
public function serialize($keys = [], $valueSeparator = '=', $fieldSeparator = ' ', $quote = '"')
{
Expand All @@ -464,7 +464,7 @@ public function serialize($keys = [], $valueSeparator = '=', $fieldSeparator = '
* Present object data as string in debug mode
*
* @param mixed $data
* @param array &$objects
* @param array $objects
* @return array
*/
public function debug($data = null, &$objects = [])
Expand Down Expand Up @@ -541,4 +541,19 @@ public function offsetGet($offset)
}
return null;
}

/**
* Export only scalar and arrays properties for var_dump
*
* @return array
*/
public function __debugInfo()
{
return array_filter(
get_object_vars($this),
function ($v) {
return is_scalar($v) || is_array($v);
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,15 @@ public function merge(array $config)
{
$this->_data = $this->pluginListGenerator->merge($config, $this->_data);
}

/**
* Disable show PluginList internals with var_dump
*
* @see https://www.php.net/manual/en/language.oop5.magic.php#object.debuginfo
* @return array
*/
public function __debugInfo()
{
return [];
}
}
23 changes: 20 additions & 3 deletions lib/internal/Magento/Framework/ObjectManager/ObjectManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
*/
namespace Magento\Framework\ObjectManager;

class ObjectManager implements \Magento\Framework\ObjectManagerInterface
use Magento\Framework\ObjectManagerInterface;

/**
* Base implementation Object Manager
*/
class ObjectManager implements ObjectManagerInterface
{
/**
* @var \Magento\Framework\ObjectManager\FactoryInterface
Expand All @@ -34,14 +39,14 @@ class ObjectManager implements \Magento\Framework\ObjectManagerInterface
/**
* @param FactoryInterface $factory
* @param ConfigInterface $config
* @param array &$sharedInstances
* @param array $sharedInstances
*/
public function __construct(FactoryInterface $factory, ConfigInterface $config, &$sharedInstances = [])
{
$this->_config = $config;
$this->_factory = $factory;
$this->_sharedInstances = &$sharedInstances;
$this->_sharedInstances[\Magento\Framework\ObjectManagerInterface::class] = $this;
$this->_sharedInstances[ObjectManagerInterface::class] = $this;
}

/**
Expand Down Expand Up @@ -74,6 +79,7 @@ public function get($type)

/**
* Configure di instance
*
* Note: All arguments should be pre-processed (sort order, translations, etc) before passing to method configure.
*
* @param array $configuration
Expand All @@ -83,4 +89,15 @@ public function configure(array $configuration)
{
$this->_config->extend($configuration);
}

/**
* Disable show ObjectManager internals with var_dump
*
* @see https://www.php.net/manual/en/language.oop5.magic.php#object.debuginfo
* @return array
*/
public function __debugInfo()
{
return [];
}
}