/** * Initialize method */ public function initialize() { $paths = App::path('Shell/Task', 'CodeBlastrQueue'); foreach ($paths as $path) { $Folder = new Folder($path); $res = array_merge($this->tasks, $Folder->find('Queue.+\\.php')); foreach ($res as &$r) { $r = 'CodeBlastrQueue.' . basename($r, 'Task.php'); } $this->tasks = $res; } parent::initialize(); }
/** * Overwrite shell initialize to dynamically load all Queue Related Tasks. * * @return void */ public function initialize() { $plugins = Plugin::loaded(); foreach ($plugins as $plugin) { $pluginPaths = App::path('Shell/Task', $plugin); foreach ($pluginPaths as $pluginPath) { $Folder = new Folder($pluginPath); $res = $Folder->find('Queue.+Task\\.php'); foreach ($res as &$r) { $r = $plugin . '.' . basename($r, 'Task.php'); } $this->tasks = array_merge($this->tasks, $res); } } $paths = App::path('Shell/Task'); foreach ($paths as $path) { $Folder = new Folder($path); $res = array_merge($this->tasks, $Folder->find('Queue.+\\.php')); foreach ($res as &$r) { $r = basename($r, 'Task.php'); } $this->tasks = $res; } parent::initialize(); $this->QueuedTasks->initConfig(); }
/** * Loads all fixtures from disk if none defined. * * @param \Cake\TestSuite\TestCase $test The test case to inspect. * @return void */ public function fixturize($test) { if (!isset($test->fixtures)) { $folder = new Folder($this->_fixturePath); $fixtureFiles = $folder->find('.*Fixture\\.php'); foreach ($fixtureFiles as $fixtureFile) { $fixtureName = str_replace('Fixture.php', '', $fixtureFile); $fixtureName = Inflector::underscore($fixtureName); $test->fixtures[] = 'app.' . $fixtureName; } } parent::fixturize($test); }
/** * Initialize a single module instance. * * @param string $modulePath */ protected static function _initializeModule($modulePath) { $moduleFolder = new Folder($modulePath); $moduleName = basename($moduleFolder->path); $moduleClassFilename = $moduleName . 'Module.php'; $moduleClassFile = $moduleFolder->find($moduleClassFilename); if (empty($moduleClassFile)) { user_error(__d('wasabi_cms', 'Module {0} has no associated class file {1} in {3}.', $moduleName, $moduleClassFilename, $modulePath)); } $moduleClassFile = $moduleClassFile[0]; $moduleNamespace = self::_extractNamespace($modulePath . DS . $moduleClassFile); if (!$moduleNamespace) { user_error(__d('wasabi_cms', 'Module {3} has no namespace.')); } $class = $moduleNamespace . '\\' . $moduleName . 'Module'; self::$_modules[$class] = new $class(); }
public function import() { $settingsFile = 'settings'; if (!empty($this->plugin)) { $settingsFile = $this->plugin . '.' . $settingsFile; $settingsFileDir = Plugin::path($this->plugin) . 'config'; } else { $settingsFileDir = ROOT . DS . 'config'; } $settingsDir = new Folder($settingsFileDir, false); if (!$settingsDir->find('settings.*')) { return false; } $config = new PhpConfig(); $data = $config->read($settingsFile); if (!$data) { return false; } if (!empty($this->plugin)) { $this->out(__d('platform', '- <success>Processing settings pool for {0} plugin</success>', $this->plugin)); } else { $this->out(__d('platform', '- <success>Processing settings pool for {0}</success>', 'App')); } $settingsTable = TableRegistry::get('Platform.Settings'); foreach ($data as $row) { if (!isset($row['plugin']) || empty($row['plugin'])) { $row['plugin'] = $this->plugin; } $data = ['plugin' => $row['plugin'], 'path' => $row['path']]; $setting = $settingsTable->find('all', ['conditions' => $data])->first(); if (!$setting) { $setting = $settingsTable->newEntity(); $data = array_merge($data, ['value' => isset($row['default']) ? $row['default'] : '']); } $setting = $settingsTable->patchEntity($setting, $data); $settingsTable->save($setting); } //TODO: Maybe chared method for settings save $settings = $settingsTable->find()->combine('path', 'value')->toArray(); ksort($settings); $settings = Hash::expand($settings); Settings::dump('config', 'default', $settings); }
/** * Returns a list of Controller names found in /src/Controller/Api. * * @return array List holding sorted Controller names */ public static function getApiControllers() { $cached = Cache::read('api_controllers'); if ($cached) { return $cached; } $dir = new Folder(APP . 'Controller' . DS . Inflector::camelize(Configure::read('App++.Api.prefix'))); $controllerFiles = $dir->find('.*Controller\\.php'); if (!$controllerFiles) { return false; } $result = []; foreach ($controllerFiles as $controllerFile) { $result[] = substr($controllerFile, 0, strlen($controllerFile) - 14); } sort($result); Cache::write('api_controllers', $result); return $result; }
private function deleteFile(Attachment $attachment, $recursive = false) { $return = true; $dir = new Folder(Configure::read('Upload.path') . $attachment->get('file_path')); if ($recursive) { $files = $dir->findRecursive($attachment->get('file_name') . '.*', true); } else { $files = $dir->find($attachment->get('file_name') . '.*'); } foreach ($files as $file) { $fileTmp = new File($file); if ($fileTmp->exists()) { if (!$fileTmp->delete()) { $return = false; } } else { $return = false; } } return $return; }
public function ajaxDeleteImage($id = null) { $this->autoRender = false; $ImagesTable = TableRegistry::get('Images'); $image = $ImagesTable->get($id); $shared = $ImagesTable->find()->where(['id !=' => $image->id, 'field_index !=' => $image->field_index, 'model' => $image->model, 'filename' => $image->filename]); if (!$shared->count()) { $basePath = WWW_ROOT . 'uploads/images/' . $image->model; $dir = new Folder($basePath); $files = $dir->find('.*_.*\\..*'); (new File($basePath . DS . $image->filename))->delete(); foreach ($files as $file) { if (preg_match("/.*_{$image->filename}/", $file)) { (new File($basePath . DS . $file))->delete(); } } } if (!$ImagesTable->delete($image)) { throw new InternalErrorException(); } if (!$this->request->is('ajax')) { $this->redirect($this->referer()); } }
/** * This function provides the correct filename for the creation of file systems. * * @param type $directory * @param type $filename * @return string */ public function getPermittedFilename($directory, $filename) { $pathInfo = pathinfo($filename); $pathInfo['filename'] = substr(Inflector::slug($pathInfo['filename']), 0, 100); if (strlen($pathInfo['filename']) < 3) { $pathInfo['filename'] = $this->_randomString(); } $dir = new Folder($directory); $iter = 0; do { if ($iter == 0) { $slugFileName = $pathInfo['filename'] . '.' . $pathInfo['extension']; } else { $slugFileName = $pathInfo['filename'] . '-' . $iter . '.' . $pathInfo['extension']; } $data = $dir->find($slugFileName, true); $iter++; } while (count($data) > 0); return $slugFileName; }
/** * testFind method * * @return void */ public function testFind() { $Folder = new Folder(); $Folder->cd(CORE_PATH . 'config'); $result = $Folder->find(); $expected = ['config.php']; $this->assertSame(array_diff($expected, $result), []); $this->assertSame(array_diff($expected, $result), []); $result = $Folder->find('.*', true); $expected = ['bootstrap.php', 'cacert.pem', 'config.php']; $this->assertSame($expected, $result); $result = $Folder->find('.*\\.php'); $expected = ['bootstrap.php', 'config.php']; $this->assertSame(array_diff($expected, $result), []); $this->assertSame(array_diff($expected, $result), []); $result = $Folder->find('.*\\.php', true); $expected = ['bootstrap.php', 'config.php']; $this->assertSame($expected, $result); $result = $Folder->find('.*ig\\.php'); $expected = ['config.php']; $this->assertSame($expected, $result); $result = $Folder->find('config\\.php'); $expected = ['config.php']; $this->assertSame($expected, $result); $Folder = new Folder(TMP . 'tests/', true); $File = new File($Folder->pwd() . DS . 'paths.php', true); $Folder->create($Folder->pwd() . DS . 'testme'); $Folder->cd('testme'); $result = $Folder->find('paths\\.php'); $expected = []; $this->assertSame($expected, $result); $Folder->cd($Folder->pwd() . '/..'); $result = $Folder->find('paths\\.php'); $expected = ['paths.php']; $this->assertSame($expected, $result); }
/** * Path to migration folder. * * @param $path * @return array */ public static function migrationData($path) { $data = []; $dir = new Folder($path); $files = $dir->find('.*\\.php'); if (!empty($files)) { foreach ($files as $file) { $_fileName = str_replace('.php', '', $file); $segments = explode('_', $_fileName); $version = $segments[0]; unset($segments[0]); $class = Inflector::camelize(implode('_', $segments)); $data[$version] = ['class' => $class, 'path' => $path . DS . $file]; } } return $data; }
/** * Loads the configuration files and populates the array * * @return void */ public function loadConfig() { // Load Yaml lib if (empty($this->_Spyc)) { $this->_Spyc = new Spyc(); } $template = Configure::read('Sb.template'); // Fetch the correct template $this->log("\"{$template}\" template is being used.", 'info', 1); $this->_template = $template; // Search for config files $configDir = $this->getConfigPath($template); $this->log("Template dir is '{$configDir}'.", 'info'); $dir = new Folder($configDir); $configFiles = $dir->find('(?!_).*\\.yml'); // Load config files foreach ($configFiles as $file) { // Opening file and converting YAML content to PHP array $this->log("Loading file: \"{$file}\"...", 'info', 1); $this->_config = array_merge_recursive($this->_config, $this->_Spyc->YAMLLoad($configDir . $file)); } $this->populate(); }
/** * Return an simple array with all Nginx site files found in /etc/nginx/sites-available. * * @return array Simple array with found filenames */ public function getNginxFiles() { $dir = new Folder($this->webserverMeta['nginx']['sites-available']); return $dir->find('.*', 'sort'); }
/** * Retrieve all controller names + paths for the app src. * * @return array */ public function getControllersForApp() { $controllers = []; $ctrlFolder = new Folder(); /** @var Folder $ctrlFolder */ $ctrlFolder->cd(ROOT . DS . 'src' . DS . 'Controller'); if (!empty($ctrlFolder)) { $files = $ctrlFolder->find('.*Controller\\.php$'); $subLength = strlen('Controller.php'); foreach ($files as $f) { $filename = basename($f); if ($filename === 'AppController.php') { continue; } $ctrlName = substr($filename, 0, strlen($filename) - $subLength); $controllers[] = ['name' => $ctrlName, 'path' => $ctrlFolder->path . DS . $f]; } $subFolders = $ctrlFolder->read(true, false, true)[0]; foreach ($subFolders as $subFolder) { $ctrlFolder->cd($subFolder); $files = $ctrlFolder->find('.*Controller\\.php$'); $subLength = strlen('Controller.php'); foreach ($files as $f) { $filename = basename($f); if ($filename === 'AppController.php') { continue; } $ctrlName = substr($filename, 0, strlen($filename) - $subLength); $controllers[] = ['name' => $ctrlName, 'path' => $ctrlFolder->path . DS . $f]; } } } return $controllers; }
/** * Includes the needed components * * @return void */ protected function _includeComponents() { // for now, we just include all components $appComponentFolder = WWW_ROOT . 'js/app/components/'; $folder = new Folder($appComponentFolder); $files = $folder->find('.*\\.js'); if (!empty($files)) { foreach ($files as $file) { $this->_dependencies[] = 'app/components/' . $file; } } // Add Plugin Components foreach (Plugin::loaded() as $pluginName) { $pluginJsComponentsFolder = Plugin::path($pluginName) . '/webroot/js/app/components/'; if (is_dir($pluginJsComponentsFolder)) { $folder = new Folder($pluginJsComponentsFolder); $files = $folder->find('.*\\.js'); foreach ($files as $file) { $this->_dependencies[] = '/' . Inflector::underscore($pluginName) . '/js/app/components/' . $file; } } } }
public function reset() { $this->out(__d('platform', '<warning>Start app database downgrading</warning>')); $this->hr(); $migrationsDir = new Folder(ROOT . DS . 'config' . DS . 'Migrations', false); $migrationFiles = $migrationsDir->find('.*\\.php'); if (count($migrationFiles) > 0) { $this->runMigrations(); $this->out(__d('platform', '- <success>App core database downgraded</success>')); } foreach (Plugin::loaded() as $plugin) { $migrationsDir = new Folder(Plugin::path($plugin) . 'config' . DS . 'Migrations', false); $migrationFiles = $migrationsDir->find('.*\\.php'); if (count($migrationFiles) > 0) { $this->runMigrations('rollback', $plugin); $this->out(__d('platform', '- <success>{0} plugin migrations downgraded</success>', $plugin)); } } $this->hr(); if (file_exists(ROOT . DS . 'config' . DS . 'config.php')) { unlink(ROOT . DS . 'config' . DS . 'config.php'); } }
/** * Get a list of controllers in the app and plugins. * * Returns an array of path => import notation. * * @param string $plugin Name of plugin to get controllers for * @param string $prefix Name of prefix to get controllers for * @return array */ public function getControllerList($plugin = null, $prefix = null) { if (!$plugin) { $path = App::path('Controller' . (empty($prefix) ? '' : DS . Inflector::camelize($prefix))); $dir = new Folder($path[0]); $controllers = $dir->find('.*Controller\\.php'); } else { $path = App::path('Controller' . (empty($prefix) ? '' : DS . Inflector::camelize($prefix)), $plugin); $dir = new Folder($path[0]); $controllers = $dir->find('.*Controller\\.php'); } return $controllers; }
/** * Delete image's thumbnails if exists. * * @param string $imagePath Full path to original image file * @return void */ public static function deleteThumbnails($imagePath) { $imagePath = normalizePath("{$imagePath}/"); $fileName = basename($imagePath); $tmbPath = normalizePath(dirname($imagePath) . '/.tmb/'); $folder = new Folder($tmbPath); $pattern = preg_quote(static::removeExt($fileName)); foreach ($folder->find(".*{$pattern}.*") as $tn) { $tn = new File($tmbPath . $tn); $tn->delete(); } }
/** * Process the tasks when they need to run * * @access private * @return void */ private function runjobs() { $dir = new Folder(TMP); // set processing flag so function takes place only once at any given time $processing = count($dir->find('\\.scheduler_running_flag')); $processingFlag = new File($dir->slashTerm($dir->pwd()) . '.scheduler_running_flag'); if ($processing && time() - $processingFlag->lastChange() < $this->processingTimeout) { $this->out("Scheduler already running! Exiting."); return false; } else { $processingFlag->delete(); $processingFlag->create(); } if (!$this->storePath) { $this->storePath = TMP; } // look for a store of the previous run $store = ""; $storeFilePath = $this->storePath . $this->storeFile; if (file_exists($storeFilePath)) { $store = file_get_contents($storeFilePath); } $this->out('Reading from: ' . $storeFilePath); // build or rebuild the store if ($store != '') { $store = json_decode($store, true); } else { $store = $this->schedule; } // run the jobs that need to be run, record the time foreach ($this->schedule as $name => $job) { $now = new DateTime(); $task = $job['task']; $action = $job['action']; // if the job has never been run before, create it if (!isset($store[$name])) { $store[$name] = $job; } // figure out the last run date $tmptime = $store[$name]['lastRun']; if ($tmptime == null) { $tmptime = new DateTime("1969-01-01 00:00:00"); } elseif (is_array($tmptime)) { $tmptime = new DateTime($tmptime['date'], new DateTimeZone($tmptime['timezone'])); } elseif (is_string($tmptime)) { $tmptime = new DateTime($tmptime); } // determine the next run time based on the last if (substr($job['interval'], 0, 1) === 'P') { $tmptime->add(new DateInterval($job['interval'])); // "P10DT4H" http://www.php.net/manual/en/class.dateinterval.php } else { $tmptime->modify($job['interval']); // "next day 10:30" http://www.php.net/manual/en/datetime.formats.relative.php } // is it time to run? has it never been run before? if ($tmptime <= $now) { $this->hr(); $this->out("Running {$name}"); $this->hr(); if (!isset($this->{$task})) { $this->{$task} = $this->Tasks->load($task); // load models if they aren't already // foreach ($this->$task->uses as $mk => $mv) { // if (!isset($this->$task->$mv)) { // App::uses('AppModel', 'Model'); // App::uses($mv, 'Model'); // $this->$task->$mv = new $mv(); // } // } } // grab the entire schedule record incase it was updated.. $store[$name] = $this->schedule[$name]; // execute the task and store the result $store[$name]['lastResult'] = call_user_func_array(array($this->{$task}, $action), $job['args']); // assign it the current time $now = new DateTime(); $store[$name]['lastRun'] = $now->format('Y-m-d H:i:s'); } } // write the store back to the file file_put_contents($this->storePath . $this->storeFile, json_encode($store)); // remove processing flag $processingFlag->delete(); }
/** * _removeExpiredLock * * @return void */ protected function _removeExpiredLock() { $dir = new Folder(CONFIG . $this->_config['lockout']['file_path'], true); $files = $dir->find(); foreach ($files as $fileName) { $file = new File($dir->pwd() . DS . $fileName); $lastChange = Time::createFromTimestamp($file->lastChange()); if ($lastChange->wasWithinLast($this->_config['lockout']['expires'])) { continue; } $file->delete(); } }
/** * Find and get module styles. * * @return array */ public function getStyles() { $theme = activeTheme(); $stylesDir = $this->_stylesPath($this->_plugin); $defaultFolder = new Folder($stylesDir); $defaultStyles = $defaultFolder->find('.*\\.ctp'); $themeStylesDir = $this->_stylesPath($theme); $themeFolder = new Folder($themeStylesDir); $themeStyles = $themeFolder->find('.*\\.ctp'); $output = []; $styles = array_replace($defaultStyles, $themeStyles); foreach ($styles as $style) { $name = pathinfo($style, PATHINFO_FILENAME); $output[$name] = Inflector::camelize($name); } return $output; }
/** * Verify that the order using name is correct. */ public function testSortByName() { $Folder = new Folder(TMP . 'tests', true); $fileA = new File($Folder->pwd() . DS . 'a.txt'); $fileA->create(); $fileC = new File($Folder->pwd() . DS . 'c.txt'); $fileC->create(); sleep(1); $fileB = new File($Folder->pwd() . DS . 'b.txt'); $fileB->create(); $results = $Folder->find('.*', Folder::SORT_NAME); $this->assertSame(['a.txt', 'b.txt', 'c.txt'], $results); }