/** * getter for specified model * * @param string $name name of model * @return BaseModel * @throws \InvalidArgumentException */ public function getModel($name) { if ($this->container->hasService($name)) { return $this->container->getService($name); } throw new \InvalidArgumentException("Model '{$name}' not found."); }
/** * @param \Venne\Module\IModule $module */ public function uninstall(IModule $module) { if (!$this->context->hasService('doctrine') || !$this->context->doctrine->createCheckConnection()) { throw new \Exception('Database connection not found!'); } $classes = $this->getClasses($module); $metadata = array(); foreach ($classes as $class) { $metadata[] = $this->entityManager->getClassMetadata($class); } $tool = new \Doctrine\ORM\Tools\SchemaTool($this->entityManager); $this->entityManager->getConnection()->beginTransaction(); try { foreach ($classes as $class) { $repository = $this->entityManager->getRepository($class); foreach ($repository->findAll() as $entity) { $repository->delete($entity); } } $tool->dropSchema($metadata); $this->entityManager->getConnection()->commit(); } catch (Exception $e) { $this->entityManager->getConnection()->rollback(); $this->entityManager->close(); throw $e; } $this->cleanCache(); }
/** * @param string $name * @param string $factory */ public function addWidget($name, $factory) { if (!is_string($name)) { throw new InvalidArgumentException('Name of widget must be string'); } if (!is_string($factory) && !method_exists($factory, 'create') && !is_callable($factory)) { throw new InvalidArgumentException('Second argument must be string or factory or callable'); } if (is_string($factory) && !$this->container->hasService($factory)) { throw new InvalidArgumentException(sprintf('Service \'%s\' does not exist', $factory)); } $this->widgets[$name] = $factory; }
/** * @param IModule $module */ public function uninstall(IModule $module) { if (!$this->context->hasService('doctrine') || !$this->context->doctrine->createCheckConnection()) { throw new \Exception('Database connection not found!'); } $layouts = $this->templateManager->getLayoutsByModule($module->getName()); $repository = $this->getTemplateRepository(); foreach ($layouts as $path => $name) { foreach ($repository->findBy(array('file' => $path)) as $entity) { $repository->delete($entity); } } }
/** * @param \Nette\DI\Container $dic * @throws MemberAccessException * @internal */ public function injectComponentFactories(Nette\DI\Container $dic) { if (!$this instanceof Nette\Application\UI\PresenterComponent && !$this instanceof Nette\Application\UI\Component) { throw new MemberAccessException('Trait ' . __TRAIT__ . ' can be used only in descendants of PresenterComponent.'); } $this->autowireComponentFactoriesLocator = $dic; $storage = $dic->hasService('autowired.cacheStorage') ? $dic->getService('autowired.cacheStorage') : $dic->getByType('Nette\\Caching\\IStorage'); $cache = new Nette\Caching\Cache($storage, 'Kdyby.Autowired.AutowireComponentFactories'); if ($cache->load($presenterClass = get_class($this)) !== NULL) { return; } $ignore = class_parents('Nette\\Application\\UI\\Presenter') + ['ui' => 'Nette\\Application\\UI\\Presenter']; $rc = new ClassType($this); foreach ($rc->getMethods() as $method) { if (in_array($method->getDeclaringClass()->getName(), $ignore, TRUE) || !Strings::startsWith($method->getName(), 'createComponent')) { continue; } foreach ($method->getParameters() as $parameter) { if (!($class = $parameter->getClassName())) { // has object type hint continue; } if (!$this->findByTypeForFactory($class) && !$parameter->allowsNull()) { throw new MissingServiceException("No service of type {$class} found. Make sure the type hint in {$method} is written correctly and service of this type is registered."); } } } $files = array_map(function ($class) { return ClassType::from($class)->getFileName(); }, array_diff(array_values(class_parents($presenterClass) + ['me' => $presenterClass]), $ignore)); $files[] = ClassType::from($this->autowireComponentFactoriesLocator)->getFileName(); $cache->save($presenterClass, TRUE, [$cache::FILES => $files]); }
/** * @param \Nette\DI\Container $dic * @throws MemberAccessException * @throws MissingServiceException * @throws InvalidStateException * @throws UnexpectedValueException */ public function injectProperties(Nette\DI\Container $dic) { if (!$this instanceof Nette\Application\UI\PresenterComponent && !$this instanceof Nette\Application\UI\Component) { throw new MemberAccessException('Trait ' . __TRAIT__ . ' can be used only in descendants of PresenterComponent.'); } $this->autowirePropertiesLocator = $dic; $storage = $dic->hasService('autowired.cacheStorage') ? $dic->getService('autowired.cacheStorage') : $dic->getByType('Nette\\Caching\\IStorage'); $cache = new Nette\Caching\Cache($storage, 'Kdyby.Autowired.AutowireProperties'); $containerFileName = ClassType::from($this->autowirePropertiesLocator)->getFileName(); $cacheKey = [$presenterClass = get_class($this), $containerFileName]; if (is_array($this->autowireProperties = $cache->load($cacheKey))) { foreach ($this->autowireProperties as $propName => $tmp) { unset($this->{$propName}); } return; } $this->autowireProperties = []; $ignore = class_parents('Nette\\Application\\UI\\Presenter') + ['ui' => 'Nette\\Application\\UI\\Presenter']; $rc = new ClassType($this); foreach ($rc->getProperties() as $prop) { if (!$this->validateProperty($prop, $ignore)) { continue; } $this->resolveProperty($prop); } $files = array_map(function ($class) { return ClassType::from($class)->getFileName(); }, array_diff(array_values(class_parents($presenterClass) + ['me' => $presenterClass]), $ignore)); $files[] = $containerFileName; $cache->save($cacheKey, $this->autowireProperties, [$cache::FILES => $files]); }
/** * @param $presenter * @return string */ public function formatPresenterFile($presenter) { $service = $this->formatPresenterFromServiceName($presenter); if ($this->container->hasService($service)) { return get_class($this->container->getService($service)); } return parent::formatPresenterFile($presenter); }
/** * @return bool */ public function __isset($name) { return $this->container->hasService($this->namespace . $name); }
/** * @param string $name * @return bool */ public function has($name) { return $this->container->hasService($name); }
/** * @param \Nette\DI\Container * @return Utils\IActionLogger */ public static function createServiceActionLogger(Container $container) { if ($container->hasService('doctrineContainer')) { return $container->doctrineContainer->getService('Nella\Utils\LoggerStorages\ActionEntity'); } else { return new Utils\LoggerStorages\FileStorage; } }
/** * @param \Nette\DI\Container * @return \Doctrine\ORM\Configuration */ public static function createServiceConfiguration(DI\Container $context) { $config = new \Doctrine\ORM\Configuration; // Cache $storage = $context->hasService('metadataCache') ? $context->metadataCache : new Cache($context->cacheStorage); $config->setMetadataCacheImpl($storage); $storage = $context->hasService('queryCache') ? $context->queryCache : new Cache($context->cacheStorage); $config->setQueryCacheImpl($storage); // Metadata $config->setClassMetadataFactoryName('Nella\Doctrine\Mapping\ClassMetadataFactory'); $config->setMetadataDriverImpl($context->annotationDriver); // Proxies $config->setProxyDir($this->configuration['proxyDir']); $config->setProxyNamespace($this->configuration['proxyNamespace']); if ($this->configuration['productionMode']) { $config->setAutoGenerateProxyClasses(FALSE); } else { if ($context->hasService('logger')) { $config->setSQLLogger($context->logger); } $config->setAutoGenerateProxyClasses(TRUE); } return $config; }