public function view() { $owners = []; foreach (Owner::find() as $id => $owner) { if (count($owner['pets']) > 0) { $owners[$id] = $owner; $owners[$id]['active'] = Str::ends_with($_SERVER['REQUEST_URI'], 'pets/' . $owner['_id']); } } $this->set('owners', $owners); }
public function run($fonts = null) { if (\Cli::option('help', \Cli::option('h', false)) or empty($fonts)) { empty($fonts) and \Cli::error(\Cli::color('No font given.', 'red')); return static::help(); } $pdf = new \TCPDF(); $type = \Cli::option('type', \Cli::option('t', '')); $enc = \Cli::option('enc', \Cli::option('e', '')); $flags = \Cli::option('flags', \Cli::option('f', 32)); $outpath = \Cli::option('outpath', \Cli::option('o', K_PATH_FONTS)); $platid = \Cli::option('platid', \Cli::option('p', 3)); $encid = \Cli::option('encid', \Cli::option('n', 1)); $addcbbox = \Cli::option('addcbbox', \Cli::option('b', false)); $link = \Cli::option('link', \Cli::option('l', false)); $path = \Cli::option('path', \Cli::option('p')); if (!is_null($path) and !\Str::ends_with($path, DS)) { $path .= DS; } $fonts = explode(',', $fonts); foreach ($fonts as $font) { if (strpos($font, DS) === false and !is_null($path)) { $font = $path . $font; } $fontfile = realpath($font); $fontname = \TCPDF_FONTS::addTTFfont($fontfile, $type, $enc, $flags, $outpath, $platid, $encid, $addcbbox, $link); if ($fontname == false) { $errors = true; \Cli::error(\Cli::color($font . ' cannot be added.', 'red')); } else { \Cli::write(\Cli::color($font . ' added.', 'green')); } } if (!empty($errors)) { \Cli::error(\Cli::color('Process finished with errors.', 'red')); } else { \Cli::write(\Cli::color('Process successfully finished.', 'green')); } }
$body = call_user_func($callback, $body); } } } $is_truncated = false; if (!$options['is_detail']) { if ($options['line']) { list($body, $is_truncated4line) = Util_string::truncate4line($body, $options['line'], '', $options['is_rtrim'], $options['encoding']); } if ($options['width']) { list($body, $is_truncated4count) = Util_string::truncate($body, $options['width'], '', true); } $is_truncated = $is_truncated4line || $is_truncated4count; } if ($is_truncated && $options['trimmarker']) { if (!Str::ends_with($body, "\n")) { $body .= ' '; } $body .= $options['trimmarker']; } ?> <div><?php echo $body; ?> </div> <?php if ($is_truncated && $options['read_more_uri']) { ?> <div class="bodyMore"><?php echo Html::anchor($options['read_more_uri'], 'もっとみる'); ?>
public static function package($args, $build = true) { $name = str_replace(array('/', '_', '-'), '', \Str::lower(array_shift($args))); $class_name = ucfirst($name); $vcs = \Cli::option('vcs', \Cli::option('v', false)); $path = \Cli::option('path', \Cli::option('p', PKGPATH)); $drivers = \Cli::option('drivers', \Cli::option('d', '')); if (empty($name)) { throw new \Exception('No package name has been provided.'); } if (!in_array($path, \Config::get('package_paths')) and !in_array(realpath($path), \Config::get('package_paths'))) { throw new \Exception('Given path is not a valid package path.'); } \Str::ends_with($path, DS) or $path .= DS; $path .= $name . DS; if (is_dir($path)) { throw new \Exception('Package already exists.'); } if ($vcs) { $output = <<<COMPOSER { \t"name": "fuel/{$name}", \t"type": "fuel-package", \t"description": "{$class_name} package", \t"keywords": [""], \t"homepage": "http://fuelphp.com", \t"license": "MIT", \t"authors": [ \t\t{ \t\t\t"name": "AUTHOR", \t\t\t"email": "*****@*****.**" \t\t} \t], \t"require": { \t\t"composer/installers": "~1.0" \t}, \t"extra": { \t\t"installer-name": "{$name}" \t} } COMPOSER; static::create($path . 'composer.json', $output); $output = <<<README # {$class_name} package Here comes some description README; static::create($path . 'README.md', $output); } if (!empty($drivers)) { $drivers === true or $drivers = explode(',', $drivers); $output = <<<CLASS <?php namespace {$class_name}; class {$class_name}Exception extends \\FuelException {} class {$class_name} { \t/** \t * loaded instance \t */ \tprotected static \$_instance = null; \t/** \t * array of loaded instances \t */ \tprotected static \$_instances = array(); \t/** \t * Default config \t * @var array \t */ \tprotected static \$_defaults = array(); \t/** \t * Init \t */ \tpublic static function _init() \t{ \t\t\\Config::load('{$name}', true); \t} \t/** \t * {$class_name} driver forge. \t * \t * @param\tstring\t\t\t\$instance\t\tInstance name \t * @param\tarray\t\t\t\$config\t\tExtra config array \t * @return {$class_name} instance \t */ \tpublic static function forge(\$instance = 'default', \$config = array()) \t{ \t\tis_array(\$config) or \$config = array('driver' => \$config); \t\t\$config = \\Arr::merge(static::\$_defaults, \\Config::get('{$name}', array()), \$config); \t\t\$class = '\\{$class_name}\\{$class_name}_' . ucfirst(strtolower(\$config['driver'])); \t\tif( ! class_exists(\$class, true)) \t\t{ \t\t\tthrow new \\FuelException('Could not find {$class_name} driver: ' . ucfirst(strtolower(\$config['driver'])); \t\t} \t\t\$driver = new \$class(\$config); \t\tstatic::\$_instances[\$instance] = \$driver; \t\treturn \$driver; \t} \t/** \t * Return a specific driver, or the default instance (is created if necessary) \t * \t * @param string \$instance \t * @return {$class_name} instance \t */ \tpublic static function instance(\$instance = null) \t{ \t\tif (\$instance !== null) \t\t{ \t\t\tif ( ! array_key_exists(\$instance, static::\$_instances)) \t\t\t{ \t\t\t\treturn false; \t\t\t} \t\t\treturn static::\$_instances[\$instance]; \t\t} \t\tif (static::\$_instance === null) \t\t{ \t\t\tstatic::\$_instance = static::forge(); \t\t} \t\treturn static::\$_instance; \t} } CLASS; static::create($path . 'classes' . DS . $name . '.php', $output); $output = <<<DRIVER <?php namespace {$class_name}; abstract class {$class_name}_Driver { \t/** \t* Driver config \t* @var array \t*/ \tprotected \$config = array(); \t/** \t* Driver constructor \t* \t* @param array \$config driver config \t*/ \tpublic function __construct(array \$config = array()) \t{ \t\t\$this->config = \$config; \t} \t/** \t* Get a driver config setting. \t* \t* @param string \$key the config key \t* @param mixed \$default the default value \t* @return mixed the config setting value \t*/ \tpublic function get_config(\$key, \$default = null) \t{ \t\treturn \\Arr::get(\$this->config, \$key, \$default); \t} \t/** \t* Set a driver config setting. \t* \t* @param string \$key the config key \t* @param mixed \$value the new config value \t* @return object \$this for chaining \t*/ \tpublic function set_config(\$key, \$value) \t{ \t\t\\Arr::set(\$this->config, \$key, \$value); \t\treturn \$this; \t} } DRIVER; static::create($path . 'classes' . DS . $name . DS . 'driver.php', $output); $bootstrap = "\n\t'{$class_name}\\\\{$class_name}_Driver' => __DIR__ . '/classes/{$name}/driver.php',"; if (is_array($drivers)) { foreach ($drivers as $driver) { $driver = \Str::lower($driver); $driver_name = ucfirst($driver); $output = <<<CLASS <?php namespace {$class_name}; class {$class_name}_{$driver_name} extends {$class_name}_Driver { \t/** \t* Driver specific functions \t*/ } CLASS; $bootstrap .= "\n\t'{$class_name}\\\\{$class_name}_{$driver_name}' => __DIR__ . '/classes/{$name}/{$driver}.php',"; static::create($path . 'classes' . DS . $name . DS . $driver . '.php', $output); } } } else { $output = <<<CLASS <?php namespace {$class_name}; class {$class_name}Exception extends \\FuelException {} class {$class_name} { \t/** \t * Default config \t * @var array \t */ \tprotected static \$_defaults = array(); \t/** \t* Driver config \t* @var array \t*/ \tprotected \$config = array(); \t/** \t * Init \t */ \tpublic static function _init() \t{ \t\t\\Config::load('{$name}', true); \t} \t/** \t * {$class_name} driver forge. \t * \t * @param\tarray\t\t\t\$config\t\tConfig array \t * @return {$class_name} \t */ \tpublic static function forge(\$config = array()) \t{ \t\t\$config = \\Arr::merge(static::\$_defaults, \\Config::get('{$name}', array()), \$config); \t\t\$class = new static(\$config); \t\treturn \$class; \t} \t/** \t* Driver constructor \t* \t* @param array \$config driver config \t*/ \tpublic function __construct(array \$config = array()) \t{ \t\t\$this->config = \$config; \t} \t/** \t* Get a config setting. \t* \t* @param string \$key the config key \t* @param mixed \$default the default value \t* @return mixed the config setting value \t*/ \tpublic function get_config(\$key, \$default = null) \t{ \t\treturn \\Arr::get(\$this->config, \$key, \$default); \t} \t/** \t* Set a config setting. \t* \t* @param string \$key the config key \t* @param mixed \$value the new config value \t* @return object \$this for chaining \t*/ \tpublic function set_config(\$key, \$value) \t{ \t\t\\Arr::set(\$this->config, \$key, \$value); \t\treturn \$this; \t} } CLASS; static::create($path . 'classes' . DS . $name . '.php', $output); $bootstrap = ""; } $output = <<<CONFIG <?php return array( ); CONFIG; static::create($path . 'config' . DS . $name . '.php', $output); $output = <<<CLASS <?php Autoloader::add_core_namespace('{$class_name}'); Autoloader::add_classes(array( \t'{$class_name}\\\\{$class_name}' => __DIR__ . '/classes/{$name}.php', \t'{$class_name}\\\\{$class_name}Exception' => __DIR__ . '/classes/{$name}.php', {$bootstrap} )); CLASS; static::create($path . 'bootstrap.php', $output); $build and static::build(); }
public static function truncate4line($body, $line, $trimmarker = '...', $is_rtrim = true, $encoding = null) { $encoding or $encoding = \Fuel::$encoding; $is_truncated = false; if (!$line) { return array($body, $is_truncated); } if (!($pos = Util_string::mb_strpos_n($body, "\n", $line, $encoding))) { return array($body, $is_truncated); } $is_truncated = $pos < mb_strlen($body, $encoding); $body = mb_substr($body, 0, $pos, $encoding); if ($is_truncated) { if ($is_rtrim) { $body = rtrim($body); } if ($trimmarker) { if (!Str::ends_with($body, "\n")) { $body .= ' '; } $body .= $trimmarker; } } return array($body, $is_truncated); }
/** * Test for Str::ends_with() * * @test */ public function test_ends_with() { $string = 'HELLO WORLD'; $output = Str::ends_with($string, 'WORLD'); $this->assertTrue($output); $output = Str::ends_with($string, 'world'); $this->assertFalse($output); $output = Str::ends_with($string, 'world', true); $this->assertTrue($output); }
protected static function process_migration($scaffold, $module, $module_path) { /* * Migration files are a special case because the * original and destination file names are not * constant. Depending on the existing migrations * files, we could have to move the * 003_create_category.php migration file from the * application to the 002_create_category.php * migration file in the module. * Moreover, we don't need to do any replacements * here. */ $app_migrations_path = APPPATH . 'migrations'; $module_migrations_path = $module_path . 'migrations'; // Getting the list of migrations files in the // application. $app_migrations = \File::read_dir($app_migrations_path); // If the migration directory doesn't exists in the // module, we create it. if (!file_exists($module_migrations_path)) { mkdir($module_migrations_path, static::$folder_permissions, true); } $migration_filename_end = '_create_' . \Inflector::pluralize($scaffold) . '.php'; $module_migrations = \File::read_dir($module_migrations_path); // Searching if the migration file already exists $module_migration_filename = null; $already_exists = false; foreach ($module_migrations as $module_migration) { if (\Str::ends_with($module_migration, $migration_filename_end)) { $module_migration_filename = $module_migration; $already_exists = true; break; } } // If it doesn't, the migration file name generated if (is_null($module_migration_filename)) { // Finding the destination migration file name. $nb_module_migrations = count($module_migrations); $module_migration_number = str_pad($nb_module_migrations + 1, 3, '0', STR_PAD_LEFT); $module_migration_filename = $module_migration_number . $migration_filename_end; } // Finding the original migration file foreach ($app_migrations as $app_migration) { if (\Str::ends_with($app_migration, $migration_filename_end)) { // Moving it to the destination file. $migration_from = $app_migrations_path . DS . $app_migration; $migration_to = $module_migrations_path . DS . $module_migration_filename; /* * If it already exists, the migration is * not overriden except if the force option * is set. */ if (!$already_exists || static::$force) { \Cli::write("\tCreating migration: " . $migration_to, 'green'); file_put_contents($migration_to, file_get_contents($migration_from)); } \Cli::write("\tDeleting migration: " . $migration_from, 'green'); \File::delete($migration_from); break; } } }
/** * Add file to zip * * @param string $filename * @param string|null $localname Where will be added to the file in the zip * * @return bool */ private function addFileToZip($filename, $localname = null) { if ($localname === null) { $localname . basename($filename); } if (empty($localname)) { $localname = basename($filename); } elseif (!empty($localname) && !\Str::ends_with($localname, '/')) { $localname .= '/' . basename($filename); } $result = $this->_zip_archive->addFile($filename, $localname); return $result; }