Created
July 11, 2012 15:13
-
-
Save sasezaki/3091055 to your computer and use it in GitHub Desktop.
Zend Framework 2 GyaruMoji Module for demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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