Ejemplo n.º 1
0
 public function afterCompile(ClassType $class)
 {
     $container = $this->getContainerBuilder();
     $config = $this->getConfig($this->defaults);
     if ($config['panel'] && $container->parameters['debugMode']) {
         $init = $class->methods['initialize'];
         $init->addBody(Helpers::format('Foowie\\Cron\\Diagnostics\\Panel::register($this->getByType(?), $this->getByType(?));', 'Foowie\\Cron\\ICron', 'Nette\\Http\\Request'));
     }
 }
 public function afterCompile(Code\ClassType $class)
 {
     $container = $this->getContainerBuilder();
     if ($eventManager = $container->getByType('Kdyby\\Events\\EventManager')) {
         $methodName = 'createService' . ucfirst($this->name) . '__command';
         $method = $class->methods[$methodName];
         $body = explode(';', substr(trim($method->getBody()), 0, -1));
         $return = array_pop($body);
         $body[] = Code\Helpers::format(PHP_EOL . '$service->setEventManager($this->getService(?))', $eventManager);
         $body[] = $return;
         $method->setBody(implode(';', $body) . ';');
     }
 }
Ejemplo n.º 3
0
    public function afterCompile(Nette\PhpGenerator\ClassType $class)
    {
        $config = $this->getConfig($this->defaults);
        Nette\Utils\Validators::assertField($config, 'enabled', 'boolean');
        Nette\Utils\Validators::assertField($config, 'accessToken', 'string');
        Nette\Utils\Validators::assertField($config, 'roomName', 'string');
        Nette\Utils\Validators::assertField($config, 'filters', 'array');
        if (!$config['enabled']) {
            return;
        }
        unset($config['enabled']);
        $init = $class->methods['initialize'];
        $init->addBody(Nette\PhpGenerator\Helpers::format('
			$logger = new Vysinsky\\HipChat\\Bridges\\Tracy\\Logger(?, ?, ?, ?);
			Tracy\\Debugger::setLogger($logger);
		', $config['accessToken'], $config['roomName'], $config['filters'], $config['linkFactory']));
    }
Ejemplo n.º 4
0
 protected function processRepositoryFactoryEntities(Code\ClassType $class)
 {
     if (empty($this->postCompileRepositoriesQueue)) {
         return;
     }
     $dic = self::evalAndInstantiateContainer($class);
     foreach ($this->postCompileRepositoriesQueue as $manager => $items) {
         $config = $this->managerConfigs[$manager];
         /** @var Kdyby\Doctrine\EntityManager $entityManager */
         $entityManager = $dic->getService($this->configuredManagers[$manager]);
         /** @var Doctrine\ORM\Mapping\ClassMetadata $entityMetadata */
         $metadataFactory = $entityManager->getMetadataFactory();
         $allMetadata = [];
         foreach ($metadataFactory->getAllMetadata() as $entityMetadata) {
             if ($config['defaultRepositoryClassName'] === $entityMetadata->customRepositoryClassName || empty($entityMetadata->customRepositoryClassName)) {
                 continue;
             }
             $allMetadata[ltrim($entityMetadata->customRepositoryClassName, '\\')] = $entityMetadata;
         }
         foreach ($items as $item) {
             if (!isset($allMetadata[$item[0]])) {
                 throw new Nette\Utils\AssertionException(sprintf('Repository class %s have been found in DIC, but no entity has it assigned and it has no entity configured', $item[0]));
             }
             $entityMetadata = $allMetadata[$item[0]];
             $serviceMethod = Nette\DI\Container::getMethodName($item[1]);
             $method = $class->getMethod($serviceMethod);
             $methodBody = $method->getBody();
             $method->setBody(str_replace('"%entityName%"', Code\Helpers::format('?', $entityMetadata->getName()), $methodBody));
         }
     }
 }
Ejemplo n.º 5
0
 /**
  * @return string
  */
 public function __toString()
 {
     $this->setBody('');
     if (strtolower($this->getName()) === '__construct') {
         $this->addParameter('_kdyby_aopContainer')->setTypeHint('\\Nette\\DI\\Container');
         $this->addBody('$this->_kdyby_aopContainer = $_kdyby_aopContainer;');
     }
     $this->addBody('$__arguments = func_get_args(); $__exception = $__result = NULL;');
     if ($this->before) {
         foreach ($this->before as $before) {
             $this->addBody($before);
         }
     }
     if ($this->afterThrowing || $this->after) {
         $this->addBody('try {');
     }
     if (!$this->around) {
         $parentCall = Code\Helpers::format('$__result = call_user_func_array("parent::?", $__arguments);', $this->getName());
     } else {
         $parentCall = Code\Helpers::format('$__around = new \\Kdyby\\Aop\\JoinPoint\\AroundMethod($this, __FUNCTION__, $__arguments);');
         foreach ($this->around as $around) {
             $parentCall .= "\n" . $around;
         }
         $parentCall .= "\n" . Code\Helpers::format('$__result = $__around->proceed();');
     }
     $this->addBody($this->afterThrowing || $this->after ? Nette\Utils\Strings::indent($parentCall) : $parentCall);
     if ($this->afterThrowing || $this->after) {
         $this->addBody('} catch (\\Exception $__exception) {');
     }
     if ($this->afterThrowing) {
         foreach ($this->afterThrowing as $afterThrowing) {
             $this->addBody(Nette\Utils\Strings::indent($afterThrowing));
         }
     }
     if ($this->afterThrowing || $this->after) {
         $this->addBody('}');
     }
     if ($this->afterReturning) {
         if ($this->afterThrowing || $this->after) {
             $this->addBody('if (empty($__exception)) {');
         }
         foreach ($this->afterReturning as $afterReturning) {
             $this->addBody($this->afterThrowing || $this->after ? Nette\Utils\Strings::indent($afterReturning) : $afterReturning);
         }
         if ($this->afterThrowing || $this->after) {
             $this->addBody('}');
         }
     }
     if ($this->after) {
         foreach ($this->after as $after) {
             $this->addBody($after);
         }
     }
     if ($this->afterThrowing || $this->after) {
         $this->addBody('if ($__exception) { throw $__exception; }');
     }
     $this->addBody('return $__result;');
     return parent::__toString();
 }
Ejemplo n.º 6
0
 public function afterCompile(Code\ClassType $class)
 {
     $init = $class->methods['initialize'];
     /** @hack This tries to add the event invokation right after the code, generated by NetteExtension. */
     $foundNetteInitStart = $foundNetteInitEnd = FALSE;
     $lines = explode(";\n", trim($init->body));
     $init->body = NULL;
     while (($line = array_shift($lines)) || $lines) {
         if ($foundNetteInitStart && !$foundNetteInitEnd && stripos($line, 'Nette\\') === FALSE && stripos($line, 'set_include_path') === FALSE && stripos($line, 'date_default_timezone_set') === FALSE) {
             $init->addBody(Code\Helpers::format('$this->getService(?)->createEvent(?)->dispatch($this);', $this->prefix('manager'), array('Nette\\DI\\Container', 'onInitialize')));
             $foundNetteInitEnd = TRUE;
         }
         if (!$foundNetteInitEnd && (stripos($line, 'Nette\\') !== FALSE || stripos($line, 'set_include_path') !== FALSE || stripos($line, 'date_default_timezone_set') !== FALSE)) {
             $foundNetteInitStart = TRUE;
         }
         $init->addBody($line . ';');
     }
     if (!$foundNetteInitEnd) {
         $init->addBody(Code\Helpers::format('$this->getService(?)->createEvent(?)->dispatch($this);', $this->prefix('manager'), array('Nette\\DI\\Container', 'onInitialize')));
     }
 }
    /**
     * @param Translator $translator
     * @param MessageCatalogueInterface[] $availableCatalogues
     * @param string $locale
     * @return string
     */
    protected function compilePhpCache(Translator $translator, array &$availableCatalogues, $locale)
    {
        $fallbackContent = '';
        $current = new Code\PhpLiteral('');
        foreach ($this->fallbackResolver->compute($translator, $locale) as $fallback) {
            $fallbackSuffix = new Code\PhpLiteral(ucfirst(preg_replace('~[^a-z0-9_]~i', '_', $fallback)));
            $fallbackContent .= Code\Helpers::format(<<<EOF
\$catalogue? = new MessageCatalogue(?, ?);
\$catalogue?->addFallbackCatalogue(\$catalogue?);

EOF
, $fallbackSuffix, $fallback, $availableCatalogues[$fallback]->all(), $current, $fallbackSuffix);
            $current = $fallbackSuffix;
        }
        $content = Code\Helpers::format(<<<EOF
use Kdyby\\Translation\\MessageCatalogue;

\$catalogue = new MessageCatalogue(?, ?);

?
return \$catalogue;

EOF
, $locale, $availableCatalogues[$locale]->all(), new Code\PhpLiteral($fallbackContent));
        return '<?php' . "\n\n" . $content;
    }
Ejemplo n.º 8
0
 private function doSerializeValueResolve(ContainerBuilder $builder, $expression)
 {
     if ($expression instanceof Code\PhpLiteral) {
         $expression = self::resolveExpression($expression);
     } elseif (substr($expression, 0, 1) === '%') {
         $expression = $builder->expand($expression);
     } elseif (substr($expression, 0, 1) === '$') {
         $expression = new Code\PhpLiteral($expression);
     } else {
         if (!($m = self::shiftAccessPath($expression))) {
             return $expression;
             // it's probably some kind of expression
         } else {
             if ($m['context'] === 'this') {
                 $targetObject = '$this';
             } elseif ($m['context'] === 'context' && ($p = self::shiftAccessPath($m['path']))) {
                 if (class_exists($p['context']) || interface_exists($p['context'])) {
                     $targetObject = Code\Helpers::format('$this->_kdyby_aopContainer->getByType(?)', $p['context']);
                 } else {
                     $targetObject = Code\Helpers::format('$this->_kdyby_aopContainer->getService(?)', $p['context']);
                 }
                 $m['path'] = $p['path'];
             } else {
                 throw new Kdyby\Aop\NotImplementedException();
             }
             $expression = Code\Helpers::format('PropertyAccess::createPropertyAccessor()->getValue(?, ?)', new Code\PhpLiteral($targetObject), $m['path']);
         }
         $expression = new Code\PhpLiteral($expression);
     }
     return $expression;
 }