<commit_before><?php

/**
 * LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * PHP version 5
 *
 * @category  WindowsAzure
 * @package   ericsk\WindowsAzure
 * @author    <NAME> <<EMAIL>>
 * @license   http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
 * @link      http://github.com/ericsk/azure-table-sessionhandler-for-php
 */

namespace ericsk\WindowsAzure;

require_once 'vendor/autoload.php';

use WindowsAzure\Common\ServiceBuilder;
use WindowsAzure\Common\ServiceException;
use WindowsAzure\Table\Models\Entity;
use WindowsAzure\Table\Models\EdmType;
use WindowsAzure\Table\Models\TableServiceOptions;

/**
 *
 */
class WindowsAzureTableSessionHandler implements SessionHandlerInterface {

  /**
   * Windows Azure Table Service REST proxy instance.
   *
   * @var TableService
   */
  protected $_tableRestProxy;

  /**
   * The name of the table which stores session data.
   *
   * @var string
   */
  protected $_sessionContainer;

  /**
   * The name of the partition for storing session data.
   *
   * @var string
   */
  protected $_sessionContainerPartition;


  /**
   * Session handler constructor.
   *
   * @param $storageAccountName The Windows Azure Table Services account name.
   * @param $storageAccountKey The Windows Azure Table Service account key.
   * @param $sessionContainer The name of the table for storing session data.
   * @param $sessionContainerParition The name of the partition for storing session data.
   */
  public function __construct($storageAccountName, $storageAccountKey, $sessionContainer = 'phpsessions', $sessionContainerPartition = 'sessions') {
    // create the conneciton string for creating the table service rest proxy intance.
    $connectionString = "DefaultEndpointsProtocol=https;AccountName=" .
                        $storageAccountName .
                        ";AccountKey=" . 
                        $storageAccountKey;

    // create the table service instance.
    $this->_tableRestProxy = ServiceBuilder::getInstance()->createTableService($connectionString);

    // set up the table and partition names.
    $this->_sessionContainer = $sessionContainer;
    $this->_sessionContainerPartition = $sessionContainerPartition;
  }

  /**
   * Destructor.
   */
  public function __destruct() {
    session_write_close();
  }

  /**
   * Callback function for session handler. It's invoked while the session is being opened.
   *
   * @param $savePath The path to store the session.
   * @param $sessionName The name of the session.
   *
   * @return boolean If the open operation success.
   */
  public function open($savePath, $sessionName) {
    try {
      // get table to see if the table exists.
      $this->_tableRestProxy->getTable($this->_sessionContainer);
    } catch (ServiceException $e) {
      // cannot get the table, so create it
      $this->_tableRestProxy->createTable($this->_sessionContainer);
    }
    return TRUE;
  }
<commit_msg>Register the session shutdown function.<commit_after><?php

/**
 * LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * PHP version 5
 *
 * @category  WindowsAzure
 * @package   ericsk\WindowsAzure
 * @author    <NAME> <<EMAIL>>
 * @license   http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
 * @link      http://github.com/ericsk/azure-table-sessionhandler-for-php
 */

namespace ericsk\WindowsAzure;

require_once 'vendor/autoload.php';

use WindowsAzure\Common\ServiceBuilder;
use WindowsAzure\Common\ServiceException;
use WindowsAzure\Table\Models\Entity;
use WindowsAzure\Table\Models\EdmType;
use WindowsAzure\Table\Models\TableServiceOptions;

/**
 *
 */
class WindowsAzureTableSessionHandler implements SessionHandlerInterface {

  /**
   * Windows Azure Table Service REST proxy instance.
   *
   * @var TableService
   */
  protected $_tableRestProxy;

  /**
   * The name of the table which stores session data.
   *
   * @var string
   */
  protected $_sessionContainer;

  /**
   * The name of the partition for storing session data.
   *
   * @var string
   */
  protected $_sessionContainerPartition;


  /**
   * Session handler constructor.
   *
   * @param $storageAccountName The Windows Azure Table Services account name.
   * @param $storageAccountKey The Windows Azure Table Service account key.
   * @param $sessionContainer The name of the table for storing session data.
   * @param $sessionContainerParition The name of the partition for storing session data.
   */
  public function __construct($storageAccountName, $storageAccountKey, $sessionContainer = 'phpsessions', $sessionContainerPartition = 'sessions') {
    // create the conneciton string for creating the table service rest proxy intance.
    $connectionString = "DefaultEndpointsProtocol=https;AccountName=" .
                        $storageAccountName .
                        ";AccountKey=" . 
                        $storageAccountKey;

    // create the table service instance.
    $this->_tableRestProxy = ServiceBuilder::getInstance()->createTableService($connectionString);

    // set up the table and partition names.
    $this->_sessionContainer = $sessionContainer;
    $this->_sessionContainerPartition = $sessionContainerPartition;
	
	// register the session shutdown function.
	register_shutdown_function('session_write_close');
  }

  /**
   * Destructor.
   */
  public function __destruct() {
    session_write_close();
  }

  /**
   * Callback function for session handler. It's invoked while the session is being opened.
   *
   * @param $savePath The path to store the session.
   * @param $sessionName The name of the session.
   *
   * @return boolean If the open operation success.
   */
  public function open($savePath, $sessionName) {
    try {
      // get table to see if the table exists.
      $this->_tableRestProxy->getTable($this->_sessionContainer);
    } catch (ServiceException $e) {
      // cannot get the table, so create it
      $this->_tableRestProxy->createTable($this->_sessionContainer);
    }
    return TRUE;
  }

{'fraction_non_alphanumeric': 0.09927854454203262, 'fraction_numerical': 0.0028230865746549563, 'mean_word_length': 3.2627005347593583, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '2766721', 'n_tokens_mistral': 1731, 'n_tokens_neox': 1686, 'n_words': 528}