<commit_before><?php

/*
 * This file is part of the CRUDlex package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CRUDlex;

use Silex\Application;
use Silex\ServiceProviderInterface;

use Symfony\Component\Yaml\Yaml;
use CRUDlex\CRUDEntityDefinition;
use CRUDlex\CRUDDataFactoryInterface;
use CRUDlex\CRUDEntity;

class CRUDServiceProvider implements ServiceProviderInterface {

    protected $datas;

    protected $strings;

    public function init(CRUDDataFactoryInterface $dataFactory, $crudFile, $stringsFile) {
        $this->strings = Yaml::parse(file_get_contents($stringsFile));

        $cruds = Yaml::parse(file_get_contents($crudFile));
        $this->datas = array();
        foreach ($cruds as $name => $crud) {
            $label = key_exists('label', $crud) ? $crud['label'] : $name;
            $standardFieldLabels = array(
                'id' => $this->translate('label.id'),
                'created_at' => $this->translate('label.created_at'),
                'updated_at' => $this->translate('label.updated_at')
            );
            $definition = new CRUDEntityDefinition($crud['table'], $crud['fields'], $label, $standardFieldLabels);
            $this->datas[$name] = $dataFactory->createData($definition);
        }

        foreach($this->datas as $name => $data) {
            $fields = $data->getDefinition()->getFieldNames();
            foreach ($fields as $field) {
                if ($definition->getType($field) == 'reference') {
                    $this->datas[$data->getDefinition()->getReferenceEntity($field)]->getDefinition()->addParent($data->getDefinition()->getTable(), $field);
                }
            }
        }

    }

    public function register(Application $app) {
        $app['crud'] = $app->share(function() use ($app) {
            $result = new CRUDServiceProvider();
            $stringsFile = $app->offsetExists('crud.stringsfile') ? $app['crud.stringsfile'] : __DIR__.'/../strings.yml';
            $result->init($app['crud.datafactory'], $app['crud.file'], $stringsFile);
            return $result;
        });
    }

    public function boot(Application $app) {
    }

    public function getData($name) {
        if (!key_exists($name, $this->datas)) {
            return null;
        }
        return $this->datas[$name];
    }

    public function getEntities() {
        return array_keys($this->datas);
    }

    public function formatDate($value) {
        if (!$value) {
            return '';
        }
        $dateTime = new \DateTime($value);
        return $dateTime->format('Y-m-d');
    }

    public function translate($key, array $placeholders = array()) {
        if (!key_exists($key, $this->strings)) {
            return $key;
        }
        $result = $this->strings[$key];
        $amount = count($placeholders);
        for ($i = 0; $i < $amount; ++$i) {
            $result = str_replace('{'.$i.'}', $placeholders[$i], $result);
        }
        return $result;
    }

}
<commit_msg>error handling in the CrudServiceProvider initialization one of the given files does not exist<commit_after><?php

/*
 * This file is part of the CRUDlex package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CRUDlex;

use Silex\Application;
use Silex\ServiceProviderInterface;

use Symfony\Component\Yaml\Yaml;
use CRUDlex\CRUDEntityDefinition;
use CRUDlex\CRUDDataFactoryInterface;
use CRUDlex\CRUDEntity;

class CRUDServiceProvider implements ServiceProviderInterface {

    protected $datas;

    protected $strings;

    public function init(CRUDDataFactoryInterface $dataFactory, $crudFile, $stringsFile) {
        $stringsContent = @file_get_contents($stringsFile);
        if ($stringsContent === false) {
            throw new \Exception('Could not open CRUD strings file');
        }
        $this->strings = Yaml::parse($stringsContent);

        $crudsContent = @file_get_contents($crudFile);
        if ($crudsContent === false) {
            throw new \Exception('Could not open CRUD definition file');
        }
        $cruds = Yaml::parse($crudsContent);
        $this->datas = array();
        foreach ($cruds as $name => $crud) {
            $label = key_exists('label', $crud) ? $crud['label'] : $name;
            $standardFieldLabels = array(
                'id' => $this->translate('label.id'),
                'created_at' => $this->translate('label.created_at'),
                'updated_at' => $this->translate('label.updated_at')
            );
            $definition = new CRUDEntityDefinition($crud['table'], $crud['fields'], $label, $standardFieldLabels);
            $this->datas[$name] = $dataFactory->createData($definition);
        }

        foreach($this->datas as $name => $data) {
            $fields = $data->getDefinition()->getFieldNames();
            foreach ($fields as $field) {
                if ($definition->getType($field) == 'reference') {
                    $this->datas[$data->getDefinition()->getReferenceEntity($field)]->getDefinition()->addParent($data->getDefinition()->getTable(), $field);
                }
            }
        }

    }

    public function register(Application $app) {
        $app['crud'] = $app->share(function() use ($app) {
            $result = new CRUDServiceProvider();
            $stringsFile = $app->offsetExists('crud.stringsfile') ? $app['crud.stringsfile'] : __DIR__.'/../strings.yml';
            $result->init($app['crud.datafactory'], $app['crud.file'], $stringsFile);
            return $result;
        });
    }

    public function boot(Application $app) {
    }

    public function getData($name) {
        if (!key_exists($name, $this->datas)) {
            return null;
        }
        return $this->datas[$name];
    }

    public function getEntities() {
        return array_keys($this->datas);
    }

    public function formatDate($value) {
        if (!$value) {
            return '';
        }
        $dateTime = new \DateTime($value);
        return $dateTime->format('Y-m-d');
    }

    public function translate($key, array $placeholders = array()) {
        if (!key_exists($key, $this->strings)) {
            return $key;
        }
        $result = $this->strings[$key];
        $amount = count($placeholders);
        for ($i = 0; $i < $amount; ++$i) {
            $result = str_replace('{'.$i.'}', $placeholders[$i], $result);
        }
        return $result;
    }

}

{'fraction_non_alphanumeric': 0.15396203675805967, 'fraction_numerical': 0.00030129557095510696, 'mean_word_length': 2.508985200845666, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '956041', 'n_tokens_mistral': 1933, 'n_tokens_neox': 1885, 'n_words': 405}