Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace WOW\Service;
- use Nette\Application\UI\Presenter;
- use Nette\DI\Container;
- use Nette\Utils\Finder;
- use Nette\Caching\Cache;
- use Nette\Reflection\ClassType;
- class ComponentRegister extends Nette\Object
- {
- /** @var Cache */
- private $cache;
- /** @var callable */
- private $expander;
- /** @var array */
- private $types = array(
- 'jpg' => '%wwwDir%/images',
- 'png' => '%wwwDir%/images',
- 'gif' => '%wwwDir%/images',
- 'css' => '%wwwDir%/css',
- 'js' => '%wwwDir%/js',
- );
- /**
- * @param Container $container
- */
- public function __construct(Container $container, Nette\Caching\IStorage $cacheStorage)
- {
- $this->expander = callback($continer, 'expand');
- $this->cache = new Cache($cacheStorage, 'WOW.ComponentInstaller');
- }
- /**
- * @param Presenter $presenter
- */
- public function installPresenterComponents(Presenter $presenter)
- {
- if ($this->isInstalled($presenter)) {
- return;
- }
- $files = array();
- foreach ($this->getPresenterComponents($presenter) as $component) {
- $files = array_merge($files, $this->prepareAssets($component));
- }
- $this->setInstalled($presenter, $files);
- }
- /**
- * @param Presenter $presenters
- * @return array of files
- */
- public function getPresenterAssets(Presenter $presenter)
- {
- if (!$this->isInstalled($presenter)) {
- throw new Nette\InvalidArgumentException("Presenter " . get_class($presenter) . " is not installed");
- }
- return $this->cache->load($presenter->getAction(TRUE));
- }
- /**
- * @param Presenter $presenters
- * @return boolean
- */
- public function isInstalled(Presenter $presenter)
- {
- return $this->cache->load($presenter->getAction(TRUE)) !== NULL;
- }
- /**
- * @param Presenter $presenters
- * @param array $files
- */
- private function setInstalled(Presenter $presenter, array $files)
- {
- $this->cache->save(
- $presenter->getAction(TRUE), // vrací "nette" cestu k presenteru
- $files, // list of files to include, Control will resolve...
- array(
- Cache::FILES => $files + array('presenter' => $presenter->getReflection()->getFileName())
- )
- );
- }
- /**
- * @param object $component
- * @return array of sources
- */
- private function prepareAssets($component)
- {
- $files = array();
- foreach ($this->types as $type => $publicPath) {
- foreach (Finder::findFiles('*.' . $type)->from($component->path) as $file) {
- $files[] = $source = $file->getRealPath();
- @copy($source, $this->expander->invoke($publicPath . '/' . basename($source)));
- }
- }
- return $files;
- }
- /**
- * @param Presenter $presenter
- * @return array components
- */
- private function getPresenterComponents(Presenter $presenter)
- {
- $components = array();
- foreach (ClassType::from($presenter)->getMethods() as $method) {
- if (substr($method->name, 0, 15) !== 'createComponent') {
- continue;
- }
- if ($method->hasAnnotation('component')) {
- if (!$method->getAnnotation('return')) {
- throw new Nette\UnexpectedValueException("Set annotation @return for " . $method->getName() . " in " . get_class($presenter));
- }
- $name = lcFirst(substr($method->getName(), 15));
- $components[] = (object)array(
- 'name' => $name,
- 'factory' => $method,
- 'class' => $reflection->getAnnotation('return'),
- 'path' => ClassType::from($reflection->getAnnotation('return'))->getFileName();
- );
- }
- }
- return $components;
- }
- }
- abstract class BasePresenter extends Nette\Application\UI\Presenter
- {
- protected function startup()
- {
- parent::startup();
- $this->context->componentRegister->installPresenterComponents($this);
- }
- /**
- * @return AssetsControl
- */
- protected function createComponentAssets()
- {
- return new AssetsControl($this->context->componentRegister);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment