示例#1
0
 /**
  * Open the connection
  *
  * @return static
  *
  * @throws ConnectionException
  */
 public function connect()
 {
     if ($this->_client === null) {
         $remainingAttempts = (int) $this->_config()->getItem('connect_attempts', 1);
         while ($remainingAttempts > 0) {
             $remainingAttempts--;
             $exception = null;
             try {
                 if (empty($this->_availableHosts)) {
                     $this->_availableHosts = ValueAs::arr($this->_config()->getItem('hosts', 'localhost'));
                     $this->_availableHostCount = count($this->_availableHosts);
                     if ($this->_availableHostCount < 1) {
                         throw new ConnectionException('Could not find any configured hosts');
                     }
                 }
                 shuffle($this->_availableHosts);
                 $host = reset($this->_availableHosts);
                 $this->_socket = new DalSocket($host, (int) $this->_config()->getItem('port', 9160), ValueAs::bool($this->_config()->getItem('persist', false)));
                 $this->_socket->setConnectTimeout((int) $this->_config()->getItem('connect_timeout', 1000));
                 $this->_socket->setRecvTimeout((int) $this->_config()->getItem('receive_timeout', 1000));
                 $this->_socket->setSendTimeout((int) $this->_config()->getItem('send_timeout', 1000));
                 $this->_transport = new TFramedTransport($this->_socket);
                 $this->_protocol = new TBinaryProtocolAccelerated($this->_transport);
                 $this->_client = new CassandraClient($this->_protocol);
                 $this->_transport->open();
                 $this->_connected = true;
                 $this->_clearStmtCache();
                 $username = $this->_config()->getItem('username');
                 // @codeCoverageIgnoreStart
                 if ($username) {
                     $this->_client->login(new AuthenticationRequest(['credentials' => ['username' => $username, 'password' => $this->_config()->getItem('password', '')]]));
                 }
                 //@codeCoverageIgnoreEnd
                 $this->_switchDatabase(null, !$this->_socket->isPersistent());
             } catch (TException $e) {
                 $exception = $e;
             } catch (CqlException $e) {
                 $exception = $e;
             }
             if ($exception) {
                 $this->_removeCurrentHost();
                 $this->disconnect();
                 if ($remainingAttempts < 1) {
                     if (!$exception instanceof CqlException) {
                         $exception = CqlException::from($exception);
                     }
                     throw new ConnectionException('Failed to connect: ' . $exception->getMessage(), $exception->getCode(), $exception->getPrevious());
                 }
             } else {
                 break;
             }
         }
     }
     return $this;
 }
示例#2
0
 public function configure(ConfigSectionInterface $config)
 {
     $this->_config = $config;
     $mode = $config->getItem('mode', 'reader');
     if ($mode == 'reader') {
         $this->_reader = new Reader($config->getItem('database', new \Exception('No GeoIP Database defined')));
     } else {
         if ($mode == 'client') {
             $this->_reader = new Client($config->getItem('user_id', new \Exception("No maxmind user id specified")), $config->getItem('licence_key', new \Exception("No maxmind licence key specified")), ValueAs::arr($config->getItem('locales', 'en')), $config->getItem('host', 'geoip.maxmind.com'));
         }
     }
 }
示例#3
0
 /**
  * @param $resource
  *
  * @return ArrayHelper
  *
  * @throws \Exception
  */
 public static function create($resource)
 {
     if (is_object($resource)) {
         return new static((array) $resource);
     }
     if (is_array($resource)) {
         return new static($resource);
     }
     if (is_string($resource)) {
         return new static(ValueAs::arr($resource));
     }
     throw new \Exception(gettype($resource) . " is not currently supported");
 }
示例#4
0
 /**
  * Open the connection
  *
  * @return static
  *
  * @throws ConnectionException
  */
 public function connect()
 {
     $cfg = $this->_config();
     $this->_connection = $this->_newConnection();
     $servers = ValueAs::arr($cfg->getItem('servers', 'localhost'));
     foreach ($servers as $alias => $server) {
         $port = (int) $cfg->getItem($alias . '.port', $cfg->getItem('port', 11211));
         $persist = ValueAs::bool($cfg->getItem($alias . '.persist', $cfg->getItem('persist', true)));
         $weight = (int) $cfg->getItem($alias . '.weight', $cfg->getItem('weight', 1));
         $timeout = (int) $cfg->getItem($alias . '.timeout', $cfg->getItem('timeout', 1));
         $this->_addserver($server, $port, $persist, $weight, $timeout);
     }
     return $this;
 }
示例#5
0
 /**
  * Open the connection
  *
  * @return static
  *
  * @throws ConnectionException
  */
 public function connect()
 {
     if (!$this->isConnected()) {
         $this->_clearStmtCache();
         $this->_host = null;
         $this->_username = $this->_config()->getItem('username', 'root');
         $dsn = $this->_config()->getItem('dsn', null);
         if ($dsn === null) {
             $this->_host = $this->_config()->getItem('hostname', '127.0.0.1');
             $this->_port = $this->_config()->getItem('port', 3306);
             $dsn = sprintf("mysql:host=%s;port=%d", $this->_host, $this->_port);
         }
         $options = array_replace($this->_defaultOptions(), ValueAs::arr($this->_config()->getItem('options')));
         $remainingAttempts = (int) $this->_config()->getItem('connect_retries', 3);
         while ($remainingAttempts > 0 && $this->_connection === null) {
             try {
                 $cachedConnection = $this->_getCachedConnection();
                 if ($cachedConnection && $cachedConnection instanceof \PDO) {
                     $this->_connection = $cachedConnection;
                 } else {
                     $this->_connection = new \PDO($dsn, $this->_username, $this->_config()->getItem('password', ''), $options);
                 }
                 if (isset($options[\PDO::ATTR_EMULATE_PREPARES])) {
                     $this->_emulatedPrepares = $options[\PDO::ATTR_EMULATE_PREPARES];
                 } else {
                     $serverVersion = $this->_connection->getAttribute(\PDO::ATTR_SERVER_VERSION);
                     $this->_emulatedPrepares = version_compare($serverVersion, '5.1.17', '<');
                     $this->_connection->setAttribute(\PDO::ATTR_EMULATE_PREPARES, $this->_emulatedPrepares);
                 }
                 $this->_storeCachedConnection($this->_connection);
                 $this->_switchDatabase(null, true);
                 $remainingAttempts = 0;
             } catch (\Exception $e) {
                 $remainingAttempts--;
                 $this->_connection = null;
                 if ($remainingAttempts > 0) {
                     usleep(mt_rand(1000, 5000));
                 } else {
                     throw new ConnectionException("Failed to connect to PDO: " . $e->getMessage(), $e->getCode(), $e);
                 }
             }
         }
         $this->_lastConnectTime = time();
     }
     return $this;
 }
示例#6
0
 public function hydrate($data)
 {
     parent::hydrate($data);
     $this->result = ValueAs::arr($this->result);
 }
示例#7
0
 /**
  * Is Dispatch responsible for the incoming request
  *
  * @param Request $request
  *
  * @return bool
  */
 public function isDispatchRequest(Request $request)
 {
     $runOn = $this->_config->getItem('run_on', 'path');
     switch ($runOn) {
         case 'path':
             $match = $this->_config->getItem('run_match', 'res');
             return Strings::startsWith($request->getPathInfo() . '/', "/{$match}/");
         case 'subdomain':
             $matchCfg = $this->_config->getItem('run_match', 'static.,assets.');
             $subDomains = ValueAs::arr($matchCfg, ['static.']);
             return Strings::startsWithAny($request->getHost(), $subDomains);
         case 'domain':
             $matchCfg = $this->_config->getItem('run_match', null);
             $domains = ValueAs::arr($matchCfg, []);
             return Strings::endsWithAny($request->getHttpHost(), $domains, false);
     }
     return false;
 }
 /**
  * @param string $useName  Use this as the key
  * @param string $useValue Use this as the value
  *
  * @return string
  */
 public function getReadableValue($useName = null, $useValue = null)
 {
     $useName = $useName ?: ucwords(Strings::humanize($this->key));
     $useValue = $useValue ?: $this->value;
     if (is_array($useValue)) {
         $useValue = implode(', ', ValueAs::arr($useValue));
     }
     $nonIsComparators = [AdvancedFilterComparator::STARTS_WITH, AdvancedFilterComparator::NOT_STARTS_WITH, AdvancedFilterComparator::ENDS_WITH, AdvancedFilterComparator::NOT_ENDS_WITH];
     if (!in_array($this->comparator, $nonIsComparators)) {
         $useName .= ' is';
     }
     if (in_array($this->comparator, [AdvancedFilterComparator::BETWEEN, AdvancedFilterComparator::NOT_BETWEEN])) {
         list($min, $max) = explode(',', $useValue);
         return $useName . ' between ' . trim($min) . ' and ' . trim($max);
     }
     if (is_bool($useValue)) {
         return $useName . ' ' . ($useValue ? 'true' : 'false');
     }
     return $useName . ' ' . AdvancedFilterComparator::getDisplayValue($this->comparator) . ' ' . $useValue;
 }
示例#9
0
 /**
  * Generate the URI for the provided details
  *
  * @param $type
  * @param $lookup
  * @param $domain
  * @param $path
  * @param $file
  *
  * @return null|string
  */
 public function generateUriPath($type, $lookup, $domain, $path, $file)
 {
     $parts = [];
     //Include the map type
     $parts[] = $type;
     //When lookup parts are avilable, e.g. vendor/package, include them
     if (is_array($lookup)) {
         foreach ($lookup as $lookupPart) {
             $parts[] = $lookupPart;
         }
     }
     $parts[] = static::hashDomain($domain);
     //If not path is available, assume you are requesting the base path
     if (empty($path)) {
         $partHash = 'b';
     } else {
         //Build the hashable path
         $partHash = $this->_mapper->hashDirectoryArray(ValueAs::arr(explode('/', $path)));
     }
     $parts[] = $partHash;
     $baseDir = $this->getBasePath($this->_mapper, $type, (array) $lookup);
     $filePath = Path::build($baseDir, $path, $file);
     $fileHash = $this->_dispatcher->getFileHash($filePath);
     if ($fileHash === null) {
         //File hash doesnt exist in the cache, so lets look it up
         $fullPath = Path::build($this->_dispatcher->getBaseDirectory(), $filePath);
         $fileHash = ResourceGenerator::getFileHash($fullPath);
         if (!$fileHash) {
             //If we cant get a hash of the file, its unlikely it exists
             return null;
         }
         //Cache the entry, to optimise should the same resource be re-requested
         $this->_dispatcher->addFileHashEntry($filePath, $fileHash);
     }
     $parts[] = substr($fileHash, 0, 7);
     //Include the file extension
     $parts[] = $file;
     return implode('/', $parts);
 }
示例#10
0
 /**
  * Convert an alias URL to the correct path
  *
  * @param $parts
  *
  * @return null|string
  */
 public function aliasPath($parts)
 {
     $check = $parts[1];
     $aliases = ValueAs::arr($this->_config->getItem('aliases'));
     if (isset($aliases[$check])) {
         return $aliases[$check];
     }
     return null;
 }
示例#11
0
 /**
  * @param $data string Serialized representation of the authed user
  *
  * @throws \Exception
  *
  * @return IAuthedUser|self
  */
 public function unserialize($data)
 {
     $json = json_decode($data);
     if (json_last_error() === JSON_ERROR_NONE) {
         $this->_data = (array) $json;
         $this->_data['data'] = ValueAs::arr($this->_data['data'], []);
     } else {
         throw new \Exception('Unable to unserialize login cookie', json_last_error());
     }
     return $this;
 }
 /**
  * Hydrate the public properties
  *
  * @param $data
  */
 public function hydrate($data)
 {
     parent::hydrate($data);
     $this->values = ValueAs::arr($this->values);
 }
示例#13
0
 public function removeClass($class)
 {
     if (func_num_args() === 1) {
         $classes = ValueAs::arr($class);
     } else {
         $classes = func_get_args();
     }
     foreach ($classes as $class) {
         if (is_array($class)) {
             foreach ($class as $c) {
                 $this->_removeClass($c);
             }
         } else {
             $this->_removeClass($class);
         }
     }
     return $this;
 }
示例#14
0
 /**
  * Create a new asset manager based on the calling class
  *
  * @param object $callee
  * @param string $forceType DirectoryMapper::MAP_*
  * @param mixed  $lookupParts
  *
  * @throws \Exception
  */
 public function __construct($callee = null, $forceType = null, $lookupParts = null)
 {
     $this->_mapType = $forceType;
     if ($forceType === null) {
         if (!is_object($callee)) {
             throw new \Exception("You cannot construct an asset manager without specifying " . "either a callee or forceType");
         }
         $this->_mapType = $this->lookupMapType($callee);
     }
     //If provided, use the lookup parts
     if ($lookupParts !== null) {
         $this->_lookupParts = ValueAs::arr($lookupParts);
     }
 }
示例#15
0
 public function processDocBlockTag($tag, $value)
 {
     switch (strtolower($tag)) {
         case 'label':
         case 'inputtype':
         case 'type':
         case 'input':
             break;
         case 'values':
             $this->setOption($tag, ValueAs::arr($value));
             break;
         case 'name':
             $this->_form->addPropertyAlias($value, $this->_property);
             $this->setOption($tag, $value);
             break;
         case 'id':
             $this->setOption($tag, $value);
             break;
         case 'novalidate':
         case 'multiple':
         case 'autofocus':
         case 'required':
             $this->setAttribute($tag, null);
             break;
         case 'disabled':
             $this->setAttribute($tag, true);
             break;
         case 'nolabel':
             $this->setLabelPosition(FormElement::LABEL_NONE);
             break;
         case 'labelbefore':
             $this->setLabelPosition(FormElement::LABEL_BEFORE);
             break;
         case 'labelafter':
             $this->setLabelPosition(FormElement::LABEL_AFTER);
             break;
         case 'labelposition':
         case 'label_position':
         case 'labelpos':
             $this->setLabelPosition($value);
             break;
         default:
             $this->setAttribute($tag, $value);
             break;
     }
 }