/** * @covers Monolog\Registry::clear */ public function testClearClears() { Registry::addLogger(new Logger('test1'), 'log'); Registry::clear(); $this->setExpectedException('\\InvalidArgumentException'); Registry::getInstance('log'); }
/** * Removes the not assigned tokens. * * @param Token[] $tokens */ public function cleanUpUnassignedTokens(array $tokens) { $this->em->clear(); foreach ($tokens as $token) { $token = $this->em->merge($token); $this->em->remove($token); } $this->em->flush(); }
/** * Search for open jobs and executes them * then show a report about the done job. */ protected function executeJobs() { // Get the Jobs to be processed. $jobsInQueue = $this->em->getRepository('CampaignChainCoreBundle:Job')->getOpenJobsForScheduler($this->scheduler); if (empty($jobsInQueue)) { return; } $this->io->section('Executing jobs now:'); $this->logger->info('Executing {counter} jobs now:', ['counter' => count($jobsInQueue)]); $this->io->progressStart(count($jobsInQueue)); foreach ($jobsInQueue as $jobInQueue) { // Execute job. $this->executeJob($jobInQueue); $this->io->progressAdvance(); } // ensure that the progress bar is at 100% $this->io->progressFinish(); $this->em->clear(); // Get the processed jobs. $jobsProcessed = $this->em->getRepository('CampaignChainCoreBundle:Job')->getProcessedJobsForScheduler($this->scheduler); if (empty($jobsProcessed)) { return; } // Display the results of the execution. $tableHeader = ['Job ID', 'Operation ID', 'Process ID', 'Job Name', 'Job Start Date', 'Job End Date', 'Duration', 'Status', 'Message']; $outputTableRows = []; foreach ($jobsProcessed as $jobProcessed) { $startDate = null; $endDate = null; if ($jobProcessed->getStartDate()) { $startDate = $jobProcessed->getStartDate()->format('Y-m-d H:i:s'); } if ($jobProcessed->getEndDate()) { $endDate = $jobProcessed->getEndDate()->format('Y-m-d H:i:s'); } $jobData = [$jobProcessed->getId(), $jobProcessed->getActionId(), $jobProcessed->getPid(), $jobProcessed->getName(), $startDate, $endDate, $jobProcessed->getDuration() . ' ms', $jobProcessed->getStatus(), $jobProcessed->getMessage()]; $outputTableRows[] = $jobData; if (Job::STATUS_ERROR === $jobProcessed->getStatus()) { $context = array_combine($tableHeader, $jobData); $this->logger->error($jobProcessed->getMessage(), $context); } } $this->io->text('Results of executed actions:'); $this->io->table($tableHeader, $outputTableRows); }
/** * Download a file. * Outputs HTTP headers and file content for download * @param $filePath string the location of the file to be sent * @param $mediaType string the MIME type of the file, optional * @param $inline print file as inline instead of attachment, optional * @return boolean */ function downloadFile($filePath, $mediaType = null, $inline = false, $fileName = null) { $result = null; if (HookRegistry::call('FileManager::downloadFile', array(&$filePath, &$mediaType, &$inline, &$result, &$fileName))) { return $result; } if (is_readable($filePath)) { if ($mediaType === null) { // If the media type wasn't specified, try to detect. $mediaType = String::mime_content_type($filePath); if (empty($mediaType)) { $mediaType = 'application/octet-stream'; } } if ($fileName === null) { // If the filename wasn't specified, use the server-side. $fileName = basename($filePath); } Registry::clear(); // Free some memory header("Content-Type: {$mediaType}"); header('Content-Length: ' . filesize($filePath)); header('Content-Disposition: ' . ($inline ? 'inline' : 'attachment') . "; filename=\"{$fileName}\""); header('Cache-Control: private'); // Workarounds for IE weirdness header('Pragma: public'); // Beware of converting to instance call // https://github.com/pkp/pkp-lib/commit/82f4a36db406ecac3eb88875541a74123e455713#commitcomment-1459396 FileManager::readFile($filePath, true); return true; } else { return false; } }
/** * Download a file. * Outputs HTTP headers and file content for download * @param $filePath string the location of the file to be sent * @param $mediaType string the MIME type of the file, optional * @param $inline print file as inline instead of attachment, optional * @return boolean */ function downloadFile($filePath, $mediaType = null, $inline = false, $fileName = null) { $result = null; if (HookRegistry::call('FileManager::downloadFile', array(&$filePath, &$mediaType, &$inline, &$result, &$fileName))) { return $result; } $postDownloadHookList = array('FileManager::downloadFileFinished', 'UsageEventPlugin::getUsageEvent'); if (is_readable($filePath)) { if ($mediaType === null) { // If the media type wasn't specified, try to detect. $mediaType = String::mime_content_type($filePath); if (empty($mediaType)) { $mediaType = 'application/octet-stream'; } } if ($fileName === null) { // If the filename wasn't specified, use the server-side. $fileName = basename($filePath); } $postDownloadHooks = null; $hooks = HookRegistry::getHooks(); foreach ($postDownloadHookList as $hookName) { if (isset($hooks[$hookName])) { $postDownloadHooks[$hookName] = $hooks[$hookName]; } } unset($hooks); Registry::clear(); // Stream the file to the end user. header("Content-Type: {$mediaType}"); header('Content-Length: ' . filesize($filePath)); header('Content-Disposition: ' . ($inline ? 'inline' : 'attachment') . "; filename=\"{$fileName}\""); header('Cache-Control: private'); // Workarounds for IE weirdness header('Pragma: public'); // Beware of converting to instance call // https://github.com/pkp/pkp-lib/commit/82f4a36db406ecac3eb88875541a74123e455713#commitcomment-1459396 FileManager::readFile($filePath, true); if ($postDownloadHooks) { foreach ($postDownloadHooks as $hookName => $hooks) { HookRegistry::setHooks($hookName, $hooks); } } $returner = true; } else { $returner = false; } HookRegistry::call('FileManager::downloadFileFinished', array(&$returner)); return $returner; }
/** * Download a file. * Outputs HTTP headers and file content for download * @param $filePath string the location of the file to be sent * @param $mediaType string the MIME type of the file, optional * @param $inline print file as inline instead of attachment, optional * @return boolean */ function downloadFile($filePath, $mediaType = null, $inline = false) { $result = null; if (HookRegistry::call('FileManager::downloadFile', array(&$filePath, &$mediaType, &$inline, &$result))) { return $result; } if (is_readable($filePath)) { if ($mediaType == null) { $mediaType = String::mime_content_type($filePath); if (empty($mediaType)) { $mediaType = 'application/octet-stream'; } } Registry::clear(); // Free some memory header("Content-Type: {$mediaType}"); header("Content-Length: " . filesize($filePath)); header("Content-Disposition: " . ($inline ? 'inline' : 'attachment') . "; filename=\"" . basename($filePath) . "\""); header("Cache-Control: private"); // Workarounds for IE weirdness header("Pragma: public"); import('lib.pkp.classes.file.FileManager'); FileManager::readFile($filePath, true); return true; } else { return false; } }
protected function setUp() { Registry::clear(); }
function run($f3) { $test = new \Test(); Registry::clear('Cron'); //plugin config $f3->config('tests/jobs.ini'); $f3->mset(array('log' => FALSE, 'cli' => TRUE, 'web' => FALSE, 'clipath' => 'index.php'), 'CRON.'); $cron = Cron::instance(); $test->expect(!$cron->log && $cron->cli && !$cron->web && count($cron->jobs) == 3 && $cron->clipath == 'index.php', 'Initial config'); $test->expect($cron->jobs['JobC'][1] === '*/2 0-23 1-3,4-5 4,5,6 *', 'Expression containing commas correctly restored'); $test->expect(isset($f3->ROUTES['/cron']), 'Route automatically defined'); //async auto-detection $async = function_exists('exec') && exec('php -r "echo 1+3;"') == '4'; $test->expect($cron->async === $async, 'Async auto-detection: ' . ($async ? 'ON' : 'OFF')); //expression parsing $test->expect($cron->parseExpr('1 2 3,9 4 5') === array(array(1), array(2), array(3, 9), array(4), array(5)), 'parseExpr(): single values'); $test->expect($cron->parseExpr('1-3 2-5 3-2,1 4 *') === array(array(1, 2, 3), array(2, 3, 4, 5), array(3, 2, 1), array(4), range(0, 6)), 'parseExpr(): ranges and wildcards'); $test->expect($cron->parseExpr('0,1-6/2,10 2 0-10/3 4 */3') === array(array(0, 1, 3, 5, 10), array(2), array(0, 3, 6, 9), array(4), array(0, 3, 6)), 'parseExpr(): step values on ranges and wildcards'); $test->expect($cron->parseExpr('@yearly') === $cron->parseExpr('0 0 1 1 *') && $cron->parseExpr('@annually') === $cron->parseExpr('0 0 1 1 *') && $cron->parseExpr('@monthly') === $cron->parseExpr('0 0 1 * *') && $cron->parseExpr('@weekly') === $cron->parseExpr('0 0 * * 0') && $cron->parseExpr('@daily') === $cron->parseExpr('0 0 * * *') && $cron->parseExpr('@hourly') === $cron->parseExpr('0 * * * *') && $cron->parseExpr('@weekend') === $cron->parseExpr('0 8 * * 6') && $cron->parseExpr('@lunch') === $cron->parseExpr('0 12 * * *'), 'parseExpr(): schedule presets'); $test->expect($cron->parseExpr('1 2 3 4') === FALSE && $cron->parseExpr('*-2 2 3 4 5') === FALSE && $cron->parseExpr('1 2 3-5/4 4 5') === FALSE && $cron->parseExpr('1 2 3 4 sun') === FALSE && $cron->parseExpr('@dinner') === FALSE, 'parseExpr(): invalid expressions or presets return FALSE'); //timestamp parsing $time = mktime(1, 2, 3, 4, 5, 2015); // 2015-04-05 01:02:03 (Saturday) $test->expect($cron->parseTimestamp($time) == array(2, 1, 5, 4, 0), 'parseTimestamp()'); //due check $test->expect($cron->isDue('JobA', $time) && $cron->isDue('JobB', $time) && $cron->isDue('JobC', $time), 'isDue() returns TRUE if the requested job is due at the given time'); $test->expect($cron->isDue('Foo', $time) === FALSE || $cron->isDue('joba', $time) === FALSE, 'isDue() returns FALSE if the requested job doesn\'t exist or if the case doesn\'t match'); $cron->set('JobD', function () use($f3) { $f3->job .= 'D'; }, '* * 4 * *'); $cron->set('JobE', 'Jobs->jobE', '2 1 5 4 1'); $cron->set('JobF', 'Jobs->jobF', '*/3 * * * *'); $test->expect(!$cron->isDue('JobD', $time) && !$cron->isDue('JobE', $time) && !$cron->isDue('JobF', $time), 'isDue() returns FALSE if the requested job is not due at the given time'); //job execution $f3->job = ''; $cron->execute('JobA', FALSE); $cron->execute('JobD', FALSE); $test->expect($f3->job === 'AD', 'Serial job execution'); $cron->execute('JobC', FALSE); $test->expect(TRUE, 'Silently fail on a non-existing job handler'); //logging @unlink($logfile = $f3->LOGS . 'cron.log'); $cron->log = TRUE; $cron->execute('JobA', FALSE); $cron->log = FALSE; $test->expect(file_exists($logfile) && count(file($logfile)) == 1, 'Logging to file'); //schedule running $f3->job = ''; $cron->run($time, FALSE); $test->expect($f3->job = 'AB', 'Run scheduler, i.e executes all due jobs'); //async job execution if ($async) { $cron->set('test1', 'Jobs->test1', '* * * * *'); $cron->set('test2', 'Jobs->test2', '* * * * *'); @unlink($testfile = $f3->TEMP . 'cron-test.txt'); $cron->execute('test1', TRUE); $cron->execute('test2', TRUE); $async_ok = FALSE; //wait for async processes to complete $start = microtime(TRUE); $loop = array(0.1, 4); //loop (step=0.1s / max=4s) while (microtime(TRUE) - $start < $loop[1]) { usleep($loop[0] * 1000000); if (file_exists($testfile) && preg_match('/([ABCD]){4}/', file_get_contents($testfile), $m) && array_unique($m) === $m) { $async_ok = TRUE; break; } } $test->expect($async_ok, 'Parallel job execution'); } //web access forbidden by default $f3->HALT = FALSE; $f3->clear('ERROR'); $f3->ONERROR = function () { }; $f3->mock('GET /cron'); $test->expect(isset($f3->ERROR['code']) && $f3->ERROR['code'] === 404, 'Web access forbidden by default'); $f3->set('results', $test->results()); }
/** * Download a file. * Outputs HTTP headers and file content for download * @param $filePath string the location of the file to be sent * @param $type string the MIME type of the file, optional * @param $inline print file as inline instead of attachment, optional * @return boolean */ function downloadFile($filePath, $type = null, $inline = false, $filename = null) { if (is_null($filename)) { $filename = basename($filePath); } $u_agent = $_SERVER['HTTP_USER_AGENT']; if (preg_match('/MSIE/i', $u_agent) && !preg_match('/Opera/i', $u_agent) || strpos($_SERVER['HTTP_USER_AGENT'], 'Trident/7.0; rv:11.0') !== false) { $filename = iconv('utf8', 'big5', $filename); } //$filename = basename($filePath); $result = null; if (HookRegistry::call('FileManager::downloadFile', array(&$filePath, &$type, &$inline, &$result))) { return $result; } if (is_readable($filePath)) { if ($type == null) { $type = String::mime_content_type($filePath); if (empty($type)) { $type = 'application/octet-stream'; } } Registry::clear(); // Free some memory header("Content-Type: {$type}"); header("Content-Length: " . filesize($filePath)); header("Content-Disposition: " . ($inline ? 'inline' : 'attachment') . "; filename=\"" . $filename . "\""); header("Cache-Control: private"); // Workarounds for IE weirdness header("Pragma: public"); import('file.FileManager'); FileManager::readFile($filePath, true); return true; } else { return false; } }
/** Wrap-up @return NULL **/ function __destruct() { Registry::clear(get_called_class()); }
/** * Download a file. * Outputs HTTP headers and file content for download * @param $filePath string the location of the file to be sent * @param $mediaType string the MIME type of the file, optional * @param $inline print file as inline instead of attachment, optional * @return boolean */ function downloadFile($filePath, $mediaType = null, $inline = false, $fileName = null) { $result = null; if (HookRegistry::call('FileManager::downloadFile', array(&$filePath, &$mediaType, &$inline, &$result, &$fileName))) { return $result; } $postDownloadHookList = array('FileManager::downloadFileFinished', 'UsageEventPlugin::getUsageEvent'); if (is_readable($filePath)) { if ($mediaType === null) { // If the media type wasn't specified, try to detect. $mediaType = PKPString::mime_content_type($filePath); if (empty($mediaType)) { $mediaType = 'application/octet-stream'; } } if ($fileName === null) { // If the filename wasn't specified, use the server-side. $fileName = basename($filePath); } // Free some memory $postDownloadHooks = null; $hooks = HookRegistry::getHooks(); foreach ($postDownloadHookList as $hookName) { if (isset($hooks[$hookName])) { $postDownloadHooks[$hookName] = $hooks[$hookName]; } } unset($hooks); Registry::clear(); // Stream the file to the end user. header("Content-Type: {$mediaType}"); header('Content-Length: ' . filesize($filePath)); header('Content-Disposition: ' . ($inline ? 'inline' : 'attachment') . "; filename=\"{$fileName}\""); header('Cache-Control: private'); // Workarounds for IE weirdness header('Pragma: public'); $this->readFileFromPath($filePath, true); if ($postDownloadHooks) { foreach ($postDownloadHooks as $hookName => $hooks) { HookRegistry::setHooks($hookName, $hooks); } } $returner = true; } else { $returner = false; } HookRegistry::call('FileManager::downloadFileFinished', array(&$returner)); return $returner; }
/** * Download a file. * Outputs HTTP headers and file content for download * @param $filePath string the location of the file to be sent * @param $type string the MIME type of the file, optional * @param $inline print file as inline instead of attachment, optional * @return boolean */ function downloadFile($filePath, $type = null, $inline = false, $label = null, $repo = false) { if ($repo == false) { if (HookRegistry::call('FileManager::downloadFile', array(&$filePath, &$type, &$inline, &$result))) { return $result; } if (is_readable($filePath)) { if ($type == null) { $type = String::mime_content_type($filePath); if (empty($type)) { $type = 'application/octet-stream'; } } Registry::clear(); // Free some memory header("Content-Type: {$type}"); header("Content-Length: " . filesize($filePath)); header("Content-Disposition: " . ($inline ? 'inline' : 'attachment') . "; filename=\"" . basename($filePath) . "\""); header("Cache-Control: private"); header("Pragma: public"); import('lib.pkp.classes.file.FileManager'); FileManager::readFile($filePath, true); return true; } else { return false; } } else { //return file from repository $ch = curl_init($filePath); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $c = curl_exec($ch); $info = curl_getinfo($ch); curl_close($ch); if ($label == null) { $filename = str_replace("/content", "", $filePath); $filename = substr($filename, strrpos($filename, "/") + 1); } header("Etag: " . md5($filename)); header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); header("Content-Type:" . $info['content_type']); header("Content-Disposition: " . ($inline ? 'inline' : 'attachment') . "; filename=\"" . $label . "\""); header("Cache-Control: private"); header("Pragma: public"); ob_clean(); flush(); readfile($filePath); } }
public function __destruct() { // free embedded relation cache from memory \Registry::clear($this->cid); }