Extends
lithium\console\Command
Save a template with the current params. Writes file to `Create::$path`.
Parameters
- array $params
Returns
string A result string on success of writing the file. If any errors occur along the way such as missing information boolean false is returned.Source
protected function _save(array $params = array()) {
$defaults = array('namespace' => null, 'class' => null);
$params += $defaults;
if (empty($params['class']) || empty($this->_library['path'])) {
return false;
}
$contents = $this->_template();
$result = String::insert($contents, $params);
$path = str_replace('\\', '/', "{$params['namespace']}\\{$params['class']}");
$path = $this->_library['path'] . stristr($path, '/');
$file = str_replace('//', '/', "{$path}.php");
$directory = dirname($file);
if ((!is_dir($directory)) && !mkdir($directory, 0755, true)) {
return false;
}
if (file_exists($file)) {
$prompt = "{$file} already exists. Overwrite?";
$choices = array('y', 'n');
if ($this->in($prompt, compact('choices')) != 'y') {
return "{$params['class']} skipped.";
}
}
if (file_put_contents($file, "<?php\n\n{$result}\n\n?>")) {
return "{$params['class']} created in {$params['namespace']}.";
}
return false;
}