Skip to content

Instantly share code, notes, and snippets.

@WalrusSoup
Last active September 18, 2020 19:57
Show Gist options
  • Save WalrusSoup/55c1c418b2ad6841b0db8ceb21a18a2a to your computer and use it in GitHub Desktop.
Save WalrusSoup/55c1c418b2ad6841b0db8ceb21a18a2a to your computer and use it in GitHub Desktop.
Yii 1.1 Tinkerwell Driver
<?php
define('YII_DEBUG', true);
// Composer
require_once __DIR__ . '/../vendor/autoload.php';
// project uses dotenv
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
$dotenv->load();
// Fixes issue with needing 127.0.0.1 instead of localhost
putenv('DATABASE_HOSTNAME=127.0.0.1');
// Bootstrap Yii
require_once __DIR__ . '/../vendor/yiisoft/yii/framework/yii.php';
$config = require(__DIR__ . '/../protected/config/config.php');
Yii::setpathofalias('vendor', __DIR__ . '/../vendor');
unset($config['defaultController']);
// This class is a custom CApplication for use with Tinker
class CTinkerApplication extends CApplication {
protected $runner;
public function processRequest() {
$this->loadCommands();
}
// Load all commands so we can fire them from within tinker
public function loadCommands() {
$path = __DIR__.'/../protected/commands';
if(($dir=@opendir($path))===false)
return array();
$commands=array();
while(($name=readdir($dir))!==false)
{
$file=$path.DIRECTORY_SEPARATOR.$name;
if(!strcasecmp(substr($name,-11),'Command.php') && is_file($file))
require_once($file);
}
closedir($dir);
}
}
// Create the new app and run it so yii is bootstrapped and we have full access within tinker
Yii::createApplication('CTinkerApplication', $config)->run();
<?php
// Put this into the .tinkerwell folder alongside of tinker_bootstrap.php to allow yii 1.1 to work within tinker
use Tinkerwell\ContextMenu\Label;
use Tinkerwell\ContextMenu\Submenu;
use Tinkerwell\ContextMenu\SetCode;
use Tinkerwell\ContextMenu\RunCode;
use Tinkerwell\ContextMenu\OpenURL;
class YiiTinkerwellDriver extends LaravelTinkerwellDriver {
public function canBootstrap($projectPath)
{
return true;
}
public function bootstrap($projectPath)
{
require_once $projectPath . '/.tinkerwell/tinker_bootstrap.php';
}
public function contextMenu()
{
return [
Label::create('Yii 1.1 Detected'),
Submenu::create('Commands', [
RunCode::create('MyCommandName', <<< EOT
(new MYCOMMANDNAMEHERE('', new CConsoleCommandRunner()))->run([]);
EOT
)
])
];
}
public function getAvailableVariables()
{
return [
'now' => new DateTime('now', new DateTimeZone('UTC')),
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment