Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

ValueGenerator constant detection #4238

Closed
wants to merge 1 commit into from
Closed

ValueGenerator constant detection #4238

wants to merge 1 commit into from

Conversation

3axap4eHko
Copy link
Contributor

For example, we need to get a file like this:

<?php
return array(
    'module' => array(
        'moduleDir' => __DIR__ . '/../module'
    )
);

but we cant pass argument __DIR__, because this constant will be parsed and replaced to value, and we must quote it 'DIR' . And finally, after generation, we will not get this:

        'moduleDir' => __DIR__ . '/../module'

generated value will be like it:

        'moduleDir' => '__DIR__ . \'/../module\''

Changes:
Add ability to define environment constants by method

public function initEnvironmentConstants();

Also add ability to define custom constants by methods

public function addConstant($constant);
public function deleteConstant($constant)

property $constants must be type of ArrayObject for memory saving when pass it as argument for recursive generation by reference.

@@ -57,13 +59,19 @@ class ValueGenerator extends AbstractGenerator
* @var array
*/
protected $allowedTypes = null;
/**
* Autodetectable constants
* @var ArrayObject
Copy link
Member

Choose a reason for hiding this comment

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

Why are you using an ArrayObject and not simply an array?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ghost ghost assigned weierophinney Apr 16, 2013
@weierophinney
Copy link
Member

This looks good. My only comment is that I see no compelling reason for using ArrayObject to store the constants; in fact, based on the usage within the class, using array would simplify the code and likely make it more performant as well (fewer ArrayObject <-> array conversions).

@3axap4eHko
Copy link
Contributor Author

property $constants must be type of ArrayObject for memory saving when pass it as argument for recursive generation by reference. If we have an array instead of ArrayObject, at each recursive call and initialized EnvironmentConstants we will create an array of about 1500 lines and it will take much memory for nested values.

@prolic
Copy link
Contributor

prolic commented Apr 20, 2013

performance comparision scripts?

}

/**
* Return constnat list
Copy link
Member

Choose a reason for hiding this comment

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

s/constnat/constant/

weierophinney added a commit that referenced this pull request Apr 21, 2013
ValueGenerator constant detection
weierophinney added a commit that referenced this pull request Apr 21, 2013
weierophinney added a commit that referenced this pull request Apr 21, 2013
@weierophinney
Copy link
Member

Merged to develop for release with 2.2.0.

@@ -239,7 +318,7 @@ public function generate()
);
foreach ($rii as $curKey => $curValue) {
if (!$curValue instanceof ValueGenerator) {
$curValue = new self($curValue);
$curValue = new self($curValue, self::TYPE_AUTO, self::OUTPUT_MULTIPLE_LINE, $this->getConstants());
Copy link
Member

Choose a reason for hiding this comment

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

@3axap4eHko Did you use ArrayObject for this line?

I don't understand the performance improvements you claim. Non return by reference methods implemented on StdLib\ArrayObject are invoked.

The only thing I see the constants are shared by the instances created in this line, so the only need I see $this->constants must be provided as reference. Alternative solutions may

  • Use PHP native ArrayObject
  • Refactor getConstants for return by reference.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Constants is a very big array, containing about 1k string values. Let's length of every string length is 10, therefore total size of array in memory would be greater then 10KB for one instance of ValueGenerator, that means for 1 property of generated class. So for 100 classes with 100 properties total size of constant arrays would be 10MB instead of 10KB for reference value

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also you should keep in mind of allocating memory for every array in method. That 100 classes with 100 properties would be have a 10000 arrays

Copy link
Member

Choose a reason for hiding this comment

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

So native ArrayObject is suitable for this case and Stdlib version is not required, isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Stdlib\ArrayObject is a wrapper for backward version capability and nothing more

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants