lithium\console\command\Create::_save PHP Method

_save() protected method

Save a template with the current params. Writes file to Create::$path.
protected _save ( array $params = [] ) : string | boolean
$params array
return string | boolean A result string on success of writing the file. If any errors occur along the way such as missing information boolean false is returned.
    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);
        $namespace = str_replace($this->_library['prefix'], '\\', $params['namespace']);
        $path = str_replace('\\', '/', "{$namespace}\\{$params['class']}");
        $path = $this->_library['path'] . stristr($path, '/');
        $file = str_replace('//', '/', "{$path}.php");
        $directory = dirname($file);
        $relative = str_replace($this->_library['path'] . '/', "", $file);
        if (!is_dir($directory) && !mkdir($directory, 0755, true)) {
            return false;
        }
        if (file_exists($file)) {
            $prompt = "{$relative} 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 {$relative}.";
        }
        return false;
    }