示例#1
0
 /**
  * @param $helper
  * @return callable
  * @throws \movi\InvalidArgumentException
  */
 public function loader($helper)
 {
     if (isset($this->helpers[$helper])) {
         return Callback::create($this->helpers[$helper], 'process');
     } else {
         return false;
     }
 }
示例#2
0
 /**
  * @param $callback
  * @return $this
  * @throws \movi\InvalidArgumentException
  */
 public function setDisabled($callback)
 {
     if (!is_callable($callback)) {
         throw new InvalidArgumentException('Callback is not callable');
     }
     $this->disabled = Callback::create($callback);
     return $this;
 }
示例#3
0
 public function onSuccess(Form $form)
 {
     if (!Callback::create($this->checkConnection)->invoke() || !$this->schemaManager->tablesExist('users')) {
         return;
     }
     $presenter = $form->presenter;
     $logEntity = new LogEntity($this->user instanceof UserEntity ? $this->user : NULL, 'Venne\\Forms\\Form', NULL, LogEntity::ACTION_OTHER);
     $logEntity->setType($presenter->link('this'));
     $logEntity->setMessage('Configuration has been updated');
     $this->logRepository->save($logEntity);
 }
示例#4
0
 public function signalReceived($signal)
 {
     $query = $this->form->presenter->request->parameters['q'];
     if (!$this->suggestCallback) {
         throw new InvalidArgumentException("Property 'suggestCallback' is not defined.");
     }
     $data = Callback::create($this->suggestCallback)->invoke($query);
     if (!is_array($data)) {
         throw new InvalidArgumentException("Data from suggestCallback must be array.");
     }
     $this->form->getPresenter()->sendResponse(new JsonResponse(array('results' => $data, 'more' => FALSE)));
 }
示例#5
0
 /**
  * Logs message or exception to file and sends email notification.
  * @param  string|array
  * @param  int     one of constant INFO, WARNING, ERROR (sends email), CRITICAL (sends email)
  * @return bool    was successful?
  */
 public function log($message, $priority = self::INFO)
 {
     if (!is_dir($this->directory)) {
         throw new Nette\DirectoryNotFoundException("Directory '{$this->directory}' is not found or is not directory.");
     }
     if (is_array($message)) {
         $message = implode(' ', $message);
     }
     $res = error_log(trim($message) . PHP_EOL, 3, $this->directory . '/' . strtolower($priority) . '.log');
     if (($priority === self::ERROR || $priority === self::CRITICAL) && $this->email && $this->mailer && @filemtime($this->directory . '/email-sent') + self::$emailSnooze < time() && @file_put_contents($this->directory . '/email-sent', 'sent')) {
         Nette\Callback::create($this->mailer)->invoke($message, $this->email);
     }
     return $res;
 }
 /**
  * __call() implementation.
  * @param  object
  * @param  string
  * @param  array
  * @return mixed
  * @throws MemberAccessException
  */
 public static function call($_this, $name, $args)
 {
     $class = get_class($_this);
     $isProp = self::hasProperty($class, $name);
     if ($name === '') {
         throw new MemberAccessException("Call to class '{$class}' method without name.");
     } elseif ($isProp === 'event') {
         // calling event handlers
         if (is_array($_this->{$name}) || $_this->{$name} instanceof \Traversable) {
             foreach ($_this->{$name} as $handler) {
                 Nette\Callback::create($handler)->invokeArgs($args);
             }
         } elseif ($_this->{$name} !== NULL) {
             throw new UnexpectedValueException("Property {$class}::\${$name} must be array or NULL, " . gettype($_this->{$name}) . " given.");
         }
     } elseif ($cb = Reflection\ClassType::from($_this)->getExtensionMethod($name)) {
         // extension methods
         array_unshift($args, $_this);
         return $cb->invokeArgs($args);
     } else {
         throw new MemberAccessException("Call to undefined method {$class}::{$name}().");
     }
 }
示例#7
0
 public function handleSave(Form $form)
 {
     if ($form->hasSaveButton() && $form->isSubmitted() === $form->getSaveButton()) {
         try {
             $this->mapper->entityManager->getRepository(get_class($form->data))->save($form->data);
         } catch (\Exception $e) {
             $ok = true;
             if (is_array($this->onCatchError) || $this->onCatchError instanceof \Traversable) {
                 foreach ($this->onCatchError as $handler) {
                     if (\Nette\Callback::create($handler)->invokeArgs(array($form, $e))) {
                         $ok = false;
                         break;
                     }
                 }
             } elseif ($this->onCatchError !== NULL) {
                 $class = get_class($this);
                 throw new \Nette\UnexpectedValueException("Property {$class}::onCatchError must be array or NULL, " . gettype($this->onCatchError) . " given.");
             }
             if ($ok) {
                 throw $e;
             }
         }
     }
 }
 /**
  * {use class MacroSet}
  */
 public function macroUse(MacroNode $node, PhpWriter $writer)
 {
     Nette\Callback::create($node->tokenizer->fetchWord(), 'install')->invoke($this->getCompiler())->initialize();
 }
示例#9
0
 /**
  * Generates code.
  * @return string
  */
 private function compile(MacroNode $node, $def)
 {
     $node->tokenizer->reset();
     $writer = Latte\PhpWriter::using($node, $this->compiler);
     if (is_string($def)) {
         return $writer->write($def);
     } else {
         return Nette\Callback::create($def)->invoke($node, $writer);
     }
 }
示例#10
0
 protected function createComponentNavbarForm()
 {
     $_this = $this;
     $form = $this->navbarForms[$this->formName];
     $entity = $form->getEntityFactory() ? Callback::create($form->getEntityFactory())->invoke() : $this->repository->createNew();
     $form = $form->getFactory()->invoke($entity);
     $form->onSuccess[] = $this->navbarFormSuccess;
     $form->onError[] = $this->navbarFormError;
     if ($this->mode == self::MODE_PLACE) {
         $form->addSubmit('_cancel', 'Cancel')->setValidationScope(FALSE)->onClick[] = function () use($_this) {
             $_this->redirect('this', array('formName' => NULL, 'mode' => NULL));
         };
     }
     return $form;
 }
示例#11
0
 /**
  * __get() implementation.
  *
  * @param  object
  * @param  string  property name
  *
  * @return mixed   property value
  * @throws MemberAccessException if the property is not defined.
  */
 public static function &get($_this, $name)
 {
     $class = get_class($_this);
     $uname = ucfirst($name);
     if (!isset(self::$methods[$class])) {
         self::$methods[$class] = array_flip(get_class_methods($class));
         // public (static and non-static) methods
     }
     if ($name === '') {
         throw new MemberAccessException("Cannot read a class '{$class}' property without name.");
     } elseif (isset(self::$methods[$class][$m = 'get' . $uname]) || isset(self::$methods[$class][$m = 'is' . $uname])) {
         // property getter
         $val = $_this->{$m}();
         return $val;
     } elseif (isset(self::$methods[$class][$name])) {
         // public method as closure getter
         $val = Callback::create($_this, $name);
         return $val;
     } else {
         // strict class
         $type = isset(self::$methods[$class]['set' . $uname]) ? 'a write-only' : 'an undeclared';
         throw new MemberAccessException("Cannot read {$type} property {$class}::\${$name}.");
     }
 }
示例#12
0
文件: Grid.php 项目: peterzadori/movi
 /**
  * @param $name
  * @param $label
  * @param $callback
  * @throws \movi\InvalidArgumentException
  */
 public function addAction($name, $label, $callback)
 {
     if (isset($this->actions[$name])) {
         throw new InvalidArgumentException("Action '{$name}' is already added.'");
     }
     if (!is_callable($callback)) {
         throw new InvalidArgumentException('Callback is not callable');
     }
     $this->actions[$name] = Callback::create($callback);
     // Add submit
     $this['form']['action']->addSubmit($name, $label);
 }
示例#13
0
 /**
  * Helper function which is called before the first query
  *
  * @return void
  */
 protected function setup()
 {
     // Basic roles
     $this->addRole('guest');
     $this->addRole('user', 'guest');
     $this->addRole('psk', 'guest');
     // Trigger registered initializators
     foreach ($this->onSetup as $cb) {
         Nette\Callback::create($cb)->invokeArgs(array($this));
     }
 }
示例#14
0
 /**
  * @return LoginControl
  */
 public function invoke()
 {
     return Callback::create($this->loginControlFactory)->invoke();
 }
示例#15
0
 public function handleSetParent($from = NULL, $to = NULL, $dropmode = NULL)
 {
     Callback::create($this->dropCallback)->invoke($from, $to, $dropmode);
 }
示例#16
0
 /**
  * Caches results of function/method calls.
  * @param  mixed
  * @param  array  dependencies
  * @return Closure
  */
 public function wrap($function, array $dependencies = NULL)
 {
     $cache = $this;
     return function () use($cache, $function, $dependencies) {
         $key = array($function, func_get_args());
         $data = $cache->load($key);
         if ($data === NULL) {
             $data = $cache->save($key, Nette\Callback::create($function)->invokeArgs($key[1]), $dependencies);
         }
         return $data;
     };
 }
示例#17
0
 public function invoke()
 {
     return Callback::create($this->checkConnection)->invoke();
 }
示例#18
0
 /**
  * Formats PHP code for class instantiating, function calling or property setting in PHP.
  * @return string
  * @internal
  */
 public function formatStatement(Statement $statement, $self = NULL)
 {
     $entity = $this->normalizeEntity($statement->entity);
     $arguments = $statement->arguments;
     if (is_string($entity) && Strings::contains($entity, '?')) {
         // PHP literal
         return $this->formatPhp($entity, $arguments, $self);
     } elseif ($service = $this->getServiceName($entity)) {
         // factory calling or service retrieving
         if ($this->definitions[$service]->shared) {
             if ($arguments) {
                 throw new ServiceCreationException("Unable to call service '{$entity}'.");
             }
             return $this->formatPhp('$this->getService(?)', array($service));
         }
         $params = array();
         foreach ($this->definitions[$service]->parameters as $k => $v) {
             $params[] = preg_replace('#\\w+$#', '\\$$0', is_int($k) ? $v : $k) . (is_int($k) ? '' : ' = ' . PhpHelpers::dump($v));
         }
         $rm = new Nette\Reflection\GlobalFunction(create_function(implode(', ', $params), ''));
         $arguments = Helpers::autowireArguments($rm, $arguments, $this);
         return $this->formatPhp('$this->?(?*)', array(Container::getMethodName($service, FALSE), $arguments), $self);
     } elseif ($entity === 'not') {
         // operator
         return $this->formatPhp('!?', array($arguments[0]));
     } elseif (is_string($entity)) {
         // class name
         if ($constructor = Nette\Reflection\ClassType::from($entity)->getConstructor()) {
             $this->addDependency($constructor->getFileName());
             $arguments = Helpers::autowireArguments($constructor, $arguments, $this);
         } elseif ($arguments) {
             throw new ServiceCreationException("Unable to pass arguments, class {$entity} has no constructor.");
         }
         return $this->formatPhp("new {$entity}" . ($arguments ? '(?*)' : ''), array($arguments), $self);
     } elseif (!Validators::isList($entity) || count($entity) !== 2) {
         throw new Nette\InvalidStateException("Expected class, method or property, " . PhpHelpers::dump($entity) . " given.");
     } elseif ($entity[0] === '') {
         // globalFunc
         return $this->formatPhp("{$entity['1']}(?*)", array($arguments), $self);
     } elseif (Strings::contains($entity[1], '$')) {
         // property setter
         Validators::assert($arguments, 'list:1', "setup arguments for '" . Nette\Callback::create($entity) . "'");
         if ($this->getServiceName($entity[0], $self)) {
             return $this->formatPhp('?->? = ?', array($entity[0], substr($entity[1], 1), $arguments[0]), $self);
         } else {
             return $this->formatPhp($entity[0] . '::$? = ?', array(substr($entity[1], 1), $arguments[0]), $self);
         }
     } elseif ($service = $this->getServiceName($entity[0], $self)) {
         // service method
         if ($this->definitions[$service]->class) {
             $arguments = $this->autowireArguments($this->definitions[$service]->class, $entity[1], $arguments);
         }
         return $this->formatPhp('?->?(?*)', array($entity[0], $entity[1], $arguments), $self);
     } else {
         // static method
         $arguments = $this->autowireArguments($entity[0], $entity[1], $arguments);
         return $this->formatPhp("{$entity['0']}::{$entity['1']}(?*)", array($arguments), $self);
     }
 }
示例#19
0
 /**
  * @return RegistrationControl
  */
 public function invoke($userType, $mode, $loginProviderMode, $roles, $emailSender, $emailFrom, $emailSubject, $emailText)
 {
     return Callback::create($this->factory)->invoke($userType, $mode, $loginProviderMode, $roles, $emailSender, $emailFrom, $emailSubject, $emailText);
 }
示例#20
0
 /**
  * @param Callback|NULL $formFactory
  */
 public function injectFactory($factory = NULL)
 {
     $this->factory = Callback::create($factory);
 }
示例#21
0
 /**
  * @return \CmsModule\Content\Entities\ExtendedPageEntity
  */
 public function getExtendedPage()
 {
     if (!$this->extendedPage) {
         $this->extendedPage = Callback::create($this->extendedPageCallback)->invoke();
     }
     return $this->extendedPage;
 }
示例#22
0
 /**
  * @param $callback
  * @return $this
  * @throws \movi\InvalidArgumentException
  */
 public function setRenderer($callback)
 {
     if (!is_callable($callback)) {
         throw new InvalidArgumentException('Renderer is not callable');
     }
     $this->renderer = Callback::create($callback);
     return $this;
 }
示例#23
0
 /**
  * @return ExtendedUserEntity
  */
 public function getExtendedUser()
 {
     if (!$this->extendedUser) {
         $this->extendedUser = Callback::create($this->extendedUserCallback)->invoke();
     }
     return $this->extendedUser;
 }