Skip to content

Commit

Permalink
Introduce InstancePoolTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
willdurand committed Jan 27, 2013
1 parent 00442b4 commit 0a96ef6
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/Propel/Runtime/ActiveQuery/InstancePoolTrait.php
@@ -0,0 +1,62 @@
<?php

/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

namespace Propel\Runtime\ActiveQuery;

use Propel\Runtime\Propel;
use Propel\Runtime\Exception\RuntimeException;

trait InstancePoolTrait
{
private static $instances = array();

public static function addInstanceToPool($object, $key = null)

This comment has been minimized.

Copy link
@staabm

staabm Mar 25, 2013

Member

you could use self as typehint for $object so php will take care that we don't insert objects from different types into a instance-pool.
thx @gharlan for the tip.

This comment has been minimized.

Copy link
@willdurand

willdurand Mar 25, 2013

Author Contributor

self?

This comment has been minimized.

Copy link
@staabm

staabm Mar 25, 2013

Member

yes, self which represents the type of the actual class which use'ses the trait.

This comment has been minimized.

Copy link
@havvg

havvg Mar 25, 2013

Member

Should be static, instead of self, no?

This comment has been minimized.

Copy link
@staabm

staabm Mar 25, 2013

Member

nope. I am talking about

public static function addInstanceToPool(self $object, $key = null)

This comment has been minimized.

Copy link
@havvg

havvg Mar 25, 2013

Member

Ah!

{
if (Propel::isInstancePoolingEnabled()) {
if (null === $key) {
$key = (string) $object->getId();
}

self::$instances[$key] = $object;
}
}

public static function removeInstanceFromPool($value)
{
if (Propel::isInstancePoolingEnabled() && null !== $value) {
if (is_object($value)) {
$key = (string) $value->getId();
} elseif (is_scalar($value)) {
// assume we've been passed a primary key
$key = (string) $value;
} else {
throw new RuntimeException('Invalid value passed to removeInstanceFromPool()');
}

unset(self::$instances[$key]);
}
}

public static function getInstanceFromPool($key)
{
if (Propel::isInstancePoolingEnabled()) {
if (isset(self::$instances[$key])) {
return self::$instances[$key];
}
}

return null;
}

public static function clearInstancePool()
{
self::$instances = array();

This comment has been minimized.

Copy link
@hhamon

hhamon Jan 27, 2013

Member

I think you should use static everywhere instead of self.

This comment has been minimized.

Copy link
@willdurand

willdurand Jan 27, 2013

Author Contributor

in a trait, why?

This comment has been minimized.

Copy link
@hhamon

hhamon Jan 27, 2013

Member

I'm not a trait expert. Can we extend a trait?

}
}

3 comments on commit 0a96ef6

@havvg
Copy link
Member

@havvg havvg commented on 0a96ef6 Jan 28, 2013

Choose a reason for hiding this comment

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

What do you think about adding another dimension to the instance pool? The first dimension should be the scope on which you are pooling, and the second dimension would be the current one, the value representing the instance.

This would allow for example to create a pool based on a (composite) unique key, which is not the primary key.

@jaugustin
Copy link
Member

Choose a reason for hiding this comment

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

@havvg this is already possible, but you have to handle it by hand ;) you just need to provide the right $key when adding/getting your object to/from the pool.
$key = 'custom_' . $uniqKey1 . '_' . $uniqKey2;

@havvg
Copy link
Member

@havvg havvg commented on 0a96ef6 Jan 28, 2013

Choose a reason for hiding this comment

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

Ah, true - and you need to (over)write the findOneBy* methods of the *Query. Thanks ;)

Please sign in to comment.