Skip to content

Instantly share code, notes, and snippets.

@sasezaki
Created July 11, 2012 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sasezaki/3091055 to your computer and use it in GitHub Desktop.
Save sasezaki/3091055 to your computer and use it in GitHub Desktop.
Zend Framework 2 GyaruMoji Module for demo
<?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'
)
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment