Skip to content

Instantly share code, notes, and snippets.

@arleighdickerson
Last active June 11, 2017 23:00
Show Gist options
  • Save arleighdickerson/6fbd3ed286a569897e1f to your computer and use it in GitHub Desktop.
Save arleighdickerson/6fbd3ed286a569897e1f to your computer and use it in GitHub Desktop.
Helper functions living in the global namespace. Drop into bootstrap
<?php
use ProxyManager\Factory\AccessInterceptorValueHolderFactory;
use React\ChildProcess\Process;
use React\EventLoop\LoopInterface;
use React\Filesystem\Filesystem;
use React\Filesystem\FilesystemInterface;
use React\Promise\PromiseInterface;
Yii::$container
->setSingleton(LoopInterface::class, function () {
return React\EventLoop\Factory::create();
})
->setSingleton(FilesystemInterface::class, function ($container, $params, $config) {
return Filesystem::create($container->get(LoopInterface::class), $config);
})
->setSingleton(AccessInterceptorValueHolderFactory::class, function ($container, $params, $config) {
return new AccessInterceptorValueHolderFactory(ArrayHelper::getValue($params, '0', null));
})
->set(Process::class, function ($container, $params, $config) {
$cwd = alias('@cm1');
$env = null;
$options = [];
extract($config, EXTR_OVERWRITE);
/** @var string $cmd */
$process = new Process($cmd, $cwd, $env, $options);
/** @var AccessInterceptorValueHolderFactory $factory */
$factory = $container->get(AccessInterceptorValueHolderFactory::class);
$getPipes = (new \ReflectionClass($process))->getProperty('pipes');
return $factory->createProxy($process, [], [
'start' => function () use ($getPipes, $process) {
$getPipes->setAccessible(true);
$pipes =& $getPipes->getValue($process);
// hax to fix SEGFAULT on php 5.5/6
stream_set_read_buffer($pipes[1], 1);
stream_set_read_buffer($pipes[2], 1);
$getPipes->setAccessible(false);
}
]
);
});
/**
* get a reference to the application context
*
* @return yii\di\Container
*/
function ctx() {
return Yii::$container;
}
/**
* get a reference to the running application
*
* @return yii\web\Application
*/
function app() {
return Yii::$app;
}
/**
* get a reference to the executing controller
*
* @return yii\web\Controller
*/
function controller() {
return app()->controller;
}
/**
* get a reference to the applications's view component
*
*
* @return yii\web\View
*/
function view() {
return app()->view;
}
/**
* get a reference to the current application user
*
* @return yii\web\User;
*/
function user() {
return app()->user;
}
/**
* get a reference to the auth manager
*
* @return \yii\rbac\ManagerInterface
*/
function auth() {
return app()->get('authManager', true);
}
/**
* get a reference to the current request
*
* @return yii\web\Request|yii\console\Request
*/
function request() {
return app()->request;
}
/**
* get a reference to the pending response
*
* @return yii\web\Response
*/
function response() {
return app()->response;
}
/**
* get a reference to the current session
*
* @return yii\web\session;
*/
function session() {
return app()->session;
}
/**
* @return LoopInterface
*/
function loop() {
return ctx()->get(React\EventLoop\LoopInterface::class);
}
/**
* @return FilesystemInterface
*/
function fs() {
return ctx()->get(FilesystemInterface::class);
}
/**
* @param string $name the parameter name
* @param mixed $defaultValue the default parameter value if the parameter does not exist.
* @return array|mixed
*/
function get($name = null, $defaultValue = null) {
return request()->get($name, $defaultValue);
}
/**
* @param string $name the parameter name
* @param mixed $defaultValue the default parameter value if the parameter does not exist.
* @return array|mixed
*/
function post($name = null, $defaultValue = null) {
return request()->post($name, $defaultValue);
}
/**
* wrap an array in the underscore helper class
*
* @param $array array
* @return \common\helpers\Underscore;
*/
function __(array $array) {
return new common\helpers\Underscore($array);
}
/**
* @param $arg
*/
function print_jer($arg) {
echo "<pre>";
print_r($arg);
echo "</pre>";
}
/**
* @param $alias
* @param bool $throwException
* @return bool|string
*/
function alias($alias, $throwException = true) {
return Yii::getAlias($alias, $throwException);
}
/**
* @param $route
* @param null $output
* @param null $exitCode
* @return string
*/
function yii_exec($route, &$output = null, &$exitCode = null) {
$yii = dirname(Yii::getAlias('@app')) . '/yii';
return exec("$yii $route" . (YII_DEBUG ? "" : " NO_DEBUG"), $output, $exitCode);
}
/**
* @param $cmd
* @param null $cwd
* @param null $env
* @param null $options
* @return Process
*/
function child_proc($cmd, $cwd = null, $env = null, $options = null) {
$config = compact('cmd') + array_filter(compact('cwd', 'env', 'options'), function ($v) {
return !is_null($v);
});
return ctx()->get(Process::class, [], $config);
}
/**
* @param Process|string $process
* @param LoopInterface $loop
* @return PromiseInterface
*/
function child_proc_promise($process, LoopInterface $loop = null) {
if (is_string($process)) {
$process = child_proc($process);
}
if ($loop === null) {
$loop = loop();
}
return WyriHaximus\React\childProcessPromise($loop, $process);
}
/**
* @param int|string|DateTime $dateTime
* @return Moment\Moment
*/
function moment($dateTime = 'now') {
if ($dateTime instanceof Moment\Moment) {
return $dateTime;
}
if ($dateTime instanceof DateTime) {
$dateTime = (int)$dateTime->getTimestamp();
}
if (is_int($dateTime)) {
$dateTime = '@' . $dateTime;
}
return new Moment\Moment($dateTime, Yii::$app->getTimeZone(), true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment