<commit_before><?php
/*
 * This file is part of Graze DAL
 *
 * Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @see  http://github.com/graze/dal/blob/master/LICENSE
 */
namespace Graze\Dal\UnitOfWork;

use Graze\Dal\Adapter\AdapterInterface;
use Graze\Dal\Configuration\ConfigurationInterface;
use Graze\Dal\Mapper\MapperInterface;
use Graze\Dal\Persister\PersisterInterface;
use SplObjectStorage;

class UnitOfWork implements UnitOfWorkInterface
{
    const POLICY_IMPLICIT = 0;
    const POLICY_EXPLICIT = 1;

    protected $adapter;
    protected $config;
    protected $mappers = [];
    protected $persisted;
    protected $persisters = [];
    protected $records = [];
    protected $trackingPolicy;

    /**
     * @param AdapterInterface $adapter
     * @param ConfigurationInterface $config
     * @param integer $trackingPolicy
     */
    public function __construct(
        AdapterInterface $adapter,
        ConfigurationInterface $config,
        $trackingPolicy = self::POLICY_IMPLICIT
    ) {
        $this->adapter = $adapter;
        $this->config = $config;
        $this->trackingPolicy = (integer) $trackingPolicy;

        $this->persisted = new SplObjectStorage();
    }

    /**
     * @param object $entity
     */
    public function commit($entity = null)
    {
        foreach ($this->persisted as $persisted) {
            if (! $entity || $entity === $persisted) {
                $this->getPersisterByEntity($persisted)->save($persisted);

                if ($entity) {
                    $this->persisted->detach($persisted);
                    return;
                }
            }
        }

        $this->persisted->removeAll($this->persisted);
    }

    /**
     * @param object $entity
     */
    public function persist($entity)
    {
        $this->persisted->attach($entity);
    }

    /**
     * @param object $entity
     */
    public function refresh($entity)
    {
        $this->getPersisterByEntity($entity)->refresh($entity);
    }

    /**
     * @param object $entity
     */
    public function remove($entity)
    {
        $this->getPersisterByEntity($entity)->delete($entity);
    }

    /**
     * @param object $entity
     */
    public function persistByTrackingPolicy($entity)
    {
        if (self::POLICY_IMPLICIT === $this->trackingPolicy) {
            $this->persist($entity);
        }
    }<commit_msg>Changing UnitOfWork to use an array for object storage<commit_after><?php
/*
 * This file is part of Graze DAL
 *
 * Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @see  http://github.com/graze/dal/blob/master/LICENSE
 */
namespace Graze\Dal\UnitOfWork;

use Graze\Dal\Adapter\AdapterInterface;
use Graze\Dal\Configuration\ConfigurationInterface;
use Graze\Dal\Mapper\MapperInterface;
use Graze\Dal\Persister\PersisterInterface;
use SplObjectStorage;

class UnitOfWork implements UnitOfWorkInterface
{
    const POLICY_IMPLICIT = 0;
    const POLICY_EXPLICIT = 1;

    protected $adapter;
    protected $config;
    protected $mappers = [];
    protected $persisted = [];
    protected $persisters = [];
    protected $records = [];
    protected $trackingPolicy;

    /**
     * @param AdapterInterface $adapter
     * @param ConfigurationInterface $config
     * @param integer $trackingPolicy
     */
    public function __construct(
        AdapterInterface $adapter,
        ConfigurationInterface $config,
        $trackingPolicy = self::POLICY_IMPLICIT
    ) {
        $this->adapter = $adapter;
        $this->config = $config;
        $this->trackingPolicy = (integer) $trackingPolicy;
    }

    /**
     * @param object $entity
     */
    public function commit($entity = null)
    {
        foreach ($this->persisted as $persisted) {
            if (! $entity || $entity === $persisted) {
                $this->getPersisterByEntity($persisted)->save($persisted);

                if ($entity) {
                    $oid = spl_object_hash($entity);
                    unset($this->persisted[$oid]);
                    return;
                }
            }
        }

        $this->persisted = [];
    }

    /**
     * @param object $entity
     */
    public function persist($entity)
    {
        $oid = spl_object_hash($entity);
        $this->persisted[$oid] = $entity;
    }

    /**
     * @param object $entity
     */
    public function refresh($entity)
    {
        $this->getPersisterByEntity($entity)->refresh($entity);
    }

    /**
     * @param object $entity
     */
    public function remove($entity)
    {
        $this->getPersisterByEntity($entity)->delete($entity);
    }

    /**
     * @param object $entity
     */
    public function persistByTrackingPolicy($entity)
    {
        if (self::POLICY_IMPLICIT === $this->trackingPolicy) {
            $this->persist($entity);
        }
    }
{'fraction_non_alphanumeric': 0.12441314553990611, 'fraction_numerical': 0.002347417840375587, 'mean_word_length': 2.3396472893533637, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3925426', 'n_tokens_mistral': 1503, 'n_tokens_neox': 1439, 'n_words': 314}