/** * @return int|null */ public function clean() { if (!empty($this->args[0])) { $folder = realpath($this->args[0]); } else { $folder = APP; } $App = new Folder($folder); $this->out('Cleaning copyright notices in ' . $folder); $ext = '.*'; if (!empty($this->params['ext'])) { $ext = $this->params['ext']; } $files = $App->findRecursive('.*\\.' . $ext); $this->out('Found ' . count($files) . ' files.'); $count = 0; foreach ($files as $file) { $this->out('Processing ' . $file, 1, Shell::VERBOSE); $content = $original = file_get_contents($file); $content = preg_replace('/\\<\\?php\\s*\\s+\\/\\*\\*\\s*\\s+\\* CakePHP.*\\*\\//msUi', '<?php', $content); if ($content === $original) { continue; } $count++; if (empty($this->params['dry-run'])) { file_put_contents($file, $content); } } $this->out('--------'); $this->out($count . ' files fixed.'); }
public function getTemplatesList($dir) { $basicDir = $dir . $this->subDir; $dir = new Folder($basicDir); $templatesList = array_map(function ($dirAddr) use($basicDir) { return str_replace($basicDir . '/', '', $dirAddr); }, $dir->findRecursive('.*\\.html')); return array_combine($templatesList, $templatesList); }
/** * startCaching method * * Cache\[optimize] images in cache folder * * @param string $optimization 'true' or 'false' * @param string $srcPath folder name for source images * @return void */ public function startCaching($optimization = 'true', $srcPath = 'src_images') { $dir = new Folder(WWW_ROOT . 'img' . DS . $srcPath); $files = $dir->findRecursive('.*\\.(jpg|jpeg|png|gif|svg)'); /* * Error handler */ if (is_null($dir->path)) { $this->error('<red_text>Source folder not exists!</red_text>'); } if ($optimization != 'true' && $optimization != 'false') { $this->error('<red_text>Arguments \'optimization\' should equal \'true\' or \'false\'</red_text>'); } /* * Caching */ $counter = 1; $countFiles = count($files); $this->out('<info>Images caching</info>'); foreach ($files as $file) { $file = new File($file); $semanticType = explode(DS, $file->Folder()->path); $semanticType = $semanticType[count($semanticType) - 1]; //get semantic type name $this->adaptiveImagesController->passiveCaching($file->path, $semanticType); $this->_io->overwrite($this->progressBar($counter, $countFiles), 0, 50); $counter++; } /* * Optimization */ if ($optimization == 'true') { $cachePath = $this->adaptiveImagesController->getCachePath(); $pluginPath = Plugin::path('AdaptiveImages'); $cacheDir = new Folder($pluginPath . 'webroot' . DS . $cachePath); $files = $cacheDir->findRecursive('.*\\.(jpg|jpeg|png|gif|svg)'); $counter = 1; $countFiles = count($files); $this->out(''); $this->out('<info>Images optimization</info>'); foreach ($files as $file) { $this->_optimizeImage($file); $this->_io->overwrite($this->progressBar($counter, $countFiles), 0, 50); $counter++; } $this->hr(); $this->out('<green_text>Caching and optimization completed!</green_text>'); } elseif ($optimization == 'false') { $this->hr(); $this->out('<green_text>Caching completed!</green_text>'); } }
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; }
/** * Much like main() except files are modified. Be sure to have * backups or use version control. * * @return void */ public function trim() { $path = APP; if (!empty($this->params['path']) && strpos($this->params['path'], '/') === 0) { $path = $this->params['path']; } elseif (!empty($this->params['path'])) { $path .= $this->params['path']; } $folder = new Folder($path); $r = $folder->findRecursive('.*\\.php'); $this->out("Checking *.php in " . $path); foreach ($r as $file) { $c = file_get_contents($file); if (preg_match('/^[\\n\\r|\\n\\r|\\n|\\r|\\s]+\\<\\?php/', $c) || preg_match('/\\?\\>[\\n\\r|\\n\\r|\\n|\\r|\\s]+$/', $c)) { $this->out('trimming' . $this->shortPath($file)); $c = preg_replace('/^[\\n\\r|\\n\\r|\\n|\\r|\\s]+\\<\\?php/', '<?php', $c); $c = preg_replace('/\\?\\>[\\n\\r|\\n\\r|\\n|\\r|\\s]+$/', '?>', $c); file_put_contents($file, $c); } } }
/** * main method * * @param string $tempDir an other directory to clear of all folders and files, if desired * @return void */ public function main($tempDir = null) { if (empty($tempDir)) { $tempDir = Configure::read('Attachments.tmpUploadsPath'); } if (!Folder::isAbsolute($tempDir)) { $this->out('The path must be absolute, "' . $tempDir . '" given.'); exit; } $Folder = new Folder(); if ($Folder->cd($tempDir) === false) { $this->out('Path "' . $tempDir . '" doesn\'t seem to exist.'); exit; } $dir = new Folder($tempDir); $folders = $dir->read(); $files = $dir->findRecursive(); $deletedFiles = 0; $deletedFolders = 0; $this->out('Found ' . count($folders[0]) . ' folders and ' . count($files) . ' files'); foreach ($files as $filePath) { $file = new File($filePath); // only delete if last change is longer than 24 hours ago if ($file->lastChange() < time() - 24 * 60 * 60 && $file->delete()) { $deletedFiles++; } $file->close(); } foreach ($folders[0] as $folderName) { $folder = new Folder($dir->pwd() . $folderName); // only delete if folder is empty if ($folder->dirsize() === 0 && $folder->delete()) { $deletedFolders++; } } $this->out('Deleted ' . $deletedFolders . ' folders and ' . $deletedFiles . ' files.'); }
/** * Generate all files for a plugin * * Find the first path which contains `src/Template/Bake/Plugin` that contains * something, and use that as the template to recursively render a plugin's * contents. Allows the creation of a bake them containing a `Plugin` folder * to provide customized bake output for plugins. * * @param string $pluginName the CamelCase name of the plugin * @param string $path the path to the plugins dir (the containing folder) * @return void */ protected function _generateFiles($pluginName, $path) { $namespace = str_replace('/', '\\', $pluginName); $name = $pluginName; $vendor = 'your-name-here'; if (strpos($pluginName, '/') !== false) { list($vendor, $name) = explode('/', $pluginName); } $package = $vendor . '/' . $name; $this->BakeTemplate->set(['package' => $package, 'namespace' => $namespace, 'plugin' => $pluginName, 'routePath' => Inflector::dasherize($pluginName), 'path' => $path, 'root' => ROOT]); $root = $path . $pluginName . DS; $paths = []; if (!empty($this->params['theme'])) { $paths[] = Plugin::path($this->params['theme']) . 'src/Template/'; } $paths = array_merge($paths, Configure::read('App.paths.templates')); $paths[] = Plugin::path('Bake') . 'src/Template/'; do { $templatesPath = array_shift($paths) . 'Bake/Plugin'; $templatesDir = new Folder($templatesPath); $templates = $templatesDir->findRecursive('.*\\.ctp'); } while (!$templates); sort($templates); foreach ($templates as $template) { $template = substr($template, strrpos($template, 'Plugin') + 7, -4); $this->_generateFile($template, $root); } }
/** * Search files that may contain translatable strings * * @return void */ protected function _searchFiles() { $pattern = false; if (!empty($this->_exclude)) { $exclude = []; foreach ($this->_exclude as $e) { if (DIRECTORY_SEPARATOR !== '\\' && $e[0] !== DIRECTORY_SEPARATOR) { $e = DIRECTORY_SEPARATOR . $e; } $exclude[] = preg_quote($e, '/'); } $pattern = '/' . implode('|', $exclude) . '/'; } foreach ($this->_paths as $path) { $path = realpath($path) . DIRECTORY_SEPARATOR; $Folder = new Folder($path); $files = $Folder->findRecursive('.*\\.(php|ctp|thtml|inc|tpl)', true); if (!empty($pattern)) { $files = preg_grep($pattern, $files, PREG_GREP_INVERT); $files = array_values($files); } $this->_files = array_merge($this->_files, $files); } $this->_files = array_unique($this->_files); }
/** * This method should never be used unless you know what are you doing. * * Populates the "acos" DB with information of every installed plugin, or * for the given plugin. It will automatically extracts plugin's controllers * and actions for creating a tree structure as follow: * * - PluginName * - Admin * - PrivateController * - index * - some_action * - ControllerName * - index * - another_action * * After tree is created you should be able to change permissions using * User's permissions section in backend. * * @param string $for Optional, build ACOs for the given plugin, or all plugins * if not given * @param bool $sync Whether to sync the tree or not. When syncing all invalid * ACO entries will be removed from the tree, also new ones will be added. When * syn is set to false only new ACO entries will be added, any invalid entry * will remain in the tree. Defaults to false * @return bool True on success, false otherwise */ public static function buildAcos($for = null, $sync = false) { if (function_exists('ini_set')) { ini_set('max_execution_time', 300); } elseif (function_exists('set_time_limit')) { set_time_limit(300); } if ($for === null) { $plugins = plugin()->toArray(); } else { try { $plugins = [plugin($for)]; } catch (\Exception $e) { return false; } } $added = []; foreach ($plugins as $plugin) { if (!Plugin::exists($plugin->name)) { continue; } $aco = new AcoManager($plugin->name); $controllerDir = normalizePath("{$plugin->path}/src/Controller/"); $folder = new Folder($controllerDir); $controllers = $folder->findRecursive('.*Controller\\.php'); foreach ($controllers as $controller) { $controller = str_replace([$controllerDir, '.php'], '', $controller); $className = $plugin->name . '\\' . 'Controller\\' . str_replace(DS, '\\', $controller); $methods = static::_controllerMethods($className); if (!empty($methods)) { $path = explode('Controller\\', $className)[1]; $path = str_replace_last('Controller', '', $path); $path = str_replace('\\', '/', $path); foreach ($methods as $method) { if ($aco->add("{$path}/{$method}")) { $added[] = "{$plugin->name}/{$path}/{$method}"; } } } } } if ($sync && isset($aco)) { $aco->Acos->recover(); $existingPaths = static::paths($for); foreach ($existingPaths as $exists) { if (!in_array($exists, $added)) { $aco->remove($exists); } } $validLeafs = $aco->Acos->find()->select(['id'])->where(['id NOT IN' => $aco->Acos->find()->select(['parent_id'])->where(['parent_id IS NOT' => null])]); $aco->Acos->Permissions->deleteAll(['aco_id NOT IN' => $validLeafs]); } return true; }
private function listAvailibleTasksFiles() { $dir = new Folder(ROOT . '/scripts'); $files = $dir->findRecursive('.*\\.kjb'); return array_combine($files, $files); }
/** * Get all controllers only from "Controller path only" * TO DO: Implements Plugin path * * @return array return a list of all controllers */ private function __getControllers() { $path = App::path('Controller'); $dir = new Folder($path[0]); $files = $dir->findRecursive('.*Controller\\.php'); $results = []; foreach ($files as $file) { $controller = str_replace(App::path('Controller'), '', $file); $controller = explode('.', $controller)[0]; $controller = str_replace('Controller', '', $controller); array_push($results, $controller); } return $results; }
/** * Get all controllers in Plugins only from "Controller path only" * TO DO: Implements Plugin path * * @return array return a list of all controllers */ private function __getPluginsControllers() { $path = App::path('Controller'); $plugins = Plugin::loaded(); $dir = new Folder(ROOT . DS . "Plugins" . DS); $files = $dir->findRecursive('.*Controller\\.php'); $results = []; foreach ($files as $file) { $controller = str_replace(ROOT . DS . "Plugins" . DS, '', $file); $controller = explode('.', $controller)[0]; $controller = str_replace('Controller', '', $controller); $controller = str_replace('src' . DS . DS, '', $controller); array_push($results, $controller); } return $results; }
/** * Whitespaces at the end of the file * If PHP file with trailing ?> that will be removed as per coding standards. * * @return void */ public function eof() { if (!empty($this->args[0])) { $folder = realpath($this->args[0]); } else { $folder = APP; } $App = new Folder($folder); $this->out('Checking *.php in ' . $folder); $files = $App->findRecursive('.*\\.php'); $this->out('Found ' . count($files) . ' files.'); $action = $this->in('Continue? [y]/[n]', ['y', 'n'], 'n'); if ($action !== 'y') { return $this->error('Aborted'); } foreach ($files as $file) { $this->out('Processing ' . $file, 1, Shell::VERBOSE); $content = $store = file_get_contents($file); $content = trim($content); $ext = pathinfo($file, PATHINFO_EXTENSION); if ($ext === 'php' && substr($content, -2, 2) === '?>') { $content = substr($content, 0, -2); } $newline = PHP_EOL; $x = substr_count($content, "\r\n"); if ($x > 0) { $newline = "\r\n"; } else { $newline = "\n"; } // add one new line at the end $content = trim($content) . $newline; if ($content !== $store) { file_put_contents($file, $content); } } $this->out('Done'); }
/** * Adds all controllers in app/controllers to the dependencies. * * Please use only in development mode! * * @return void */ public function addAllControllers() { $controllers = []; // app/controllers/posts/*_controller.js $folder = new \Cake\Filesystem\Folder(WWW_ROOT . 'js/app/controllers'); foreach ($folder->findRecursive('.*\\.js') as $file) { $jsFile = '/' . str_replace(WWW_ROOT, '', $file); $controllers[] = $jsFile; } // Add All Plugin Controllers foreach (Plugin::loaded() as $pluginName) { $pluginPath = Plugin::path($pluginName); $pluginJsControllersFolder = $pluginPath . (substr($pluginPath, -1) === '/' ? '' : '/') . 'webroot/js/app/controllers/'; $pluginJsControllersFolder = str_replace('\\', '/', $pluginJsControllersFolder); if (is_dir($pluginJsControllersFolder)) { $this->_pluginJsNamespaces[] = $pluginName; $folder = new \Cake\Filesystem\Folder($pluginJsControllersFolder); $files = $folder->findRecursive('.*\\.js'); foreach ($files as $file) { $file = str_replace('\\', '/', $file); $file = str_replace($pluginJsControllersFolder, '', $file); if ($this->_assetCompressMode) { $controllers[] = 'plugin:' . $pluginName . ':js/app/controllers/' . $file; } else { $controllers[] = '/' . Inflector::underscore($pluginName) . '/js/app/controllers/' . $file; } } } } // Move all controllers with base_ prefix to the top, so other controllers // can inherit from them foreach ($controllers as $n => $file) { if (substr(basename($file), 0, 5) == 'base_') { unset($controllers[$n]); array_unshift($controllers, $file); } } foreach ($controllers as $file) { $this->_addDependency($file); } }
/** * Check the baked plugin matches the expected output * * Compare to a static copy of the plugin in the comparison folder * * @param string $pluginName the name of the plugin to compare to * @return void */ public function assertPluginContents($pluginName) { $pluginName = str_replace('/', DS, $pluginName); $comparisonRoot = $this->_compareBasePath . $pluginName . DS; $comparisonDir = new Folder($comparisonRoot); $comparisonFiles = $comparisonDir->findRecursive(); $bakedRoot = $this->Task->path . $pluginName . DS; $bakedDir = new Folder($bakedRoot); $bakedFiles = $comparisonDir->findRecursive(); $this->assertSame(count($comparisonFiles), count($bakedFiles), 'A different number of files were created than expected'); foreach ($comparisonFiles as $file) { $file = substr($file, strlen($comparisonRoot)); $result = file_get_contents($bakedRoot . $file); $this->assertSameAsFile($pluginName . DS . $file, $result); } }
/** * testFindRecursive method * * @return void */ public function testFindRecursive() { $Folder = new Folder(CORE_PATH); $result = $Folder->findRecursive('(config|paths)\\.php'); $expected = [CORE_PATH . 'config' . DS . 'config.php']; $this->assertSame([], array_diff($expected, $result)); $this->assertSame([], array_diff($expected, $result)); $result = $Folder->findRecursive('(config|woot)\\.php', true); $expected = [CORE_PATH . 'config' . DS . 'config.php']; $this->assertSame($expected, $result); $path = TMP . 'tests' . DS; $Folder = new Folder($path, true); $Folder->create($path . 'sessions'); $Folder->create($path . 'testme'); $Folder->cd($path . 'testme'); $File = new File($Folder->pwd() . DS . 'paths.php'); $File->create(); $Folder->cd($path . 'sessions'); $result = $Folder->findRecursive('paths\\.php'); $expected = []; $this->assertSame($expected, $result); $Folder->cd($path . 'testme'); $File = new File($Folder->pwd() . DS . 'my.php'); $File->create(); $Folder->cd($path); $result = $Folder->findRecursive('(paths|my)\\.php'); $expected = [$path . 'testme' . DS . 'my.php', $path . 'testme' . DS . 'paths.php']; $this->assertSame(sort($expected), sort($result)); $result = $Folder->findRecursive('(paths|my)\\.php', true); $expected = [$path . 'testme' . DS . 'my.php', $path . 'testme' . DS . 'paths.php']; $this->assertSame($expected, $result); }
/** * Generate all files for a plugin * * Find the first path which contains `src/Template/Bake/Plugin` that contains * something, and use that as the template to recursively render a plugin's * contents. Allows the creation of a bake them containing a `Plugin` folder * to provide customized bake output for plugins. * * @param string $pluginName the CamelCase name of the plugin * @param string $path the path to the plugins dir (the containing folder) * @return void */ protected function _generateFiles($pluginName, $path) { $this->BakeTemplate->set(['plugin' => $pluginName, 'path' => $path, 'root' => ROOT]); $root = $path . $pluginName . DS; $paths = []; if (!empty($this->params['theme'])) { $paths[] = Plugin::path($this->params['theme']) . 'src/Template/'; } $paths = array_merge($paths, Configure::read('App.paths.templates')); $paths[] = Plugin::path('Bake') . 'src/Template/'; do { $templatesPath = array_shift($paths) . 'Bake/Plugin'; $templatesDir = new Folder($templatesPath); $templates = $templatesDir->findRecursive('.*\\.ctp'); } while (!$templates); sort($templates); foreach ($templates as $template) { $template = substr($template, strrpos($template, 'Plugin') + 7, -4); $this->_generateFile($template, $root); } }
/** * 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 * @return array */ public function getControllerList($plugin = null) { if (!$plugin) { $path = App::path('Controller'); $dir = new Folder($path[0]); $controllers = $dir->findRecursive('.*Controller\\.php'); } else { $path = App::path('Controller', $plugin); $dir = new Folder($path[0]); $controllers = $dir->findRecursive('.*Controller\\.php'); } return $controllers; }
/** * Search files that may contain translateable strings * * @return void */ protected function _searchFiles() { foreach ($this->_paths as $path) { $Folder = new Folder($path); $files = $Folder->findRecursive('.*\\.(' . implode('|', $this->settings['files']) . ')', true); foreach ($files as $file) { if (strpos($file, DS . 'Vendor' . DS) !== false) { continue; } $this->_files[] = $file; } } }
/** * Recursively search an application directory for all VERSION.txt files. * * @param string $appdir Full path to the application's root directory. * @return string|bool Strubg containing full path to VERSION.txt if found */ public function findVersionFilesRecursive($appdir) { $folder = new Folder($appdir); $files = $folder->findRecursive('VERSION.txt'); if (count($files) != 0) { return $files; } return false; }
/** * Search files that may contain translatable strings * * @return void */ protected function _searchFiles() { $pattern = false; if (!empty($this->_exclude)) { $exclude = []; foreach ($this->_exclude as $e) { if (DS !== '\\' && $e[0] !== DS) { $e = DS . $e; } $exclude[] = preg_quote($e, '/'); } $pattern = '/' . implode('|', $exclude) . '/'; } foreach ($this->_paths as $path) { $Folder = new Folder($path); $files = $Folder->findRecursive('.*\\.(php|ctp|thtml|inc|tpl)', true); if (!empty($pattern)) { foreach ($files as $i => $file) { if (preg_match($pattern, $file)) { unset($files[$i]); } } $files = array_values($files); } $this->_files = array_merge($this->_files, $files); } }
public function findControllers($controllerDir = APP) { $results = []; $dir = new Folder($controllerDir . 'Controller'); $files = $dir->findRecursive('^.*Controller\\.php$', true); $ignoreList = ['Controller' . DS . 'AppController.php']; foreach ($files as &$file) { $file = str_replace($controllerDir, '', $file); if (!in_array($file, $ignoreList)) { $controller = explode('.', $file)[0]; array_push($results, $controller); } } return $results; }