本文整理汇总了PHP中Generator类的典型用法代码示例。如果您正苦于以下问题:PHP Generator类的具体用法?PHP Generator怎么用?PHP Generator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Generator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getString
/**
* Covert File to HTML string
*/
function getString()
{
$input = new Generator();
$name = $this->getNameAttributeString();
$value = $this->getValue();
return $input->newInput('file', $name, $value, $this->getAttributes())->getString();
}
示例2: execute
private function execute(\Generator $generator)
{
$stack = [];
//This is basically a simple iterative in-order tree traversal algorithm
$yielded = $generator->current();
//This is a depth-first traversal
while ($yielded instanceof \Generator) {
//... push it to the stack
$stack[] = $generator;
$generator = $yielded;
$yielded = $generator->current();
}
while (!empty($stack)) {
//We've reached the end of the branch, let's step back on the stack
$generator = array_pop($stack);
//step the generator
$yielded = $generator->send($yielded);
//Depth-first traversal
while ($yielded instanceof \Generator) {
//... push it to the stack
$stack[] = $generator;
$generator = $yielded;
$yielded = $generator->current();
}
}
return $yielded;
}
示例3: __construct
/**
* @param Generator $generator
* @param $addPid
*/
public function __construct(Generator $generator, $addPid)
{
$this->requestId = $generator->getRequestId();
if ($addPid) {
$this->pid = getmypid();
}
}
示例4: generateSql
public function generateSql()
{
$namespace = \Zule\Tools\Config::zc()->framework->application_namespace;
$system = 'Zule';
$tables = $this->settings->getTables();
foreach ($tables as $table) {
$s = new \Smarty();
$tableName = $table->getName();
// generate model
$s->assign('model_name', $_POST["class_{$tableName}"]);
$s->assign('namespace', $namespace);
$s->assign('system', $system);
$s->assign('class_name', $_POST["class_{$tableName}"]);
$s->assign('extend_path', '\\Zule\\Models\\Model');
$s->assign('impl_date', date('Y-m-d H:i:s'));
$s->assign('use_unsafe_setters', 0);
$s->assign('table_name', $tableName);
$columns = $table->getColumns();
$s->assign('columns', $columns);
$s->assign('primary_key', $table->getPrimaryKey());
$gen = new Generator("../models/" . $_POST["class_{$tableName}"] . '.php');
$gen->generate($s, 'model_sql');
$gen = new Generator("../models/Data/" . $_POST["class_{$tableName}"] . '.php');
$gen->generate($s, 'gateway_sql');
}
}
示例5: stackedCoroutine
/**
* Resolves yield calls tree
* and gives a return value if outcome of yield is CoroutineReturnValue instance
*
* @param \Generator $coroutine nested coroutine tree
* @return \Generator
*/
private static function stackedCoroutine(\Generator $coroutine)
{
$stack = new \SplStack();
while (true) {
$value = $coroutine->current();
// nested generator/coroutine
if ($value instanceof \Generator) {
$stack->push($coroutine);
$coroutine = $value;
continue;
}
// coroutine end or value is a value object instance
if (!$coroutine->valid() || $value instanceof CoroutineReturnValue) {
// if till this point, there are no coroutines in a stack thatn stop here
if ($stack->isEmpty()) {
return;
}
$coroutine = $stack->pop();
$value = $value instanceof CoroutineReturnValue ? $value->getValue() : null;
$coroutine->send($value);
continue;
}
$coroutine->send((yield $coroutine->key() => $value));
}
}
示例6: buildUser
public function buildUser(Request $request, Generator $generator)
{
if (empty($request->userName)) {
throw new \InvalidArgumentException();
}
$password = $request->password ?: $generator->generatePassword();
$hashedPassword = md5(self::SALT . $password);
return new User($request->userName, $password, $hashedPassword);
}
示例7: testGeneratingUuidShouldReturnDifferentResultEveryTime
/**
* Generating UUID should return different results every time
*
* @covers Mixpanel\Uuid\Generator::generate
* @covers Mixpanel\Uuid\Generator::ticksEntropy
* @covers Mixpanel\Uuid\Generator::uaEntropy
* @covers Mixpanel\Uuid\Generator::randomEntropy
* @covers Mixpanel\Uuid\Generator::ipEntropy
*/
public function testGeneratingUuidShouldReturnDifferentResultEveryTime()
{
$uuids = array();
for ($i = 0; $i < 100; $i++) {
$uuid = $this->generator->generate('user-agent', '127.0.0.1');
$this->assertNotContains($uuid, $uuids);
$uuids[] = $uuid;
}
}
示例8: create
public static function create($locale = self::DEFAULT_LOCALE)
{
$generator = new Generator();
foreach (static::$defaultProviders as $provider) {
$providerClassName = self::getProviderClassname($provider, $locale);
$generator->addProvider(new $providerClassName($generator));
}
return $generator;
}
示例9: run
/**
* [run 协程调度]
* @param \Generator $gen
* @throws \Exception
*/
public function run(\Generator $gen)
{
while (true) {
try {
/* 异常处理 */
if ($this->exception) {
$gen->throw($this->exception);
$this->exception = null;
continue;
}
$value = $gen->current();
// Logger::write(__METHOD__ . " value === " . var_export($value, true), Logger::INFO);
/* 中断内嵌 继续入栈 */
if ($value instanceof \Generator) {
$this->corStack->push($gen);
// Logger::write(__METHOD__ . " corStack push ", Logger::INFO);
$gen = $value;
continue;
}
/* if value is null and stack is not empty pop and send continue */
if (is_null($value) && !$this->corStack->isEmpty()) {
// Logger::write(__METHOD__ . " values is null stack pop and send ", Logger::INFO);
$gen = $this->corStack->pop();
$gen->send($this->callbackData);
continue;
}
if ($value instanceof ReturnValue) {
$gen = $this->corStack->pop();
$gen->send($value->getValue());
// end yeild
// Logger::write(__METHOD__ . " yield end words == " . var_export($value, true), Logger::INFO);
continue;
}
/* 中断内容为异步IO 发包 返回 */
if ($value instanceof \Swoole\Client\IBase) {
//async send push gen to stack
$this->corStack->push($gen);
$value->send(array($this, 'callback'));
return;
}
/* 出栈,回射数据 */
if ($this->corStack->isEmpty()) {
return;
}
// Logger::write(__METHOD__ . " corStack pop ", Logger::INFO);
$gen = $this->corStack->pop();
$gen->send($value);
} catch (EasyWorkException $e) {
if ($this->corStack->isEmpty()) {
throw $e;
}
$gen = $this->corStack->pop();
$this->exception = $e;
}
}
}
示例10: checkResult
private function checkResult(\Generator $generator, Channel $channel)
{
if (!$generator->valid()) {
foreach ($this->actions as list(, $gen, , $undo)) {
if ($gen && $gen !== $generator) {
$undo($gen->current());
}
}
return [$generator->getReturn(), $channel];
}
return false;
}
示例11: reduce
/**
* When you need to return Result from your function, and it also depends on another
* functions returning Results, you can make it a generator function and yield
* values from dependant functions, this pattern makes code less bloated with
* statements like this:
* $res = something();
* if ($res instanceof Ok) {
* $something = $res->unwrap();
* } else {
* return $res;
* }
*
* Instead you can write:
* $something = (yield something());
*
* @see /example.php
*
* @param \Generator $resultsGenerator Generator that produces Result instances
* @return Result
*/
public static function reduce(\Generator $resultsGenerator)
{
/** @var Result $result */
$result = $resultsGenerator->current();
while ($resultsGenerator->valid()) {
if ($result instanceof Err) {
return $result;
}
$tmpResult = $resultsGenerator->send($result->unwrap());
if ($resultsGenerator->valid()) {
$result = $tmpResult;
}
}
return $result;
}
示例12: createUserWithGeneratedPassword
/**
* Create user with generated password.
*
* @param $username
*
* @return User
*/
public function createUserWithGeneratedPassword($username, $hashCost = 10, $salt = '')
{
$randomPassword = $this->generator->generateRandomPassword($this->minPasswordLength, $this->maxPasswordLength);
$hashedPassword = $this->hashPassword($randomPassword, PASSWORD_DEFAULT, $hashCost, $salt);
$user = new User($username, $randomPassword, $hashedPassword);
return $user;
}
示例13: testSetData
public function testSetData()
{
/**
* ['merchantID'] string This is the merchantID assigned by PayCo.
* ['storeID'] string This is the store ID of a merchant.
* ['logEnabled'] string Should logging be enabled
* ['logLevel'] int Log level
* ['logLocationMain'] string Main log Location
* ['logLocationRequest'] string Log location for API requests
* ['logLocationMNS'] string Log for MNS asynchronous callbacks
* ['logLocationCallbacks'] string Log location for synchronous callbacks
* ['defaultRiskClass'] string Default risk class
*/
$config = array('merchantID' => $this->faker->randomNumber(8), 'storeID' => $this->faker->word, 'logEnabled' => $this->faker->boolean, 'logLevel' => Config::LOG_LEVEL_ERROR, 'logLocationMain' => $this->faker->word, 'logLocationRequest' => $this->faker->word, 'logLocationMNS' => $this->faker->word, 'logLocationCallbacks' => $this->faker->word, 'defaultRiskClass' => 2, 'defaultLocale' => strtoupper($this->faker->languageCode));
$configObject = new Config();
$configObject->setData($config);
$this->assertEquals($config['merchantID'], $configObject->getMerchantID(), "Merchant ID was set incorrectly");
$this->assertEquals($config['storeID'], $configObject->getStoreID(), "Store ID was set incorrectly");
$this->assertEquals($config['logEnabled'], $configObject->getLogEnabled(), "Log Enabled was set incorrectly");
$this->assertEquals($config['logLevel'], $configObject->getLogLevel(), "Log Level was set incorrectly");
$this->assertEquals($config['logLocationMain'], $configObject->getLogLocationMain(), "Main Log location was set incorrectly");
$this->assertEquals($config['logLocationRequest'], $configObject->getLogLocationRequest(), "Request Log location was set incorrectly");
$this->assertEquals($config['logLocationMNS'], $configObject->getLogLocationMNS(), "MNS Log location was set incorrectly");
$this->assertEquals($config['logLocationCallbacks'], $configObject->getLogLocationCallbacks(), "Callback Log location was set incorrectly");
$this->assertEquals($config['defaultRiskClass'], $configObject->getDefaultRiskClass(), "Default Risk was set incorrectly");
$this->assertEquals($config['defaultLocale'], $configObject->getDefaultLocale(), "Default was set incorrectly");
}
示例14: loadClass
/**
* @param string $className
*/
public function loadClass(string $className)
{
if ($filename = $this->generator->checkClass($className)) {
includeFile($filename);
return true;
}
}
示例15: run
/**
* Run ZMQ interface for generator
*
* Req-rep pattern; msgs are commands:
*
* GEN = Generate ID
* STATUS = Get status string
*/
public function run()
{
$context = new \ZMQContext();
$receiver = new \ZMQSocket($context, \ZMQ::SOCKET_REP);
$bindTo = 'tcp://*:' . $this->port;
echo "Binding to {$bindTo}\n";
$receiver->bind($bindTo);
while (TRUE) {
$msg = $receiver->recv();
switch ($msg) {
case 'GEN':
try {
$response = $this->generator->generate();
} catch (\Exception $e) {
$response = "ERROR";
}
break;
case 'STATUS':
$response = json_encode($this->generator->status());
break;
default:
$response = 'UNKNOWN COMMAND';
break;
}
$receiver->send($response);
}
}