public function loadConfiguration() { $container = $this->getContainerBuilder(); $config = $this->getConfig($this->defaults); $entities = $collections = array(); foreach ($config['entity'] as $domain => $entity) { $service = $container->addDefinition($entities[$domain] = $this->prefix($domain . '.entity')); Compiler::parseService($service, $entity); $service->setClass('NiceORM\\Entity')->setParameters(array('data' => NULL))->setShared(FALSE); $container->addDefinition($collections[$domain] = $this->prefix($domain . '.collection'))->setClass('NiceORM\\ICollection')->setFactory('NiceORM\\TableCollection', array($domain, '%data%'))->setParameters(array('data'))->setShared(FALSE); } $fields = $refs = $related = array(); foreach ($config['tables'] as $domain => $table) { $fields[$domain] = $refs[$domain] = $related[$domain] = array(); foreach (Arrays::get($config['fields'], $domain, array()) as $name => $column) { $fields[$domain][is_int($name) ? $column : $name] = $column; } foreach (Arrays::get($config['relations'], $domain, array()) as $name => $column) { if (is_int($name)) { $refs[$domain][$column] = array($column, NULL, $column); } else { $column = array_values((array) $column); switch (count($column)) { case 0: $refs[$domain][$name] = array($name, NULL, $name); break; case 1: $refs[$domain][$name] = array($name, $column[0], $name); break; case 2: $refs[$domain][$name] = array($column[0], $column[1], $name); break; default: $refs[$domain][$name] = array($column[0], $column[1], $column[2]); } } } } foreach ($refs as $domain => $tableRefs) { foreach ($tableRefs as $name => $ref) { list($table, $column, $type) = $ref; if ($type === $name || empty($related[$type][$domain . 's'])) { $related[$type][$domain . 's'] = array($config['tables'][$domain], $column, $domain); } elseif (!isset($refs[$domain][$type])) { unset($related[$type][$domain . 's']); } $related[$type][$domain . 's:' . $name] = array($config['tables'][$domain], $column, $domain); } } $mappers = $accessors = array(); foreach ($config['tables'] as $domain => $table) { $mapper = $container->addDefinition($mappers[$domain] = $this->prefix($domain . '.mapper'))->setClass('NiceORM\\ActiveRowMapper', array($domain, $table, $fields[$domain], $refs[$domain], $related[$domain])); $container->addDefinition($accessors[$domain] = $this->prefix($domain . '.accessor'))->setClass('NiceORM\\ActiveRowAccessor', array('%data%', $mapper))->setParameters(array('data'))->setShared(FALSE); } $entityFactory = $container->addDefinition($this->prefix('entityFactory'))->setClass('NiceORM\\DIEntityFactory', array($entities)); $collectionFactory = $container->addDefinition($this->prefix('collectionFactory'))->setClass('NiceORM\\DICollectionFactory', array($collections)); $accessorFactory = $container->addDefinition($this->prefix('accessorFactory'))->setClass('NiceORM\\DIAccessorFactory', array($accessors)); $mapperAccessor = $container->addDefinition($this->prefix('mapperAccessor'))->setClass('NiceORM\\DIMapperAccessor', array($mappers)); $container->addDefinition($this->prefix('manager'))->setClass('NiceORM\\Manager')->setFactory('NiceORM\\Manager', array($entityFactory, $collectionFactory, $accessorFactory, $mapperAccessor)); }
/** * @param array $args * @return int */ public function run(array $args = []) { /** @var ICollection|Addon[] $addons */ $addons = $this->addonRepository->findBowers(); // DO YOUR JOB =============================================== $counter = 0; foreach ($addons as $addon) { // Skip addon with bad data if ($extra = $addon->github->extra) { if ($bower = $extra->get('bower', FALSE)) { // Create bower entity if not exist if (!$addon->bower) { $addon->bower = new Bower(); } // Downloads if ($stats = $this->bower->repo($bower['name'])) { $addon->bower->downloads = Arrays::get($stats, ['hits'], 0); } // Keywords $keywords = Arrays::get($bower, 'keywords', []); $addon->bower->keywords = $keywords ? implode(',', $keywords) : NULL; // Persist $this->addonRepository->persistAndFlush($addon); // Increase counting $counter++; } else { $this->log('Skip (bower) [no bower data]: ' . $addon->fullname); } } else { $this->log('Skip (bower) [no extra data]: ' . $addon->fullname); } } return $counter; }
/** * @param Configurator $configurator */ protected function loadConfigFiles(Configurator $configurator) { if ($this->autoloadConfig === TRUE || is_array($this->autoloadConfig)) { $scanDirs = $this->autoloadConfig === TRUE ? [$this->appDir] : $this->autoloadConfig; $cache = $this->createCache(); $files = $cache->load(self::CACHE_NAMESPACE); if ($files === NULL) { $files = [0 => []]; foreach (Finder::findFiles('*.neon')->from($scanDirs) as $path => $file) { $content = Neon::decode(file_get_contents($path)); if (!is_array($content) || !array_key_exists('autoload', $content)) { continue; } $autoload = Arrays::get($content, ['autoload', 0], FALSE); if ($autoload === FALSE) { continue; } $autoload = is_int($autoload) ? $autoload : 0; if (!isset($files[$autoload])) { $files[$autoload] = []; } $files[$autoload][] = $path; } $cache->save(self::CACHE_NAMESPACE, $files); } foreach ($files as $priorityFiles) { foreach ($priorityFiles as $config) { $configurator->addConfig($config); } } } foreach ($this->configs as $config) { $configurator->addConfig($config); } }
/** * Loads HTTP data. * * @return void */ public function loadHttpData() { $path = $this->getHtmlName(); // img_x or img['x'] $path = explode('[', strtr(str_replace(']', '', strpos($path, '[') === false ? $path . '.x' : substr($path, 0, -2)), '.', '_')); $this->setValue(Nette\Utils\Arrays::get($this->getForm()->getHttpData(), $path, null)); }
/** * @return mixed|NULL */ private function getHttpData() { if ($this->http_post === NULL) { $path = explode(self::NAME_SEPARATOR, $this->lookupPath('Nette\\Forms\\Form')); $this->http_post = Nette\Utils\Arrays::get($this->getForm()->getHttpData(), $path, NULL); } return $this->http_post; }
/** * @param string $message * @param int $count * @return string */ function translate($message, $count = 0) { if (substr($message, 0, 5) != "book.") { return ""; } $this->loadTexts(); return Arrays::get($this->texts, substr($message, 5), ""); }
/** * @param string $name * @param mixed $default * @return mixed */ public function get($name, $default = NULL) { if (func_num_args() > 1) { return Arrays::get($this->parameters, $name, $default); } else { return Arrays::get($this->parameters, $name); } }
/** * Loads HTTP data. * @return void */ public function loadHttpData() { $path = \explode('[', \strtr(\str_replace(array('[]', ']'), '', $this->getHtmlName()), '.', '_')); $origValue = Arrays::get($this->getForm()->getHttpData(), $path); $from = isset($origValue['from']) ? $origValue['from'] : ''; $to = isset($origValue['to']) ? $origValue['to'] : ''; $value = array('from' => $from, 'to' => $to); $this->setValue($value); }
/** * Expands %placeholders%. * @param mixed * @param array * @param bool * @return mixed * @throws Nette\InvalidArgumentException */ public static function expand($var, array $params, $recursive = FALSE) { if (is_array($var)) { $res = array(); foreach ($var as $key => $val) { $res[$key] = self::expand($val, $params, $recursive); } return $res; } elseif ($var instanceof \stdClass || $var instanceof Statement) { $res = clone $var; foreach ($var as $key => $val) { $res->$key = self::expand($val, $params, $recursive); } return $res; } elseif (!is_string($var)) { return $var; } $parts = preg_split('#%([\w.-]*)%#i', $var, -1, PREG_SPLIT_DELIM_CAPTURE); $res = ''; foreach ($parts as $n => $part) { if ($n % 2 === 0) { $res .= $part; } elseif ($part === '') { $res .= '%'; } elseif (isset($recursive[$part])) { throw new Nette\InvalidArgumentException(sprintf('Circular reference detected for variables: %s.', implode(', ', array_keys($recursive)))); } else { try { $val = Nette\Utils\Arrays::get($params, explode('.', $part)); } catch (Nette\InvalidArgumentException $e) { throw new Nette\InvalidArgumentException("Missing parameter '$part'.", 0, $e); } if ($recursive) { $val = self::expand($val, $params, (is_array($recursive) ? $recursive : array()) + array($part => 1)); } if (strlen($part) + 2 === strlen($var)) { return $val; } if (!is_scalar($val)) { throw new Nette\InvalidArgumentException("Unable to concatenate non-scalar parameter '$part' into '$var'."); } $res .= $val; } } return $res; }
/** * @param array $args * @return int */ public function run(array $args = []) { /** @var ICollection|Addon[] $addons */ $addons = $this->addonRepository->findComposers(); // DO YOUR JOB =============================================== $counter = 0; foreach ($addons as $addon) { try { // Skip addon with bad data if ($extra = $addon->github->extra) { if ($composer = $extra->get('composer', FALSE)) { if (!isset($composer['name'])) { throw new InvalidStateException('No composer name at ' . $addon->fullname); } list($owner, $repo) = explode('/', $composer['name']); // Create composer entity if not exist if (!$addon->composer) { $addon->composer = new Composer(); } // Basic info $addon->composer->name = Arrays::get($composer, 'name', NULL); $addon->composer->description = Arrays::get($composer, 'description', NULL); $addon->composer->type = Arrays::get($composer, 'type', NULL); // Downloads if ($stats = $this->composer->repo($owner, $repo)) { $addon->composer->downloads = Arrays::get($stats, ['package', 'downloads', 'total'], 0); } // Keywords $keywords = Arrays::get($composer, 'keywords', []); $addon->composer->keywords = $keywords ? implode(',', $keywords) : NULL; // Persist $this->addonRepository->persistAndFlush($addon); // Increase counting $counter++; } else { $this->log('Skip (composer) [no composer data]: ' . $addon->fullname); } } else { $this->log('Skip (composer) [no extra data]: ' . $addon->fullname); } } catch (Exception $e) { Debugger::log($e, Debugger::EXCEPTION); $this->log('Skip (composer) [exception]: ' . $e->getMessage()); } } return $counter; }
public function getValue() { if (!$this->_valueLoaded) { $path = explode('[', strtr(str_replace(array('[]', ']'), '', $this->getHtmlName()), '.', '_')); unset($path[count($path) - 1]); $values = \Nette\Utils\Arrays::get((array) $this->getForm()->getHttpData(), $path, NULL); // remove photos if ($this->multi) { if (!$this->fileEntity) { $this->fileEntity = new ArrayCollection(); } foreach ($this->fileEntity as $file) { $delete = isset($values[$this->name . '_delete_' . $file->id]) && $values[$this->name . '_delete_' . $file->id] == 'on'; if ($delete) { $coll = $this->fileEntity; $coll->removeElement($file); } } } else { if ($this->fileEntity) { $delete = isset($values[$this->name . '_delete_' . $this->fileEntity->id]) && $values[$this->name . '_delete_' . $this->fileEntity->id] == 'on'; if ($delete) { return NULL; } } } // get photos if ($values) { if ($this->multi) { for ($i = 0; $i < 20; $i++) { if ($values[$this->name . '-' . $i] && $values[$this->name . '-' . $i]->isOk()) { $this->fileEntity[] = $entity = new FileEntity(); $entity->setFile($values[$this->name . '-' . $i]); } } } else { if ($values[$this->name] && $values[$this->name]->isOk()) { $this->fileEntity = $entity = new FileEntity(); $entity->setFile($values[$this->name]); } } } $this->_valueLoaded = true; } return $this->fileEntity; }
/** * Extracts and sanitizes submitted form data for single control. * @param array submitted data * @param string control HTML name * @param string type Form::DATA_TEXT, DATA_LINE, DATA_FILE * @return string|string[] */ public static function extractHttpData(array $data, $htmlName, $type) { $name = explode('[', str_replace(array('[]', ']', '.'), array('', '', '_'), $htmlName)); $data = Nette\Utils\Arrays::get($data, $name, NULL); if (substr($htmlName, -2) === '[]') { $arr = array(); foreach (is_array($data) ? $data : array() as $v) { $arr[] = $v = static::sanitize($type, $v); if ($v === NULL) { return array(); } } return $arr; } else { return static::sanitize($type, $data); } }
/** * Expands %placeholders%. * @param mixed * @param array * @param bool * @return mixed * @throws Nette\InvalidArgumentException */ public static function expand($var, array $params, $recursive = FALSE) { if (is_array($var)) { $res = array(); foreach ($var as $key => $val) { $res[$key] = self::expand($val, $params, $recursive); } return $res; } elseif ($var instanceof Statement) { return new Statement(self::expand($var->entity, $params, $recursive), self::expand($var->arguments, $params, $recursive)); } elseif (!is_string($var)) { return $var; } $parts = preg_split('#%([\\w.-]*)%#i', $var, -1, PREG_SPLIT_DELIM_CAPTURE); $res = ''; foreach ($parts as $n => $part) { if ($n % 2 === 0) { $res .= $part; } elseif ($part === '') { $res .= '%'; } elseif (isset($recursive[$part])) { throw new Nette\InvalidArgumentException('Circular reference detected for variables: ' . implode(', ', array_keys($recursive)) . '.'); } else { $val = Nette\Utils\Arrays::get($params, explode('.', $part)); if ($recursive) { $val = self::expand($val, $params, (is_array($recursive) ? $recursive : array()) + array($part => 1)); } if (strlen($part) + 2 === strlen($var)) { return $val; } if (!is_scalar($val)) { throw new Nette\InvalidArgumentException("Unable to concatenate non-scalar parameter '{$part}' into '{$var}'."); } $res .= $val; } } return $res; }
/** * Extracts and sanitizes submitted form data for single control. * @param array submitted data * @param string control HTML name * @param string type Form::DATA_TEXT, DATA_LINE, DATA_FILE, DATA_KEYS * @return string|string[] * @internal */ public static function extractHttpData(array $data, $htmlName, $type) { $name = explode('[', str_replace(array('[]', ']', '.'), array('', '', '_'), $htmlName)); $data = Nette\Utils\Arrays::get($data, $name, NULL); $itype = $type & ~Form::DATA_KEYS; if (substr($htmlName, -2) === '[]') { if (!is_array($data)) { return array(); } foreach ($data as $k => $v) { $data[$k] = $v = static::sanitize($itype, $v); if ($v === NULL) { unset($data[$k]); } } if ($type & Form::DATA_KEYS) { return $data; } return array_values($data); } else { return static::sanitize($itype, $data); } }
/** * Returns uploaded file. * @param string key (or more keys) * @return FileUpload */ public final function getFile($key) { $args = func_get_args(); return Nette\Utils\Arrays::get($this->files, $args, NULL); }
/** * Loads HTTP data. * @return void */ public function loadHttpData() { $path = explode('[', strtr(str_replace(array('[]', ']'), '', $this->getHtmlName()), '.', '_')); $this->setValue(Nette\Utils\Arrays::get($this->getForm()->getHttpData(), $path, NULL)); }
/** * Pre-expands %placeholders% in string. * @internal */ public static function preExpand($s, array $params, $check = array()) { if (!is_string($s)) { return $s; } $parts = preg_split('#%([\\w.-]*)%#i', $s, -1, PREG_SPLIT_DELIM_CAPTURE); $res = ''; foreach ($parts as $n => $part) { if ($n % 2 === 0) { $res .= str_replace('%', '%%', $part); } elseif ($part === '') { $res .= '%%'; } elseif (isset($check[$part])) { throw new Nette\InvalidArgumentException('Circular reference detected for variables: ' . implode(', ', array_keys($check)) . '.'); } else { try { $val = Nette\Utils\Arrays::get($params, explode('.', $part)); } catch (Nette\InvalidArgumentException $e) { $res .= "%{$part}%"; continue; } $val = self::preExpand($val, $params, $check + array($part => 1)); if (strlen($part) + 2 === strlen($s)) { if (is_array($val)) { array_walk_recursive($val, function (&$val) use($params, $check, $part) { $val = Configurator::preExpand($val, $params, $check + array($part => 1)); }); } return $val; } if (!is_scalar($val)) { throw new Nette\InvalidArgumentException("Unable to concatenate non-scalar parameter '{$part}' into '{$s}'."); } $res .= $val; } } return $res; }
public function getDefault($vars) { if ($vars == NULL) { return ";"; } else { if (!is_array($vars)) { return " = " . $this->parseValue($vars) . ";"; } else { if (($default = Arrays::get($vars, 'default', NULL)) != NULL) { return " = " . $this->parseValue($default) . ";"; } else { return ";"; } } } }
/** * Expands %placeholders% in string. * @param string * @return string * @throws Nette\InvalidStateException */ public function expand($s) { if (is_string($s) && strpos($s, '%') !== FALSE) { $that = $this; return @preg_replace_callback('#%([a-z0-9._-]*)%#i', function ($m) use($that) { // intentionally @ due PHP bug #39257 list(, $param) = $m; if ($param === '') { return '%'; } elseif (!is_scalar($val = Nette\Utils\Arrays::get((array) $that->params, explode('.', $param)))) { throw new Nette\InvalidStateException("Parameter '{$param}' is not scalar."); } return $val; }, $s); } return $s; }
/** * Filter methods */ private function filterMethods() { foreach ($this->context->getReflection()->getMethods() as $method) { if (strpos($method, "SystemContainer") !== FALSE) { $match = Strings::match($method, "#.+::([a-zA-Z0-9_]+)#"); $res = Arrays::get((array) $match, 1, NULL); $parameters = $this->getMethodParameters($method->getParameters()); if (strpos($res, "createServiceNette") !== FALSE) { $this->netteServices[] = $this->buildFunction($res, $parameters); } else { if (strpos($res, "createService") !== FALSE) { $this->services[] = $this->buildFunction($res, $parameters); } else { if (strpos($res, "createNette") !== FALSE) { $this->netteServices[] = $this->buildFunction($res, $parameters); } else { if (strpos($res, "create") !== FALSE) { $this->factories[] = $this->buildFunction($res, $parameters); } } } } } } }
function loadHttpData() { $path = $this->getHtmlName(); $path = explode('[', strtr(str_replace(']', '', strpos($path, '[') === FALSE ? $path . '.x' : substr($path, 0, -2)), '.', '_')); $this->setValue(Nette\Utils\Arrays::get($this->getForm()->getHttpData(), $path, NULL)); }
/** * Returns uploaded file. * @param string key * @return FileUpload|NULL */ public function getFile($key) { if (func_num_args() > 1) { trigger_error('Calling getFile() with multiple keys is deprecated.', E_USER_DEPRECATED); return Nette\Utils\Arrays::get($this->files, func_get_args(), NULL); } return isset($this->files[$key]) ? $this->files[$key] : NULL; }
/** @return array */ function getFriends() { return Arrays::get($this->fb->api('/me/friends'), 'data'); }
/** * Returns value of parameter in DI container. It starts at * parameters: key in config and you can * use dot to navigate to deeper levels. * * @param $key * @param mixed $default Default value returned, if value was not found. * @return mixed */ protected function InjectableTrait_getParameter($key, $default = null) { return \Nette\Utils\Arrays::get($this->InjectableTrait_parameters, explode('.', $key), $default); }
public function create($type = NULL, $data = NULL, Repository $repository = NULL) { $class = Arrays::get($this->classes, $type, $this->classes[NULL]); return new $class($this->normalizeData($data), $repository); }
/** * @author Petr Besir Horacek <*****@*****.**> * @param string $parameter * @return mixed */ protected function getConfigParameter($parameter = NULL) { return \Nette\Utils\Arrays::get($this->configParameters, $parameter, NULL); }
/** * Returns uploaded file. * @param string key (or more keys) * @return FileUpload|NULL */ public function getFile($key) { return Nette\Utils\Arrays::get($this->files, func_get_args(), NULL); }
public function create($name) { $table = Arrays::get($this->tables, $name, $name); return new Selection($table, $this->connection); }
/** * @param array $config */ public function __construct(array $config) { $this->nestedTransactions = Arrays::get($config, 'nestedTransactions'); }
/** * @return array */ private function getHttpData() { $path = $this->lookupPath('Nette\\Application\\UI\\Form'); $allData = $this->getForm()->getHttpData(); return Nette\Utils\Arrays::get($allData, $path, NULL); }