function profileSave() { if (!array_key_exists('profile_result', $_SERVER)) { return; } $pads = array(STR_PAD_RIGHT, STR_PAD_LEFT, STR_PAD_LEFT); $summary = array(array('name', 'calls', 'total')); $stopwatchs = array(); foreach (profileStopwatches() as $stopwatch) { $stopwatchs[] = $stopwatch; } usort($stopwatchs, function ($lhs, $rhs) { return $rhs->total() - $lhs->total(); }); foreach ($stopwatchs as $stopwatch) { $summary[] = array($stopwatch->name, $stopwatch->calls(), $stopwatch->total() > 2 ? round($stopwatch->total(), 2) . ' s' : round($stopwatch->total() * 1000, 2) . ' ms'); } $columnWidths = array_fill(0, count($summary[0]), 0); foreach ($summary as $summaryEntry) { foreach ($columnWidths as $columnWidthIndex => $columnWidth) { $columnWidths[$columnWidthIndex] = max($columnWidth, strlen($summaryEntry[$columnWidthIndex])); } } $output = ''; if (array_key_exists('HTTP_HOST', $_SERVER) && array_key_exists('REQUEST_URI', $_SERVER)) { $output .= 'url: ' . (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 'https' : 'http') . "://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}\n"; } $output .= 'codeTimestamp: ' . codeTimestamp() . "\n"; $output .= 'codeChanged: ' . (codeChanged() ? 'true' : 'false') . "\n"; $output .= 'staticCacheExpired: ' . (staticCacheExpired('profile') ? 'true' : 'false') . "\n"; $output .= "\n"; foreach ($summary as $summaryIndex => $summaryEntry) { if ($summaryIndex == 1) { $columns = array(); foreach ($summaryEntry as $columnIndex => $column) { $columns[] = str_pad('-', $columnWidths[$columnIndex], '-'); } $output .= implode('-+-', $columns) . "\n"; } $columns = array(); foreach ($summaryEntry as $columnIndex => $column) { $columns[] = str_pad($column, $columnWidths[$columnIndex], ' ', $pads[$columnIndex]); } $output .= implode(' | ', $columns) . "\n"; } file_put_contents($_SERVER['profile_result'], "\n\n" . $output, FILE_APPEND + LOCK_EX); }
function codeChanged() { static $includedFiles = array(); static $result = true; $newIncludedFiles = includedFile(); if (count($includedFiles) != count($newIncludedFiles)) { $fingerprintFile = $_SERVER['cachePath'] . '/codeBase_' . sha1(serialize($newIncludedFiles)) . '.timestamp'; $result = !is_file($fingerprintFile) || codeTimestamp() > filemtime($fingerprintFile); if ($result) { directory(dirname($fingerprintFile)); $touchResult = touch($fingerprintFile); enforce($touchResult, "Could not touch '{$fingerprintFile}'"); } $includedFiles = $newIncludedFiles; } return $result; }
/** * Static cache that is older than this timestamp is considered expired. */ function lastCodeChangeTimestamp() { static $resultCache = 0; static $resultCacheSet = false; $timestampFile = $_SERVER['cachePath'] . '/lastCodeChangeTimestamp'; $correlationFile = "{$timestampFile}.correlation"; if (!$resultCacheSet) { if (!is_file($timestampFile) || version_development && microtime(true) > filemtime($timestampFile) + 2) { file_put_contents($timestampFile, codeTimestamp()); } else { $resultCache = file_get_contents($timestampFile); enforce($resultCache !== false, "Could not read '{$timestampFile}'"); $resultCache = (double) $resultCache; } $resultCacheSet = true; } return $resultCache > 1 ? $resultCache : codeTimestamp(); }