public
Created

Zend Framework 2 GyaruMoji Module for demo

  • Download Gist
Module.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
<?php
namespace GyaruMoji;
 
class Module
{
public function getConfig()
{
return array(
'service_manager' => array(
'factories' => array(
'gyarumoji' => function ($sm) {
$view = $sm->get('view_manager')->getView();
$view->addRenderingStrategy(
function ($e) {
$gals = (array)
json_decode(file_get_contents(__DIR__ . '/gal.json'));
 
$viewModel = $e->getModel();
foreach ($viewModel->getVariables() as $key => $text) {
 
$replaced_tags = array();
$i = 1;
 
$text = preg_replace_callback("/<[^>]+>/",
function ($matches) use(&$i, &$replaced_tags) {
$replaced_tags["[".$i."]"] = $matches[0];
return "[" . $i++ . "]";
}, $text);
foreach ($gals as $search => $replace) {
if (empty($replace) === false
&& strpos($text, $search) !== false) {
$text = str_replace($search,
$replace[array_rand($replace)],
$text);
}
}
$text = str_replace(
array_keys($replaced_tags),
array_values($replaced_tags),
$text);
$viewModel->setVariable($key, $text);
}
}
);
return $view;
}
)
),
'view_manager' => array(
'strategies' => array(
'gyarumoji'
)
),
);
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.