public function init() { if (!isset($this['realUrl'])) { $this['realUrl'] = \PMVC\plug('url')->realUrl(); } \PMVC\callPlugin('dispatcher', 'attach', array($this, \PMVC\Event\MAP_REQUEST)); }
/** * Handles signals. * SIGHUP: kill -HUP pid * SIGINT: ctrl + c * SIGTERM: kill pid */ public function __invoke($signo) { $plug = \PMVC\plug('supervisor'); $name = array(SIGINT => 'SIGINT', SIGTERM => 'SIGTERM', SIGHUP => 'SIGHUP'); $plug->log('Recieve ' . $name[$signo]); if (!empty($plug['parent'])) { $plug['isStopMe'] = true; return; } static $term_count = 0; switch ($signo) { case SIGINT: case SIGTERM: $plug->log('Ask start to shutting down...'); $term_count++; if ($term_count < 5) { $plug->stop($signo); } else { $plug->stop(SIGKILL); } break; case SIGHUP: $plug['start']->restart(); break; } }
public function testToStreet() { $map = \PMVC\plug($this->_plug); $map['center'] = '46.414382,10.013988'; $expected = 'https://maps.googleapis.com/maps/api/streetview?location=46.414382%2C10.013988&size=640x300'; $this->assertEquals($expected, $map->toStreet()); }
/** * IDp wrappers initializer */ public function initialize() { if (!$this->config['FLICKR_APP_ID'] || !$this->config['FLICKR_APP_SECRET']) { throw new \Exception("Your application id and secret are required in order to connect to {$this->providerId}.", 4); } $this->api = \PMVC\plug('flickr', array('appId' => $this->config['FLICKR_APP_ID'], 'appSecret' => $this->config['FLICKR_APP_SECRET'])); }
function testGetOutput() { $debug = \PMVC\plug($this->_plug); $debug['output'] = \PMVC\plug('output', [_CLASS => __NAMESPACE__ . '\\fakeOutput']); $o = $debug->getOutput(); $this->assertContains('output', print_r($o, true)); }
function testGetPalette() { $c = PMVC\plug($this->_plug); $palette = $c->getPalette(DEMO_PIC); $this->assertTrue(is_array($palette)); $this->assertEquals(10, count($palette)); }
function testOtp() { $secret = PMVC\plug('otp')->getNewSecret('test'); $one = PMVC\plug('otp')->getOneCode(); $isValidated = PMVC\plug('otp')->validate(array('one' => $one)); $this->assertTrue($isValidated); }
function testHash() { $guid = \PMVC\plug(TestPlug); $imageDb = $guid->getDb('image'); $hash = $imageDb->getHash([]); $this->assertEquals(md5($hash->json), $hash->md5); }
public function writeToFile($filename) { if (empty($this->sheets)) { self::log("Error in " . __CLASS__ . "::" . __FUNCTION__ . ", no worksheets defined."); return; } foreach ($this->sheets as $sheet_name => $sheet) { self::finalizeSheet($sheet_name); //making sure all footers have been written } $zip = \PMVC\plug('zip'); $zip->open($filename); $zip->addFromString("docProps/app.xml", self::buildAppXML()); $zip->addFromString("docProps/core.xml", self::buildCoreXML()); $zip->addFromString("_rels/.rels", self::buildRelationshipsXML()); foreach ($this->sheets as $sheet) { $zip->addFile($sheet->filename, "xl/worksheets/" . $sheet->xmlname); } if (!empty($this->shared_strings)) { $zip->addFile($this->writeSharedStringsXML(), "xl/sharedStrings.xml"); } $zip->addFromString("xl/workbook.xml", self::buildWorkbookXML()); $zip->addFile($this->writeStylesXML(), "xl/styles.xml"); $zip->addFromString("[Content_Types].xml", self::buildContentTypesXML()); $zip->addFromString("xl/_rels/workbook.xml.rels", self::buildWorkbookRelsXML()); }
function testGen() { $plug = PMVC\plug($this->_plug); $regs = [['either', 'https', 'http'], ['maybe', 'http']]; $s = $plug->gen(...$regs); $this->assertEquals($s, '/(https|http)(http)?/'); }
function askMinions($minionsServer, $curl, $more = null) { $curlPlug = \PMVC\plug(self::curl); $callback = $curl[self::callback]; $options = $curl[self::options]; if (is_string($minionsServer)) { $host = $minionsServer; } else { if (!isset($minionsServer[0])) { return !trigger_error("Minions server config not correct. " . var_export($minionsServer, true), E_USER_WARNING); } $host = $minionsServer[0]; if (isset($minionsServer[1])) { $options += $minionsServer[1]; } } $curlPlug->post($host, function ($r) use($callback, $minionsServer) { $json = json_decode($r->body); if (!isset($json->r)) { return !trigger_error("Minions respond failed. " . var_export($json, true)); } $serverTime = $json->serverTime; $r =& $json->r; unset($json); $r->body = gzuncompress(urldecode($r->body)); call_user_func($callback, $r, array($minionsServer, $serverTime)); }, array('curl' => $options, 'more' => $more)); }
public function toGd() { if (!$this->_gd) { $this->_gd = \PMVC\plug('image')->create($this); } return $this->_gd; }
/** * construct */ public function __construct($return, $curlHelper, $more = []) { $oCurl = $curlHelper->getInstance(); $this->errno = curl_errno($oCurl); $this->code = curl_getinfo($oCurl, CURLINFO_HTTP_CODE); $header_size = curl_getinfo($oCurl, CURLINFO_HEADER_SIZE); if (empty($header_size)) { return; } $this->rawHeader = substr($return, 0, $header_size); $this->header = $this->getHeaders($this->rawHeader); if ($curlHelper->manualFollow && isset($this->header['location'])) { $curlHelper->setOptions($this->header['location'], null, function ($r) { $this->body = $r->body; }); $curlHelper->process(); } elseif (!empty($this->header['content-encoding']) && 'gzip' === $this->header['content-encoding']) { $this->body = gzinflate(substr($return, $header_size + 10, -8)); } else { $this->body = substr($return, $header_size); } if (!empty($more)) { $pCurl = \PMVC\plug('curl'); foreach ($more as $key) { $info = new SplFixedArray(2); $info[0] = curl_getinfo($oCurl, $key); $info[1] = $pCurl->info_to_str()->one($key); $this->more[$key] = $info; } } }
public function getStorage() { if (empty($this['GUID_DB'])) { return !trigger_error('Global setting not setted. "GUID_DB"', E_USER_WARNING); } return \PMVC\plug($this['GUID_DB']); }
function testDiff() { $a = 'tw.yahoo.com'; $b = 'google.com'; $result = PMVC\plug('simple_diff')->diff($a, $b); $this->assertEquals('.com', $result[2]); }
/** * Returns the colors of the image in an array, ordered in descending order, where * the keys are the colors, and the values are the count of the color. * @param $filePath the path to the local image file * @return an array keyed by hex color value, mapping to the frequency of use in the images */ public static function GenerateFromLocalImage(ImageFile $file) { $pImage = \PMVC\plug('image'); // resize the image for a reasonable amount of colors $previewSize = new ImageSize(150, 150); $srcSize = $file->getSize(); $scale = 1; if ($srcSize->w > 0) { $scale = min($previewSize->w / $srcSize->w, $previewSize->h / $srcSize->h); } if ($scale < 1) { $destSize = new ImageSize(floor($scale * $srcSize->w), floor($scale * $srcSize->h)); } else { $destSize = new ImageSize($srcSize->w, $srcSize->h); } $image_resized = $pImage->create($destSize); $image_orig = $pImage->create($file); //WE NEED NEAREST NEIGHBOR RESIZING, BECAUSE IT DOESN'T ALTER THE COLORS imagecopyresampled($image_resized, $image_orig, 0, 0, 0, 0, $destSize->w, $destSize->h, $srcSize->w, $srcSize->h); // walk the image counting colors $hexarray = []; $pImage->process($destSize, [$image_resized], function ($point, $im) use(&$hexarray) { $index = imagecolorat($im, $point->x, $point->y); $Colors = imagecolorsforindex($im, $index); $hexarray[] = (new BaseColor($Colors['red'], $Colors['green'], $Colors['blue']))->toHex(); }); $hexarray = array_count_values($hexarray); natsort($hexarray); $hexarray = array_slice($hexarray, -10, 10); $hexarray = array_reverse($hexarray, true); return $hexarray; }
public function __construct($db) { if (!empty($this->groupKey)) { $this->groupTable = \PMVC\plug('guid')->getDb('GlobalKey')[$this->groupKey]; } $this->db = $db->getDb($this->groupTable, $this->groupKey); }
function testToImage() { $f = __DIR__ . '/resource/demo.jpg'; $pImage = \PMVC\plug($this->_plug); $image = $pImage->toImage($f); $this->assertEquals('jpg', $image->getExt()); }
public function __construct(callable $callBack = null) { $plug = \PMVC\plug(PLUGIN); $plug->log("Monitor starting."); while (empty($plug[IS_STOP_ALL]) || count($plug[CHILDREN])) { if (!empty($plug[MY_PARENT])) { break; } // Check for exited children $pid = pcntl_wait($status, WNOHANG); if (isset($plug[CHILDREN][$pid])) { $exitCode = pcntl_wexitstatus($status); $plug->log("Child {$pid} was stopped with exit code of {$exitCode}"); if (!$plug[IS_STOP_ALL] && 1 !== $exitCode) { $callbackId = $plug[CHILDREN][$pid]; $plug['start']->restore($callbackId); } $plug->cleanPid($pid); } pcntl_signal_dispatch(); if (empty($plug[CHILDREN])) { $plug[IS_STOP_ALL] = true; break; } if ($callBack && empty($plug[IS_STOP_ALL])) { call_user_func($callBack); } // php will eat up your cpu if you don't have this usleep(50000); pcntl_signal_dispatch(); } $plug->log("Monitor was exited."); }
public function write($session_id, $session_data) { $curl = \PMVC\plug('curl'); $url = $this->caller['api'] . $session_id; $curl->post($url, null, array('data' => $session_data)); $curl->process(); }
function testParseDataTypeAnnotation() { $plug = \PMVC\plug($this->_plug); $annotation = $plug->get([__NAMESPACE__ . '\\fakeClass', 'fake2']); $dataType = $annotation->getDataType('params'); $expected = [['type' => 'string', 'name' => '$abc', '111 222'], ['type' => 'array', 'name' => '$def', '333 444']]; $this->assertEquals($expected, $dataType); }
function testGetTempFolder() { $prefix = 'unit_test_dir'; $plug = PMVC\plug($this->_plug); $dir = $plug->dir($prefix); $this->assertContains($prefix, $dir); $this->assertTrue(is_dir($dir)); }
function testOnMapRequest() { $p = \PMVC\plug($this->_plug); $url = \PMVC\plug('url', ['REQUEST_URI' => '/fake/hello', 'SCRIPT_NAME' => '/fake/']); $p->onMapRequest(); $r = \PMVC\plug('controller')->getRequest(); $this->assertEquals(['hello'], \PMVC\get($r)); }
function testExcel() { $excel = PMVC\plug($this->_plug); $data = array(array('year', 'month', 'amount'), array('2003', '1', '220'), array('2003', '2', '153.5')); $writer = $excel->create(); $writer->writeSheet($data); $writer->writeToFile('output.xlsx'); }
function testGetFileTime() { $p = PMVC\plug($this->_plug); $f = $p->file(__FILE__); $atime = fileatime($f->realPath()); $time = date('Y/m/d H:i:s', $atime); $this->assertEquals($time, (string) $f->getTime()->atime()); }
function testEmptyDimensionUrl() { $c = \PMVC\plug('controller'); $c->setApp('fake'); $p = \PMVC\plug($this->_plug); $result = $p->onMapRequest(new FakeSubject()); $this->assertFalse($result); }
function testCssParse() { $parser = PMVC\plug('html_parser'); $text = 'hi body'; $html = '<html><body id="bd">' . $text . '</body></html>'; $qp = $parser->css($html, '#bd'); $this->assertEquals($text, $qp->text()); }
function testGetUnderscoreToArray() { # load dot file $actual = PMVC\plug('dotenv')->getUnderscoreToArray('./.env.example'); $expected = 'ddd'; $actual = $actual['AAA']['BBB']['CCC']; $this->assertEquals($expected, $actual); }
function testPlugin() { ob_start(); print_r(PMVC\plug($this->_plug)); $output = ob_get_contents(); ob_end_clean(); $this->assertContains($this->_plug, $output); }
function testImageOutput() { $pImage = \PMVC\plug('image'); $in = $pImage->create(new ImageSize('100', '100')); $out = (new ImageOutput($in))->save(); $imageFile = new ImageFile($out); $this->assertEquals('png', $imageFile->getExt()); }