<commit_before><?php
/**
 * Wei Framework
 *
 * @copyright   Copyright (c) 2008-2013 <NAME>
 * @license     http://opensource.org/licenses/mit-license.php MIT License
 */

namespace Wei;

/**
 * A service that provide the functionality of exclusive Lock
 *
 * @author      <NAME> <<EMAIL>>
 * @property    Cache $cache A cache service
 */
class Lock extends Base
{
    /**
     * @var array
     */
    protected $keys = array();

    /**
     * Constructor
     *
     * @param array $options
     */
    public function __construct(array $options = array())
    {
        parent::__construct($options);
        register_shutdown_function(array($this, 'releaseAll'));
    }

    /**
     * Acquire a lock key
     *
     * @param string $key
     * @return bool
     */
    public function __invoke($key)
    {
        if ($this->cache->add($key, true)) {
            $this->keys[] = $key;
            return true;
        } else {
            return false;
        }
    }

    /**
     * Release a lock key
     *
     * @param string $key
     * @return bool
     */
    public function release($key)
    {
        if ($this->cache->remove($key)) {
            if(($index = array_search($key, $this->keys)) !== false) {
                unset($this->keys[$index]);
            }
            return true;
        } else {
            return false;
        }
    }

    /**
     * Release all lock keys
     */
    public function releaseAll()
    {
        foreach ($this->keys as $key) {
            $this->release($key);
        }
    }
}<commit_msg>added signal support for lock service<commit_after><?php
/**
 * Wei Framework
 *
 * @copyright   Copyright (c) 2008-2013 <NAME>
 * @license     http://opensource.org/licenses/mit-license.php MIT License
 */

namespace Wei;

/**
 * A service that provide the functionality of exclusive Lock
 *
 * @author      <NAME> <<EMAIL>>
 * @property    Cache $cache A cache service
 */
class Lock extends Base
{
    /**
     * @var array
     */
    protected $keys = array();

    /**
     * Constructor
     *
     * @param array $options
     */
    public function __construct(array $options = array())
    {
        parent::__construct($options);

        register_shutdown_function(array($this, 'releaseAll'));

        // Release all lock and exist when catch signal in CLI
        if (function_exists('pcntl_signal')) {
            pcntl_signal(SIGINT, array($this, 'catchSignal'));
            pcntl_signal(SIGTERM, array($this, 'catchSignal'));
        }
    }

    /**
     * Acquire a lock key
     *
     * @param string $key
     * @return bool
     */
    public function __invoke($key)
    {
        if ($this->cache->add($key, true)) {
            $this->keys[] = $key;
            return true;
        } else {
            return false;
        }
    }

    /**
     * Release a lock key
     *
     * @param string $key
     * @return bool
     */
    public function release($key)
    {
        if ($this->cache->remove($key)) {
            if (($index = array_search($key, $this->keys)) !== false) {
                unset($this->keys[$index]);
            }
            return true;
        } else {
            return false;
        }
    }

    /**
     * Release all lock keys
     */
    public function releaseAll()
    {
        foreach ($this->keys as $key) {
            $this->release($key);
        }
    }

    /**
     * Release all lock keys and exit
     */
    public function catchSignal()
    {
        $this->releaseAll();
        exit;
    }
}
{'fraction_non_alphanumeric': 0.13925127623369257, 'fraction_numerical': 0.0045377197958026095, 'mean_word_length': 1.6800911854103344, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 30, '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': '1618602', 'n_tokens_mistral': 1044, 'n_tokens_neox': 1012, 'n_words': 257}