/** * Returns a unique ID for the given Document * * @param DocumentInterface|array $document * @return string */ public static function getIdentifierForDocument($document) { $argumentIsArray = is_array($document); // Check if the real ID is set if ($argumentIsArray) { if (isset($document[Constants::DATA_ID_KEY])) { return $document[Constants::DATA_ID_KEY]; } } else { $value = $document->getId(Constants::DATA_ID_KEY); if ($value) { return $value; } } // If no read ID is defined check the most common $commonIdentifiers = array('id', 'uid', 'email'); foreach ($commonIdentifiers as $identifier) { if ($argumentIsArray) { if (isset($document[$identifier])) { return GeneralUtility::toString($document[$identifier]); } } else { $value = $document->valueForKey($identifier); if ($value) { return GeneralUtility::toString($value); } } } return sprintf('stairtower_%s_%s_document_%s', Constants::VERSION, getmypid(), microtime()); }
/** * Returns the Document instance defined by the arguments 'database' and 'identifier' and will throw an exception if * none is found and graceful is FALSE * * @param InputInterface $input * @param bool $graceful * @return DocumentInterface */ protected function findDataInstanceFromInput(InputInterface $input, $graceful = FALSE) { $objectIdentifier = $input->getArgument('identifier'); GeneralUtility::assertDataIdentifier($objectIdentifier); $database = $this->findDatabaseInstanceFromInput($input); $document = $database->findByIdentifier($objectIdentifier); if (!$document && !$graceful) { throw new InvalidDataException(sprintf('Object with ID "%s" not found in database %s', $objectIdentifier, $database->getIdentifier())); } return $document; }
/** * Unserialize the given data * * @param string $string * @throws \Cundd\PersistentObjectStore\Serializer\Exception if the data could not be unserialized * @return mixed */ public function unserialize($string) { $data = parent::unserialize($string); if ($data === NULL) { return NULL; } $databaseIdentifier = ObjectUtility::valueForKeyPathOfObject(Constants::DATA_META_KEY . '.' . Constants::DATA_DATABASE_KEY, $data, ''); if ($databaseIdentifier) { GeneralUtility::assertDatabaseIdentifier($databaseIdentifier); } return new Document($data, $databaseIdentifier); }
/** * Creates a new database * * @param string $identifier * @param array $rawData */ function __construct($identifier, $rawData = array()) { GeneralUtility::assertDatabaseIdentifier($identifier); $this->identifier = $identifier; if ($rawData) { $this->setRawData($rawData); } else { $this->rawData = new SplFixedArray(0); $this->objectData = new SplFixedArray(0); } $this->indexes[] = new IdentifierIndex(); }
/** * Create a new RequestInfo object * * @param Request $request * @param string $dataIdentifier * @param string $databaseIdentifier * @param string $method * @param string $specialHandlerAction */ function __construct($request, $dataIdentifier, $databaseIdentifier, $method, $specialHandlerAction = NULL) { if ($method) { GeneralUtility::assertRequestMethod($method); } if ($dataIdentifier) { GeneralUtility::assertDataIdentifier($dataIdentifier); } if ($databaseIdentifier) { GeneralUtility::assertDatabaseIdentifier($databaseIdentifier); } $this->method = $method; $this->dataIdentifier = $dataIdentifier; $this->databaseIdentifier = $databaseIdentifier; $this->specialHandlerAction = $specialHandlerAction ?: NULL; $this->request = $request; }
/** * Tries to handle a crashed system */ public function handleCrash() { $error = error_get_last(); if ($error !== NULL) { // Construct a helpful crash message $errorNumber = intval($error['type']); $errorFile = $error['file']; $errorLine = $error['line']; $errorMessage = $error['message']; $errorReport = []; $errorReport[] = sprintf('Server crashed with code %d and message "%s" in %s at %s', $errorNumber, $errorMessage, $errorFile, $errorLine); $errorReport[] = sprintf('Date/time: %s', $this->getTimeWithMicroseconds()->format('Y-m-d H:i:s.u')); $errorReport[] = sprintf('Current memory usage: %s', GeneralUtility::formatBytes(memory_get_usage(TRUE))); $errorReport[] = sprintf('Peak memory usage: %s', GeneralUtility::formatBytes(memory_get_peak_usage(TRUE))); // Try to rescue data $errorReport[] = $this->rescueData(); // Output and save the information $errorReport = implode(PHP_EOL, $errorReport); $errorReportPath = static::getRescueDirectory() . 'CRASH_REPORT.txt'; file_put_contents($errorReportPath, $errorReport); print $errorReport; } }
/** * Returns if the database with the given identifier exists * * @param string $databaseIdentifier Unique identifier of the database * @return bool */ public function databaseExists($databaseIdentifier) { GeneralUtility::assertDatabaseIdentifier($databaseIdentifier); if (Manager::hasObject($databaseIdentifier)) { return TRUE; } return $this->dataReader->databaseExists($databaseIdentifier); }
/** * Checks if the given JSON file can be loaded into memory * * The method tries to free enough memory if needed * * @param $filePath * @return bool */ public function checkMemoryForJsonFile($filePath) { $guessedMemory = $this->guessMemoryForJsonFile($filePath); $availableMemory = $this->getAvailableMemory(); // DebugUtility::pl('Available memory: %s', GeneralUtility::formatBytes($availableMemory)); // DebugUtility::pl('We will need about %s', GeneralUtility::formatBytes($guessedMemory)); if ($guessedMemory > $availableMemory) { // DebugUtility::pl('Please free %s bytes', GeneralUtility::formatBytes($guessedMemory - $availableMemory)); if (!$this->freeMemory($guessedMemory - $availableMemory)) { DebugUtility::pl('Required estimated memory amount of %s not available', GeneralUtility::formatBytes($guessedMemory - $availableMemory)); // throw new MemoryException(sprintf( // 'Required memory amount of %s not available', // GeneralUtility::formatBytes($guessedMemory - $availableMemory)) // ); } } }
/** * Returns the associated database * * @param string $databaseIdentifier */ public function setDatabaseIdentifier($databaseIdentifier) { GeneralUtility::assertDatabaseIdentifier($databaseIdentifier); $this->databaseIdentifier = $databaseIdentifier; }
/** * Returns an action method name if the path contains a special information identifier, otherwise FALSE * * @param Request $request * @param string $interface * @return string|bool */ protected static function getActionForRequestAndInterface($request, $interface) { $path = $request->getPath(); $method = $request->getMethod(); if ($path[0] === '/') { $path = substr($path, 1); } if ($path[0] === '_') { list($path, ) = explode('/', $path, 2); $handlerAction = GeneralUtility::underscoreToCamelCase(strtolower($method) . '_' . substr($path, 1)) . 'Action'; if (method_exists($interface, $handlerAction)) { return $handlerAction; } } return FALSE; }
/** * Perform a 'like' comparison for integers * * @param int $int * @param mixed $search * @return bool */ protected function performLikeInt($int, $search) { $searchAsInt = GeneralUtility::validateInteger($search); if ($searchAsInt === NULL) { return FALSE; } return $int === $searchAsInt; }
/** * @return array|mixed */ public function jsonSerialize() { return array('version' => $this->getVersion(), 'guid' => $this->getGuid(), 'startTime' => $this->getStartTime() ? $this->getStartTime()->format('r') : 'undefined', 'upTime' => $this->getUpTime() ? $this->getStartTime()->format('r') : 'undefined', 'memoryUsage' => GeneralUtility::formatBytes($this->getMemoryUsage()), 'memoryPeakUsage' => GeneralUtility::formatBytes($this->getMemoryPeakUsage())); }
/** * @test */ public function toStringTest() { $this->assertSame('Jesus saved my life', GeneralUtility::toString('Jesus saved my life')); $this->assertSame('Jesus saved my life', GeneralUtility::toString(array('Jesus', 'saved', 'my', 'life'))); $this->assertSame('1', GeneralUtility::toString(1)); $this->assertSame('0', GeneralUtility::toString(0)); $this->assertSame('1', GeneralUtility::toString(TRUE)); $this->assertSame('', GeneralUtility::toString(FALSE)); $this->assertSame('', GeneralUtility::toString(NULL)); $this->assertSame('NAN', GeneralUtility::toString(sqrt(-1.0))); $tempFile = tmpfile(); $this->assertContains('Resource id ', GeneralUtility::toString($tempFile)); fclose($tempFile); $dataInstance = new Document(array('my' => 'life')); $this->assertFalse(GeneralUtility::toString($dataInstance)); $object = new DummyObjectThatCanBeConvertedToString('my life'); $this->assertSame('my life', GeneralUtility::toString($object)); }