/** * save * * @param int|string $pk * @param DataInterface $data * * @return boolean * * @throws \Windwalker\Record\Exception\NoResultException * @throws \UnexpectedValueException * @throws \RuntimeException * @throws \InvalidArgumentException * @throws \LogicException * @throws \DomainException */ protected function save($pk, DataInterface $data) { // We load existing item first and bind data into it. $record = $this->model->getRecord(); $record->reset(); $record->load($pk); $record->bind($data); $recordClone = $this->model->getRecord(); $condition = array(); // Check table has increment fields, default is title and alias. foreach ($this->incrementFields as $field => $type) { if ($record->hasField($field)) { $condition[$field] = $record[$field]; } } // Recheck item with same conditions(default is title & alias), if true, increment them. // If no item got, means it is the max number. do { $result = true; try { $recordClone->load($condition); foreach ($this->incrementFields as $field => $type) { if ($record->hasField($field)) { $record[$field] = $condition[$field] = StringHelper::increment($record[$field], $type); } } } catch (\RuntimeException $e) { $result = false; } $recordClone->reset(false); } while ($result); unset($record->{$this->keyName}); return $this->model->save($record); }
/** * Method to save item. * * @param int $pk The primary key value. * @param array $data The item data. * * @return mixed */ protected function save($pk, $data) { if (!$this->allowAdd($data)) { return false; } // We load existing item first and bind data into it. $this->table->reset(); $this->table->load($pk); $this->table->bind($data); // Dump as array $item = $this->table->getProperties(true); // Handle Title increment $table2 = $this->model->getTable(); $condition = array(); // Check table has increment fields, default is title and alias. foreach ($this->incrementFields as $field => $type) { if (property_exists($this->table, $field)) { $condition[$field] = $item[$field]; } } // Recheck item with same conditions(default is title & alias), if true, increment them. // If no item got, means it is the max number. while ($table2->load($condition)) { foreach ($this->incrementFields as $field => $type) { if (property_exists($this->table, $field)) { $item[$field] = $condition[$field] = StringHelper::increment($item[$field], $type); } } } // Unset the primary key so that we can copy it. unset($item[$this->urlVar]); return $this->model->save($item); }
/** * doExecute * * @return mixed */ public function doExecute() { // Load SQL file. @($installFile = file_get_contents($this->config['dir.src'] . '/sql/install.sql')); @($uninstallFile = file_get_contents($this->config['dir.src'] . '/sql/uninstall.sql')); $installSql = StringHelper::parseVariable($installFile, $this->config['replace']); $uninstallSql = StringHelper::parseVariable($uninstallFile, $this->config['replace']); // Prevent import twice $table = '#__' . $this->config['name'] . '_' . $this->config['replace.controller.list.name.lower']; $db = Container::getInstance()->get('db'); try { $db->getTableColumns($table); } catch (\RuntimeException $e) { // Import sql $this->controller->out('Importing SQL to table: ' . $table); $this->executeSql($installSql); $this->controller->out('Imported'); } if (!strpos($installSql, $table)) { // Write SQL file to project. @($fp = fopen($this->config['dir.dest'] . '/sql/install.sql', 'a+')); @fputs($fp, "\n\n\n" . $installSql); @fclose($fp); @($fp = fopen($this->config['dir.dest'] . '/sql/uninstall.sql', 'a+')); @fputs($fp, "\n\n" . $uninstallSql); @fclose($fp); } }
/** * Do this execute. * * @return mixed */ protected function doExecute() { $tmpl = file_get_contents(GENERATOR_BUNDLE_PATH . '/Template/test/testClass.php'); $file = StringHelper::parseVariable($tmpl, $this->replace); Folder::create(dirname($this->config['replace.test.class.file'])); file_put_contents($this->config['replace.test.class.file'], $file); }
/** * copyFile * * @param string $src * @param string $dest * @param array $replace * * @return void */ protected function copyFile($src, $dest, $replace = array()) { // Replace dest file name. $dest = StringHelper::parseVariable($dest, $replace); if (is_file($dest)) { $this->io->out('File exists: ' . $dest); } else { $content = StringHelper::parseVariable(file_get_contents($src), $replace); if (File::write($dest, $content)) { $this->io->out('File created: ' . $dest); } } }
/** * Execute the controller. * * @return boolean True if controller finished execution, false if the controller did not * finish execution. A controller might return false if some precondition for * the controller to run has not been satisfied. * * @since 1.0 * @throws \LogicException * @throws \RuntimeException */ public function execute() { // Flip src & dest $dest = $this->config['path.src']; $src = $this->config['path.dest']; $this->config['path.src'] = $src; $this->config['path.dest'] = $dest; // Flip replace array $this->replace = array_flip($this->replace); // Quote by tag variable foreach ($this->replace as &$replace) { $replace = StringHelper::quote($replace, (array) $this->config['tag.variable']); } $this->doAction(new Action\ConvertAction()); }
/** * Do this execute. * * @return mixed */ protected function doExecute() { /** @var CopyOperator $copyOperator */ $copyOperator = $this->container->get('operator.factory')->getOperator('copy'); $src = $this->config['dir.src']; $dest = $this->config['dir.dest']; $list = StringHelper::quote('controller.list.name.cap', $this->config['tagVariables']); $files = array('Controller/%s', 'Field', 'Form/%s', 'Model/%sModel.php.tpl', 'Templates/' . StringHelper::quote('controller.list.name.lower', $this->config['tagVariables']), 'View/%s'); foreach ($files as $file) { $file = sprintf($file, $list); if (!file_exists($src . '/' . $file)) { continue; } $copyOperator->copy($src . '/' . $file, $dest . '/' . $file, $this->config['replace']); } }
/** * Execute the controller. * * @return boolean True if controller finished execution, false if the controller did not * finish execution. A controller might return false if some precondition for * the controller to run has not been satisfied. * * @since 1.0 * @throws \LogicException * @throws \RuntimeException */ public function execute() { // Flip replace string and path because we are converting code back to template. // Flip src & dest $dest = $this->config['path.src']; $src = $this->config['path.dest']; $this->config['path.src'] = $src; $this->config['path.dest'] = $dest; // Flip replace array $this->replace = array_flip($this->replace); // Quote by tag variable foreach ($this->replace as &$replace) { $replace = StringHelper::quote($replace, (array) $this->config['tag.variable']); } // Do it now. $this->doAction(new Action\ConvertAction()); }
/** * Execute the controller. * * @return boolean True if controller finished execution, false if the controller did not * finish execution. A controller might return false if some precondition for * the controller to run has not been satisfied. * * @throws \LogicException * @throws \RuntimeException */ public function execute() { $this->config['item_name'] = StringHelper::quote('controller.item.name.cap', $this->config['tagVariables']); $this->config['list_name'] = StringHelper::quote('controller.list.name.cap', $this->config['tagVariables']); $this->doAction(new Subsystem\PrepareAction()); $this->doAction(new Subsystem\CopyItemAction()); // Some dirty things handling $this->doAction(new AddTableNameAction()); $this->doAction(new CopyMigrationAction()); $this->doAction(new AddSeederAction()); if ($this->config['migrate']) { $this->doAction(new MigrateAction()); if ($this->config['seed']) { $this->doAction(new SeedAction()); } } }
/** * Do this execute. * * @return mixed */ protected function doExecute() { /** @var ConvertOperator $operator */ $operator = $this->container->get('operator.factory')->getOperator('convert'); $replace = ArrayHelper::flatten($this->replace); // Flip replace array because we want to convert template. $replace = array_flip($replace); foreach ($replace as &$val) { $val = StringHelper::quote($val, $operator->getTagVariable()); } // Flip src and dest because we want to convert template. $src = $this->config['dir.src']; $dest = $this->config['dir.dest']; if (is_dir($dest)) { // Remove dir first Folder::delete($dest); } $operator->copy($src, $dest, $replace); }
/** * Pose execute hook. * * @param mixed $return Executed return value. * * @return mixed */ protected function postExecute($return = null) { // Run old save process to release edit id. parent::postExecute($return); // Attempt to check-in the current record. $data = array('cid' => array($this->recordId), 'quiet' => true); $this->fetch($this->prefix, $this->viewList . '.check.checkin', $data); // Reset the ID and then treat the request as for Apply. $this->data[$this->key] = 0; $this->data['checked_out'] = ''; $this->data['checked_out_time'] = ''; if (isset($this->data['title'])) { $this->data['title'] = StringHelper::increment($this->data['title']); } if (isset($this->data['alias'])) { $this->data['alias'] = StringHelper::increment($this->data['alias'], 'dash'); } // Set new date into session. $this->app->setUserState($this->context . '.data', $this->data); return $return; }
/** * doExecute * * @return int */ protected function doExecute() { $path = $this->getArgument(0); $package = $this->getOption('p'); $folder = $this->console->get('asset.folder', 'asset'); if ($package = PackageHelper::getPackage($package)) { $path = $package->getDir() . '/Resources/asset/' . $path; } else { $path = WINDWALKER_PUBLIC . '/' . trim($folder, '/') . '/' . $path; } if (is_file($path)) { $files = array(new \SplFileInfo($path)); } elseif (is_dir($path)) { $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, \FilesystemIterator::FOLLOW_SYMLINKS)); } else { throw new \InvalidArgumentException('No path'); } /** @var \SplFileInfo $file */ foreach ($files as $file) { $ext = File::getExtension($file->getPathname()); if (StringHelper::endsWith($file->getBasename(), '.min.' . $ext)) { continue; } if ($ext == 'css') { $this->out('[<comment>Compressing</comment>] ' . $file); $data = \Minify_CSS_Compressor::process(file_get_contents($file)); $data = str_replace("\n", ' ', $data); } elseif ($ext == 'js') { $this->out('[<comment>Compressing</comment>] ' . $file); $data = \JSMinPlus::minify(file_get_contents($file)); $data = str_replace("\n", ';', $data); } else { continue; } $newName = $file->getPath() . '/' . File::stripExtension($file->getBasename()) . '.min.' . $ext; file_put_contents($newName, $data); $this->out('[<info>Compressed</info>] ' . $newName); } }
/** * normalizeBacktrace * * @param array $trace * * @return array */ public static function normalizeBacktrace(array $trace) { $trace = new Data($trace); $args = []; foreach ($trace['args'] as $arg) { if (is_array($arg)) { $arg = 'Array'; } elseif (is_object($arg)) { $arg = ReflectionHelper::getShortName($arg); } elseif (is_string($arg)) { if (Utf8String::strlen($arg) > 20) { $arg = Utf8String::substr($arg, 0, 20) . '...'; } $arg = StringHelper::quote($arg); } elseif (is_null($arg)) { $arg = 'NULL'; } elseif (is_bool($arg)) { $arg = $arg ? 'TRUE' : 'FALSE'; } $args[] = $arg; } return array('file' => $trace['file'] ? $trace['file'] . ' (' . $trace['line'] . ')' : null, 'function' => ($trace['class'] ? $trace['class'] . $trace['type'] : null) . $trace['function'] . sprintf('(%s)', implode(', ', $args))); }
/** * Get minify file name. * * - If in debug mode, we will search for .min file, if minify file not exists, retuen nomral file. * - If not in debug mode, we will search for normal file, if normal file not exists but min file does, retuen min file. * - If both not exists, return false. * * @param string $path The path to check file. * @param string $file The file name. * * @return string|false The found file or false. */ protected function getMinFile($path, $file) { $ext = \JFile::getExt($file); if (StringHelper::endsWith($file, '.min.' . $ext)) { $assetFile = substr($file, 0, -strlen('.min.' . $ext)) . '.' . $ext; $assetMinFile = $file; } else { $assetMinFile = substr($file, 0, -strlen('.' . $ext)) . '.min.' . $ext; $assetFile = $file; } // Use uncompressed file first if ($this->debug) { if (is_file($path . '/' . $assetFile)) { return $assetFile; } if (is_file($path . '/' . $assetMinFile)) { return $assetMinFile; } } else { if (is_file($path . '/' . $assetMinFile)) { return $assetMinFile; } if (is_file($path . '/' . $assetFile)) { return $assetFile; } } // All file not found, fallback to default path. return false; }
/** * testCollapseWhitespace * * @return void */ public function testCollapseWhitespace() { $this->assertEquals('foo bar', StringHelper::collapseWhitespace(' foo bar ')); $this->assertEquals('foo bar', StringHelper::collapseWhitespace(" foo \n \r bar \n ")); }
/** * convertPath * * @param string $path * * @return string */ protected function convertPath($path) { $user = Container::getInstance()->get('user'); $date = DateHelper::getDate(); $replace = array('username' => $user->username, 'name' => $user->name, 'session' => \JFactory::getSession()->getId(), 'year' => $date->year, 'month' => $date->month, 'day' => $date->day); return StringHelper::parseVariable($path, $replace); }
/** * Execute the controller. * * @return boolean True if controller finished execution, false if the controller did not * finish execution. A controller might return false if some precondition for * the controller to run has not been satisfied. * * @throws \LogicException * @throws \RuntimeException */ public function execute() { $package = $this->io->getArgument(0, new ValidatePrompter('Enter package name: ')); $class = $this->io->getArgument(1, new ValidatePrompter('Enter class name: ')); $class = StringNormalise::toClassNamespace($class); $target = $this->io->getArgument(2, $package . '\\' . $class . 'Test'); $target = StringNormalise::toClassNamespace($target); $package = ucfirst($package); if (!class_exists($class)) { $class = 'Windwalker\\' . $package . '\\' . $class; } if (!class_exists($class)) { $this->out('Class not exists: ' . $class); exit; } $replace = $this->replace; $ref = new \ReflectionClass($class); $replace['origin.class.dir'] = dirname($ref->getFileName()); $replace['origin.class.file'] = $ref->getFileName(); $replace['origin.class.name'] = $ref->getName(); $replace['origin.class.shortname'] = $ref->getShortName(); $replace['origin.class.namespace'] = $ref->getNamespaceName(); $replace['test.dir'] = WINDWALKER_ROOT . DIRECTORY_SEPARATOR . 'test'; $replace['test.class.name'] = 'Windwalker\\Test\\' . $target; $replace['test.class.file'] = Path::clean($replace['test.dir'] . DIRECTORY_SEPARATOR . $target . '.php'); $replace['test.class.dir'] = dirname($replace['test.class.file']); $replace['test.class.shortname'] = $this->getShortname(StringNormalise::toClassNamespace($replace['test.class.name'])); $replace['test.class.namespace'] = $this->getNamespace($replace['test.class.name']); $this->replace = $replace; $config = new Registry(); // Set replace to config. foreach ($this->replace as $key => $val) { $config->set('replace.' . $key, $val); } $methods = $ref->getMethods(\ReflectionMethod::IS_PUBLIC); $methodTmpl = file_get_contents(GENERATOR_BUNDLE_PATH . '/Template/test/testMethod.php'); $methodCodes = array(); foreach ($methods as $method) { $config['replace.origin.method'] = $method->getName(); $config['replace.test.method'] = ucfirst($method->getName()); $methodCodes[] = StringHelper::parseVariable($methodTmpl, $config->get('replace')); } $config['replace.test.methods'] = implode("", $methodCodes); $this->replace = $config->get('replace'); $this->config = $config; $this->doAction(new GenClassAction()); $this->out('Generate test class: ' . $replace['test.class.name'] . ' to file: ' . $replace['test.class.file'])->out(); return true; }
/** * getSubsystemText * * @param \SplFileinfo $file * * @return string */ protected function getSubsystemText(\SplFileinfo $file) { $text = file_get_contents($file); $text = substr($text, strpos($text, '; {{controller.item.name.cap}}') - strlen($text)); return StringHelper::parseVariable($text, $this->config['replace']); }
/** * Test... * * @param string $string String to increment. * @param string $style Default of Dash. * @param string $number Number to increment. * @param string $expected Expected value. * * @return void * * @covers Windwalker\String\StringHelper::increment * @dataProvider seedTestIncrement * @since 1.0 */ public function testIncrement($string, $style, $number, $expected) { $this->assertEquals($expected, StringHelper::increment($string, $style, $number)); }
/** * Process ordering query. * * @param JDatabaseQuery $query The query object. * @param string $ordering The ordering string. * @param string $direction ASC or DESC. * * @return void */ protected function processOrdering(JDatabaseQuery $query, $ordering = null, $direction = null) { $ordering = $ordering ?: $this->get('list.ordering'); // If no ordering set, ignore this function. if (!$ordering) { return; } $direction = $direction ?: $this->get('list.direction', 'ASC'); $ordering = explode(',', $ordering); // Add quote foreach ($ordering as $key => &$value) { // Remove extra spaces preg_replace('/\\s+/', ' ', trim($value)); $value = StringHelper::explode(' ', $value); if (!$this->filterField($value[0])) { unset($ordering[$key]); continue; } $value[0] = $this->mapField($value[0]); // Ignore expression if (!empty($value[0]) && $value[0][strlen($value[0]) - 1] != ')') { $value[0] = $query->quoteName($value[0]); } $value = implode(' ', $value); } $ordering = implode(', ', $ordering); if (!$ordering) { return; } $query->order($ordering . ' ' . $direction); }
/** * Method to change the title & alias. * * @param integer $categoryId The id of the category. * @param string $alias The alias. * @param string $title The title. * * @return array Contains the modified title and alias. */ protected function generateNewTitle($categoryId, $alias, $title) { // Alter the title & alias $table = $this->getTable(); while ($table->load(array('alias' => $alias, 'catid' => $categoryId))) { $title = StringHelper::increment($title); $alias = StringHelper::increment($alias, 'dash'); } return array($title, $alias); }
/** * getSuccessRedirect * * @param DataInterface|Entity $data * * @return string * * @throws \OutOfRangeException */ protected function getSuccessRedirect(DataInterface $data = null) { $data = $data ?: $this->getDataObject(); switch ($this->task) { case 'save2close': return $this->router->route(strtolower($this->config['list_name']), $this->getRedirectQuery()); case 'save2new': return $this->router->route(strtolower($this->getName()), $this->getRedirectQuery(array('new' => ''))); case 'save2copy': $data->{$this->keyName} = null; if ($data->title) { $data->title = StringHelper::increment($data->title); } if ($data->alias) { $data->alias = StringHelper::increment($data->alias, StringHelper::INCREMENT_STYLE_DASH); } $this->setUserState($this->getContext('edit.data'), $data->dump(true)); return $this->router->route(strtolower($this->getName()), $this->getRedirectQuery()); default: $pk = $data->{$this->keyName}; return $this->router->route(strtolower($this->getName()), $this->getRedirectQuery(array($this->keyName => $pk))); } }